0
我有一个Theano dvector
与100个元素。我也有一个有5列和100行的矩阵(换句话说,每列包含100个元素)。如何将矩阵的每列乘以Theano中的矢量元素?
现在我需要应用每个列的元素乘法的矢量。什么是在Theano做正确的方法?
我应该创建一个新的矩阵:重复我的向量5次并对其进行移位,然后乘以相同形状的两个矩阵元素?
ADDED
我已经了解到,在numpy的,以达到所期望的行为,我只需要声明我的向量作为二维数组与一列。换句话说,我需要用“列” - 向量替换“行”向量(或者我需要垂直写入值,而不是水平)。在这种情况下,numpy会根据需要播放向量(列)(我的矩阵的每一列将被乘以我的向量元素)。然而,它看起来像Theano不继承numpy的这种行为:
X = T.dmatrix('X')
w = np.array([
[10.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 10.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 10.0, 0.0, 0.0]
], dtype=th.config.floatX)
w = np.transpose(w)
W = th.shared(w, name='W', borrow=True)
R = W + X
f = th.function([X], R)
x = np.array([[1.0], [2.0], [3.0], [4.0], [5.0]])
print f(x)
这是我的错误:
ValueError: Input dimension mis-match. (input[0].shape[1] = 3, input[1].shape[1] = 1)
Apply node that caused the error: Elemwise{add,no_inplace}(W, X)
Toposort index: 0
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(5, 3), (5, 1)]
Inputs strides: [(8, 40), (8, 8)]
顺便说一句,代码工作,如果我在下面的定义x
way:
x = np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0], [4.0, 4.0, 4.0], [5.0, 5.0, 5.0]])