原文地址:http://deeplearning.net/software/theano/tutorial/shape_info.html
严格给定Theano变量的形状是不太可能的,当一个特定值被在实时提供到Theano函数是,函数会确定条件的形状。
目前,不管形状Theano的信息在两种方法被使用: - 当标准形状提前被知道时,为了生成更快的C代码在CPU和GPU的2维卷积。 - 当我们只需要知道形状而不需要具体的值时。
import theano x = theano.tensor.matrix('x') f = theano.function([x], (x**2).shape) theano.printing.debugprint(f) #MakeVector{dtype='int64'} [id A] '' 2 # |Shape_i{0} [id B] '' 1 # | |x [id C] # |Shape_i{1} [id D] '' 0 # |x [id C]输出的编译函数不会包括任意乘法或乘方。Theano移除了他们为了直接计算他们的形状。
Theano在图里传播形状的信息。有时候会带来问题。
import numpy import theano x = theano.tensor.matrix('x') y = theano.tensor.matrix('y') z = theano.tensor.join(0, x, y) xv = numpy.random.rand(5,4) yv = numpy.random.rand(3,3) f = theano.function([x, y], z.shape) theano.printing.debugprint(f) #MakeVector{dtype='int64'} [id A] '' 4 # |Elemwise{Add}[(0, 0)] [id B] '' 3 # | |Shape_i{0} [id C] '' 1 # | | |x [id D] # | |Shape_i{0} [id E] '' 2 # | |y [id F] # |Shape_i{1} [id G] '' 0 # |x [id D] f(xv, yv) # 没有问题 f = theano.function([x,y],z) theano.orinting.debugprintf(f) #Join [id A] '' 0 # |TensorConstant{0} [id B] # |x [id C] # |y [id D] f(xv, yv) # 出错