- 只有
thenao.shared()
类型才有get_value()
成员函数(返回numpy.ndarray
)?
0. 图结构
Theano编程的核心是用符号占位把数学关系表示出来。
import theano.tensor as T
x = T.dmatrix('x')
y = x*2.
>>> y.owner.op.name
'Elemwise{mul,no_inplace}'
>>> y.owner.inputs
[x, DimShuffle{x,x}.0]
# 返回 list
>>> y.owner.inputs[0]
x
>>> y.owner.inputs[1]
DimShuffle{x,x}.0
编译Theano其实是编译了一张图。
a = T.vector('a') # declare symbolic variable
b = a + a**10 # build symbolic expression
f = theano.function([a], b)
# compile function
print(f([0, 1, 2]))
图结构中在编译之前要求参与运算的数据结构都需声明各个维度是否可broadcastable。numpy使用的是运行时的shape信息(不需声明,自动broadcast?)。
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 ndarrayvector
:1-dim ndarraymatrix
:2-dim ndarraytensor3
:3-dim ndarraytensor4
: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 W <script type="math/tex" id="MathJax-Element-2">W</script>
- # 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)