theano:两个矩阵之间的行外积
theano: row-wise outer product between two matrices
我正在尝试在不使用扫描的情况下计算 theano 中两个矩阵之间的行向外积。我可以通过使用 einsum 在 numpy 中执行此操作,这在 theano 中不可用。
A = np.array([[1,1,1],[2,2,2]])
B = np.array([[3,3,3,3],[4,4,4,4]])
print np.einsum('xi,xj->xij', A, B)
[[[3 3 3 3]
[3 3 3 3]
[3 3 3 3]]
[[8 8 8 8]
[8 8 8 8]
[8 8 8 8]]]
这应该可以通过一些重塑来实现:许多简单的 einsum 操作都归结为这一点。复杂的不行。
import theano
import theano.tensor as T
import numpy as np
a = np.array([[1,1,1],[2,2,2]]).astype('float32')
b = np.array([[3,3,3,3],[4,4,4,4]]).astype('float32')
A = T.fmatrix()
B = T.fmatrix()
C = A[:, :, np.newaxis] * B[:, np.newaxis, :]
print C.eval({A:a, B:b})
结果
[[[ 3. 3. 3., 3.]
[ 3. 3. 3., 3.]
[ 3. 3. 3.. 3.]]
[[ 8. 8. 8., 8.]
[ 8. 8. 8., 8.]
[ 8. 8. 8., 8.]]]
我正在尝试在不使用扫描的情况下计算 theano 中两个矩阵之间的行向外积。我可以通过使用 einsum 在 numpy 中执行此操作,这在 theano 中不可用。
A = np.array([[1,1,1],[2,2,2]])
B = np.array([[3,3,3,3],[4,4,4,4]])
print np.einsum('xi,xj->xij', A, B)
[[[3 3 3 3]
[3 3 3 3]
[3 3 3 3]]
[[8 8 8 8]
[8 8 8 8]
[8 8 8 8]]]
这应该可以通过一些重塑来实现:许多简单的 einsum 操作都归结为这一点。复杂的不行。
import theano
import theano.tensor as T
import numpy as np
a = np.array([[1,1,1],[2,2,2]]).astype('float32')
b = np.array([[3,3,3,3],[4,4,4,4]]).astype('float32')
A = T.fmatrix()
B = T.fmatrix()
C = A[:, :, np.newaxis] * B[:, np.newaxis, :]
print C.eval({A:a, B:b})
结果
[[[ 3. 3. 3., 3.]
[ 3. 3. 3., 3.]
[ 3. 3. 3.. 3.]]
[[ 8. 8. 8., 8.]
[ 8. 8. 8., 8.]
[ 8. 8. 8., 8.]]]