无法平铺时的 Theano 减法
Theano subtraction when you can't tile
假设我们有一个 nxm 的 theano 矩阵 X,还有一个 nx1 的 u。我们想做 X-u
,但如果我们这样做,我们将得到输入维度不匹配。我们可以尝试 tiling u,但 tile 只接受常量而不接受变量。我们如何做到这一点?
import theano
import theano.tensor as T
X, u = T.dmatrices("X", "u")
T.addbroadcast(u, 1)
r = X - u
f = theano.function([X, u], r)
f([[1], [0]], [[1]])
然后我收到错误 ('Bad input argument to theano function with name "<stdin>:1" at index 0(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (2,).')
X - u
应该与您通过广播编写的完全一样:
import theano
import theano.tensor as T
n = 10
m = 20
X = T.arange(n * m).reshape((n, m))
u = T.arange(0, n * m, m).reshape((n, 1))
r = X - u
r.eval()
与您更新后的问题类似,您可以这样做
import theano
import theano.tensor as T
X = T.dmatrix()
u = T.addbroadcast(T.dmatrix(), 1)
r = X - u
f = theano.function([X, u], r)
XX = np.arange(20.).reshape(2, 10)
uu = np.array([1., 100.]).reshape(2, 1)
f(XX, uu)
假设我们有一个 nxm 的 theano 矩阵 X,还有一个 nx1 的 u。我们想做 X-u
,但如果我们这样做,我们将得到输入维度不匹配。我们可以尝试 tiling u,但 tile 只接受常量而不接受变量。我们如何做到这一点?
import theano
import theano.tensor as T
X, u = T.dmatrices("X", "u")
T.addbroadcast(u, 1)
r = X - u
f = theano.function([X, u], r)
f([[1], [0]], [[1]])
然后我收到错误 ('Bad input argument to theano function with name "<stdin>:1" at index 0(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (2,).')
X - u
应该与您通过广播编写的完全一样:
import theano
import theano.tensor as T
n = 10
m = 20
X = T.arange(n * m).reshape((n, m))
u = T.arange(0, n * m, m).reshape((n, 1))
r = X - u
r.eval()
与您更新后的问题类似,您可以这样做
import theano
import theano.tensor as T
X = T.dmatrix()
u = T.addbroadcast(T.dmatrix(), 1)
r = X - u
f = theano.function([X, u], r)
XX = np.arange(20.).reshape(2, 10)
uu = np.array([1., 100.]).reshape(2, 1)
f(XX, uu)