theano使用
tensor:高维数组,T 里面其实有scalar (一个数据点),vector (向量),matrix (矩阵),tensor3 (三维矩阵),tensor4 (四位矩阵)这些都落入tensor的范畴。
dscalar:不是一个类,是一个TensorVariable实例。 特别的,T.dscalar指:doubles(d)型的0维arrays(scalar)。
pp:一个函数,from theano import pp print(pp(z)) 则pretty-print 关于z的计算:输出(x+y).
以下为具体类型(theano 0.8.2):
1 2 3 4 5 | import theano a = theano.tensor.vector() # 引入tensor中的vector型 out = a + a * * 10 f = theano.function([a], out) print (f([ 0 , 1 , 2 ])) # 输出[0. 2. 1026.] |
logistics代码:
1 2 3 4 5 6 7 | import theano import theano.tensor as T x = T.dmatrix( 'x' ) s = 1 / ( 1 + T.exp( - x)) logistic = theano.function([x], s) logistic([[ 0 , 1 ],[ - 1 , - 2 ]]) # 输出array([[0.5 ,0.73105858], [ 0.26894142 , 0.11920292 ]]) |
一次计算多项:
1 2 3 4 5 6 7 8 9 10 | >>> a, b = T.dmatrices( 'a' , 'b' ) # dmatrices 提供多个输出,这是声明多变量的一个捷径 >>> diff = a - b >>> abs_diff = abs (diff) >>> diff_squared = diff * * 2 >>> f = theano.function([a, b], [diff, abs_diff, diff_squared]) >>> f([[ 1 , 1 ], [ 1 , 1 ]], [[ 0 , 1 ], [ 2 , 3 ]]) [array([[ 1. , 0. ], [ - 1. , - 2. ]]), array([[ 1. , 0. ], [ 1. , 2. ]]), array([[ 1. , 0. ], [ 1. , 4. ]])] |
为参数设定默认值,引入function中的参数In
>>> from theano import In >>> from theano import function >>> x, y = T.dscalars('x', 'y') >>> z = x + y >>> f = function([x, In(y, value=1)], z) # 引入类In:允许你为函数参数进行更多细节上的特定化 >>> f(33) array(34.0) >>> f(33, 2) array(35.0) >>> x, y, w = T.dscalars('x', 'y', 'w') >>> z = (x + y) * w >>> f = function([x, In(y, value=1), In(w, value=2, name='w_by_name')], z) # 注意这里引入name >>> f(33) array(68.0) >>> f(33, 2) array(70.0) >>> f(33, 0, 1) array(33.0) >>> f(33, w_by_name=1) array(34.0) >>> f(33, w_by_name=1, y=0) array(33.0)
利用共享变量(Shared Variables)
例如我们想造一个累加器,开始初始化为0,随着函数每被调用一次,累加器通过函数声明进行叠加。shared函数构造了一个称为 shared vairables的结构,其值被很多函数共享,其值可以通过调用.get_value()来access,通过.set_value()来modified.
另一个说明:在function中引入参数updates .function.updates必须以pairs(shared-variable, new expression)的列表形式提供,当然形式也可以是字典(其键为shared-variables,值为new expression)。顾名思义,update就是用后面的值代替前面的值。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | >>> from theano import shared >>> state = shared( 0 ) >>> inc = T.iscalar( 'inc' ) >>> accumulator = function([inc], state, updates = [(state, state + inc)]) >>> print (state.get_value()) 0 >>> accumulator( 1 ) array( 0 ) >>> print (state.get_value()) 1 >>> accumulator( 300 ) array( 1 ) >>> print (state.get_value()) 301 >>> state.set_value( - 1 ) >>> accumulator( 3 ) array( - 1 ) >>> print (state.get_value()) 2 # 此时共享变量值为2,注意下文 >>> decrementor = function([inc], state, updates = [(state, state - inc)]) # 定义另一个函数来共享shared variable >>> decrementor( 2 ) # 给inc赋值为2 array( 2 ) # 此时输出共享变量值还为2,注意上文 >>> print (state.get_value()) # update 将state更新为0 0 |
利用function中参数givens
givens参数被用来替代任何符号变量,不仅仅是共享变量,你可以用来替代常量,表达式。注意不要引入一个互相依赖的替代品,因为替代者的顺序没有定义,所以他们会以任意顺序工作。实际中,可以将givens看作一种机制:允许你用不同的表示方法(evaluates to a tensor of same shape and dtype,相同的尺寸和类型)替代你的任何公式。
1 2 3 4 5 6 7 8 9 | >>> fn_of_state = state * 2 + inc >>> # The type of foo must match the shared variable we are replacing >>> # with the ``givens`` >>> foo = T.scalar(dtype = state.dtype) # 因为下文要用foo代替state,所以要获得相同类型 >>> skip_shared = function([inc, foo], fn_of_state, givens = [(state, foo)]) # 这里用foo代替state! >>> skip_shared( 1 , 3 ) # we're using 3 for the state, not state.value # 这里的1 赋值给了inc, 3赋值给了foo, 在计算中,用foo代替了state array( 7 ) # state *2+inc变为 foo *2+inc ,所以为7 >>> print (state.get_value()) # old state still there, but we didn't use it # state 值没变,所以仍然为0 0 |
copy 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | > import theano >>> import theano.tensor as T >>> state = theano.shared( 0 ) >>> inc = T.iscalar( 'inc' ) >>> accumulator = theano.function([inc], state, updates = [(state, state + inc)],on_unused_input = 'ignore' ) >>> accumulator( 10 ) array( 0 ) >>> print (state.get_value()) 10 >>> new_state = theano.shared( 0 ) >>> new_accumulator = accumulator.copy(swap = {state:new_state}) # 利用swap参数将new_state替代原accumulate中的state >>> new_accumulator( 100 ) [array( 0 )] >>> print (new_state.get_value()) 100 >>> print (state.get_value()) # 原函数中的state值未变 10 >>> null_accumulator = accumulator.copy(delete_updates = True ) # 再定义一个新的accumulator函数,新函数移除掉了update >>> null_accumulator( 9000 ) [array( 10 )] >>> print (state.get_value()) # 这个新函数没有了uodates功能,同时也不再使用参数 inc 10 # 如果没有移除updates,则值应该为9010。移除后,只剩state的值 |
随机数 Random Numbers
from theano.tensor.shared_randomstreams import RandomStreams from theano import function srng = RandomStreams(seed=234) rv_u = srng.uniform((2,2)) # 服从联合分布(uniform distribution)的2*2的随机矩阵 rv_n = srng.normal((2,2)) # 服从正态分布(normal distribution)的2*2的随机矩阵 f = function([], rv_u) g = function([], rv_n, no_default_updates=True) #Not updating rv_n.rng #不再更新rv_n,即不管调用几次,这个值不变 nearly_zeros = function([], rv_u + rv_u - 2 * rv_u) # remark:一个随机变量在简单函数里只生成一次,所以这个函数值虽然有三次rv_u,但是函数值应该为零! >>> f_val0 = f() >>> f_val1 = f() #different numbers from f_val0 # 两次调用,两种不同结果 >>> g_val0 = g() # different numbers from f_val0 and f_val1 >>> g_val1 = g() # same numbers as g_val0! # 两次调用,两种相同结果
补充:随机抽样(numpy.random)
rand(d0,d1,...,dn) >>>np.random.rand(a,b) a*b矩阵随机值
randn(d0,d1,...,dn) >>>np.random.randn() 返回一个标准正态分布的样本
randint(low[,high,size]) >>>np.random.randint(2, size=10) 1*10维整型数组,最大值小于2 开区间
>>>np.random.randint(size=10, low=0, high=3) 1*10维整型数组,最低可取0,最大不可取3
random_integers(low[,high,size]) >>>np.random.random_integers(5, size=(3.,2.)) 用法同randint, 闭区间
random_sample([size])、random([size])、ranf([size])、sample([size]) 返回半开区间 [0.0, 1.0) 的随机浮点数
choice(a[,size,replace,p]) >>>np.random.choice(5,3) 最大为4,数目为3的一个随机数组
>>>np.random.choice(5,3,p=[0.1, 0, 0.3, 0.6, 0]) Generate a non-uniform random sample from np.arange(5) of size 3:
>>> np.random.choice(5, 3, replace=False) array([3,1,0])
Generate a uniform random sample from np.arange(5) of size 3 without replacement
>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0]) array([2, 3, 0])
Generate a non-uniform random sample from np.arange(5) of size 3 without replacement
bytes: 返回随机字节 >>> np.random.bytes(10) ‘ eh\x85\x022SZ\xbf\xa4‘ #random
关于排列:
shuffle(x): 现场修改序列,改变自身内容。(类似洗牌,打乱顺序)
亲爱的读者和支持者们,自动博客加入了打赏功能,陆陆续续收到了各位老铁的打赏。在此,我想由衷地感谢每一位对我们博客的支持和打赏。你们的慷慨与支持,是我们前行的动力与源泉。
日期 | 姓名 | 金额 |
---|---|---|
2023-09-06 | *源 | 19 |
2023-09-11 | *朝科 | 88 |
2023-09-21 | *号 | 5 |
2023-09-16 | *真 | 60 |
2023-10-26 | *通 | 9.9 |
2023-11-04 | *慎 | 0.66 |
2023-11-24 | *恩 | 0.01 |
2023-12-30 | I*B | 1 |
2024-01-28 | *兴 | 20 |
2024-02-01 | QYing | 20 |
2024-02-11 | *督 | 6 |
2024-02-18 | 一*x | 1 |
2024-02-20 | c*l | 18.88 |
2024-01-01 | *I | 5 |
2024-04-08 | *程 | 150 |
2024-04-18 | *超 | 20 |
2024-04-26 | .*V | 30 |
2024-05-08 | D*W | 5 |
2024-05-29 | *辉 | 20 |
2024-05-30 | *雄 | 10 |
2024-06-08 | *: | 10 |
2024-06-23 | 小狮子 | 666 |
2024-06-28 | *s | 6.66 |
2024-06-29 | *炼 | 1 |
2024-06-30 | *! | 1 |
2024-07-08 | *方 | 20 |
2024-07-18 | A*1 | 6.66 |
2024-07-31 | *北 | 12 |
2024-08-13 | *基 | 1 |
2024-08-23 | n*s | 2 |
2024-09-02 | *源 | 50 |
2024-09-04 | *J | 2 |
2024-09-06 | *强 | 8.8 |
2024-09-09 | *波 | 1 |
2024-09-10 | *口 | 1 |
2024-09-10 | *波 | 1 |
2024-09-12 | *波 | 10 |
2024-09-18 | *明 | 1.68 |
2024-09-26 | B*h | 10 |
2024-09-30 | 岁 | 10 |
2024-10-02 | M*i | 1 |
2024-10-14 | *朋 | 10 |
2024-10-22 | *海 | 10 |
2024-10-23 | *南 | 10 |
2024-10-26 | *节 | 6.66 |
2024-10-27 | *o | 5 |
2024-10-28 | W*F | 6.66 |
2024-10-29 | R*n | 6.66 |
2024-11-02 | *球 | 6 |
2024-11-021 | *鑫 | 6.66 |
2024-11-25 | *沙 | 5 |
2024-11-29 | C*n | 2.88 |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了