请考虑以下代码段:
import theano.tensor as T
import theano.tensor
import numpy as np
batch_shape = (50, 40, 30, 30)
batch_size = batch_shape[0]
ncols = batch_shape[1]*batch_shape[2]*batch_shape[3]
minibatch = theano.tensor.tensor4(name='minibatch',
dtype=theano.config.floatX)
xflat = minibatch.reshape((batch_size,ncols))
partition = np.array([1, 2, 3])
xsub1 = xflat[:,partition]
partition = np.array([1])
xsub2 = xflat[:,partition]
print "xsub1.type: ", xsub1.type
print "xsub2.type: ", xsub2.type
如果你运行它,你会得到以下输出:
xsub1.type: TensorType(float64, matrix)
xsub2.type: TensorType(float64, col)
显然使用长度为1的数组进行索引会将xsub2转换为col而不是矩阵。我怎样才能使xsub2成为一个矩阵?
答案 0 :(得分:2)
col
或“列向量”是Theano用于符号矩阵的名称,它知道它只包含一列。应该可以像矩阵一样使用它。
Theano通常不知道特定符号张量的形状,只知道它的维度。然而,在某些情况下,例如问题中给出的情况,Theano能够推断出张量具有特定形状的特殊情况,并且有时可以使用该信息来优化计算。这就是col
(和row
)作为matrix
的特殊情况而存在的原因。
如果你考虑的形状不仅仅是类型,那么你会发现Theano的表现与numpy相同:
import theano
import theano.tensor
import numpy as np
def compute(minibatch):
xflat = minibatch.reshape((minibatch.shape[0], -1))
partition = np.array([1, 2, 3])
xsub1 = xflat[:, partition]
partition = np.array([1])
xsub2 = xflat[:, partition]
return xsub1, xsub2
def compile_theano_version():
minibatch = theano.tensor.tensor4(name='minibatch', dtype=theano.config.floatX)
xsub1, xsub2 = compute(minibatch)
print xsub1.type, xsub2.type
return theano.function([minibatch], [xsub1, xsub2])
def numpy_version(minibatch):
return compute(minibatch)
def main():
batch_shape = (50, 40, 30, 30)
minibatch = np.random.standard_normal(size=batch_shape).astype(theano.config.floatX)
xsub1, xsub2 = numpy_version(minibatch)
print xsub1.shape, xsub2.shape
theano_version = compile_theano_version()
xsub1, xsub2 = theano_version(minibatch)
print xsub1.shape, xsub2.shape
main()
打印
(50L, 3L) (50L, 1L)
TensorType(float64, matrix) TensorType(float64, col)
(50L, 3L) (50L, 1L)
所以col
确实是一个包含一列且不是一个向量的矩阵。