我注意到,在theano,当一个创建基于1D numpy的阵列上的共享变量,这成为一个载体,但不是排:创建theano共享行
import theano.tensor as T
import theano, numpy
shared_vector = theano.shared(numpy.zeros((10,)))
print(shared_vector.type)
# TensorType(float64, vector)
print(shared_vector.broadcastable)
# (False,)
这同样适用于一个1×N个矩阵,它成为一个矩阵,但不是排:
shared_vector = theano.shared(numpy.zeros((1,10,)))
print(shared_vector.type)
# TensorType(float64, matrix)
print(shared_vector.broadcastable)
# (False, False)
这是麻烦的,当我想一个M×N的矩阵添加至1 XN行向量,因为共享矢量不在broadcastable第一个维度。首先,这是不行的:
row = T.row('row')
mat=T.matrix('matrix')
f=theano.function(
[],
mat + row,
givens={
mat: numpy.zeros((20,10), dtype=numpy.float32),
row: numpy.zeros((10,), dtype=numpy.float32)
},
on_unused_input='ignore'
)
与错误:
TypeError: Cannot convert Type TensorType(float32, vector) (of Variable <TensorType(float32, vector)>) into Type TensorType(float32, row). You can try to manually convert <TensorType(float32, vector)> into a TensorType(float32, row).
好吧,这是明确的,我们不能分配向量行。不幸的是,这也是不精:
row = T.matrix('row')
mat=T.matrix('matrix')
f=theano.function(
[],
mat + row,
givens={
mat: numpy.zeros((20,10), dtype=numpy.float32),
row: numpy.zeros((1,10,), dtype=numpy.float32)
},
on_unused_input='ignore'
)
f()
与错误:
ValueError: Input dimension mis-match. (input[0].shape[0] = 20, input[1].shape[0] = 1)
Apply node that caused the error: Elemwise{add,no_inplace}(<TensorType(float32, matrix)>, <TensorType(float32, matrix)>)
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(20, 10), (1, 10)]
Inputs strides: [(40, 4), (40, 4)]
Inputs values: ['not shown', 'not shown']
Backtrace when the node is created:
File "<ipython-input-55-0f03bee478ec>", line 5, in <module>
mat + row,
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
因此,我们不能只用一个1×N矩阵作为行以及(因为的第一维1×N矩阵不可广播)。
问题依然存在,我们该做什么?我如何创建一个类型行的共享变量,这样我可以使用矩阵行添加进行广播?
我看来,我已经找到了,发布后秒。我想我可以在矢量上使用.reshape,创建一个1×N矩阵,它具有可广播(True,False) – Herbert