【发布时间】:2014-12-21 20:21:42
【问题描述】:
考虑以下 numpy 广播示例:
import numpy as np
import theano
from theano import tensor as T
xval = np.array([[1, 2, 3], [4, 5, 6]])
bval = np.array([[10, 20, 30]])
print xval + bval
正如预期的那样,向量bval被添加到矩阵xval的每一行,输出为:
[[11 22 33]
[14 25 36]]
尝试在 git 版本的 theano 中复制相同的行为:
x = T.dmatrix('x')
b = theano.shared(bval)
z = x + b
f = theano.function([x], z)
print f(xval)
我收到以下错误:
ValueError: Input dimension mis-match. (input[0].shape[0] = 2, input[1].shape[0] = 1)
Apply node that caused the error: Elemwise{add,no_inplace}(x, <TensorType(int64, matrix)>)
Inputs types: [TensorType(float64, matrix), TensorType(int64, matrix)]
Inputs shapes: [(2, 3), (1, 3)]
Inputs strides: [(24, 8), (24, 8)]
Inputs scalar values: ['not scalar', 'not scalar']
我了解Tensor 对象(例如x)具有broadcastable 属性,但我无法找到一种方法来1)为shared 对象正确设置它或2)正确推断。如何在 theano 中重新实现 numpy 的行为?
【问题讨论】: