程序项目代做,有需求私信(小程序、网站、爬虫、电路板设计、驱动、应用程序开发、毕设疑难问题处理等)

theano使用

一  theano内置数据类型

只有thenao.shared()类型才有get_value()成员函数(返回numpy.ndarray)?

1. 惯常处理

复制代码
x = T.matrix('x')  # the data is presented as rasterized images
y = T.ivector('y') # the labels are presented as 1D vector of [int] labels

# reshape matrix of rasterized images of shape 
# (batch_size, 28*28) to a 4D tensor, 使其与LeNetConvPoolLayer相兼容
layer0_input = x.reshape((batch_size, 1, 28, 28))

>>> x.reshape((500, 3, 28, 28))
TensorType(float64, 4D)
>>> x.type
TensorType(float64, matrix)
>>> layer0_input.type
TensorType(float64, (False, True, False, False))
            # 布尔值表示是否可被broadcast
>>> x.reshape((500, 3, 28, 28)).type
TensorType(float64, 4D)
>>> T.dtensor4().type
TensorType(float64, 4D)  
复制代码

2. theano.shared 向 numpy.ndarray 的转换

# train_set_x: theano.shared()类型
train_set_x.get_value(borrow=True)
        # 返回的正是ndarray类型,borrow=True表示返回的是“引用”
train_set_x.get_value(borrow=True).shape[0]

3. built-in data types

查阅theano完备的文档,我们知:

theano所内置的数据类型主要位于theano.tensor子模块下,

import theano.tensor as T

  • b开头,表示byte类型(bscalar, bvector, bmatrix, brow, bcol, btensor3,btensor4)
  • w开头,表示16-bit integers(wchar)(wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4)
  • i开头,表示32-bit integers(int)(iscalar, ivector, imatrix, irow, icol, itensor3, itensor4)
  • l开头,表示64-bit integers(long)(lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4)
  • f开头,表示float类型(fscalar, fvector, fmatrix, fcol, frow, ftensor3, ftensor4)
  • d开头,表示double类型(dscalar, dvector, dmatrix, dcol, drow, dtensor3, dtensor4)
  • c开头,表示complex类型(cscalar, cvector, cmatrix, ccol, crow, ctensor3, ctensor4)

这里的tensor3/4类型也不神秘,

  • scalar:0-dim ndarray
  • vector:1-dim ndarray
  • matrix:2-dim ndarray
  • tensor3:3-dim ndarray
  • tensor4:4-dim ndarray

注意以上这些类型的类型都是theano.tensor.var.TensorVariable

>>> x = T.iscalar('x')
>>> type(x)
theano.tensor.var.TensorVariable
>>> x.type
TensorType(int32, scalar)

我们继续考察tensor

>>> x = T.dmatrix()
>>> x.type

>>> x = T.matrix()
>>> x.type

在设计经典的卷积神经网络(CNN)时,在输入层和第一个隐层之间需要加一个卷积的动作,对应的api是theano.tensor.signal.conv.conv2d,其主要接受两个符号型输入symbolic inputs

一个4d的tensor对应于mini_batch的输入图像:

  • mini_batch_size
  • # of feature input maps
  • image height
  • image width

一个4d的tensor对应于权值矩阵W

  • # of feature output maps(也即 # of filters)
  • # of feature input maps
  • filter height
  • filter width
复制代码
rng = np.random.RandomState(23455)
input = T.dtensor4('input')
w_shp = (2, 3, 9, 9)
            # 3 means: rgb, 图像的三种颜色分量
w_bound = np.sqrt(np.prod(w_shp[1:]))
W = theano.shared(np.asarray(rng.uniform(low=-1./w_bound, high=1./w_bound, size=w_shp), dtype=input.dtype), name='W')
conv_out = conv.conv2d(input, W)
import numpy
import theano.tensor as T
from theano import function
x = T.dscalar('x')    
y = T.dscalar('y')
z = x + y
f = function([x, y], z)
numpy.allclose(f(16.3, 12.1), 28.4)     输出为true
numpy.allclose(z.eval({x:16.3, y:12.1}, 28.4))    输出为true
复制代码

二 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-30I*B1
2024-01-28*兴20
2024-02-01QYing20
2024-02-11*督6
2024-02-18一*x1
2024-02-20c*l18.88
2024-01-01*I5
2024-04-08*程150
2024-04-18*超20
2024-04-26.*V30
2024-05-08D*W5
2024-05-29*辉20
2024-05-30*雄10
2024-06-08*:10
2024-06-23小狮子666
2024-06-28*s6.66
2024-06-29*炼1
2024-06-30*!1
2024-07-08*方20
2024-07-18A*16.66
2024-07-31*北12
2024-08-13*基1
2024-08-23n*s2
2024-09-02*源50
2024-09-04*J2
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-26B*h10
2024-09-3010
2024-10-02M*i1
2024-10-14*朋10
2024-10-22*海10
2024-10-23*南10
2024-10-26*节6.66
2024-10-27*o5
2024-10-28W*F6.66
2024-10-29R*n6.66
2024-11-02*球6
2024-11-021*鑫6.66
2024-11-25*沙5
2024-11-29C*n2.88
posted @   大奥特曼打小怪兽  阅读(903)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
如果有任何技术小问题,欢迎大家交流沟通,共同进步

公告 & 打赏

>>

欢迎打赏支持我 ^_^

最新公告

程序项目代做,有需求私信(小程序、网站、爬虫、电路板设计、驱动、应用程序开发、毕设疑难问题处理等)。

了解更多

点击右上角即可分享
微信分享提示