我最近才了解np.einsum并很快上瘾。但是似乎theano没有等效功能,因此我需要以numpy某种方式将代码转换为theano。如何在theano中编写以下计算?
np.einsum
theano
numpy
IX=np.einsum('ijk,lj->ilk',p1,KX)
您只需要重新排列轴即可使其工作:
>>> 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)