theano学习——内置数据类型

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

惯常处理

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)         

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]

查阅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的输入图像

    1. mini_batch_size
    2. # of feature input maps
    3. image height
    4. image width
  • 一个4d的tensor对应于权值矩阵 W

    1. # of feature output maps(也即 # of filters)
    2. # of feature input maps
    3. filter height
    4. 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)

你可能感兴趣的:(theano学习——内置数据类型)