Convert einsum computation to dot product to be used in Theano

放肆的年华 提交于 2019-12-01 04:31:33

问题


I have just recently learned about np.einsum and quickly became addicted to it. But it seems that theano doesn't have an equivalent function so I need to convert my numpy code to theano somehow. How can I write the following computation in theano?

IX=np.einsum('ijk,lj->ilk',p1,KX)

回答1:


You only need to rearrange your axes to get this to work:

>>> 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))

So with that in mind:

>>> 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)


来源:https://stackoverflow.com/questions/30305891/convert-einsum-computation-to-dot-product-to-be-used-in-theano

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!