我刚刚了解了np.einsum
,很快就沉迷于它。但似乎theano
没有等效函数,因此我需要以某种方式将numpy
代码转换为theano。如何在theano中编写以下计算?
IX=np.einsum('ijk,lj->ilk',p1,KX)
答案 0 :(得分:6)
您只需重新排列轴即可使其正常工作:
>>> import numpy as np
>>> a = np.random.rand(3, 4, 5)
>>> b = np.random.rand(5, 6)
>>> np.allclose(np.einsum('ikj,jl->ikl', a, b), np.dot(a, b))
考虑到这一点:
>>> a = np.random.rand(3, 5, 4)
>>> b = np.random.rand(6, 5)
>>> out_ein = np.einsum('ijk,lj->ilk', a, b)
>>> out_dot = np.transpose(np.dot(np.transpose(a, (0, 2, 1)),
... np.transpose(b, (1, 0))),
... (0, 2, 1))
>>> np.allclose(out_ein, out_dot)