numpy.einsum for Julia?

断了今生、忘了曾经 提交于 2019-12-11 00:03:37

问题


I'm wondering how to get functionality similar to numpy.einsum in Julia.

Specifically, I have a 3rd order tensor that I'm looking to multiply by a 2nd tensor (matrix), contracting both of the dimensions to yield a 1st order tensor (vector).

Currently, I'm using PyCall so that I can use the numpy.einsum function like so:

using PyCall
@pyimport numpy as np

a = rand(5,4,3)
b = rand(5,4)

c = np.einsum("ijk,ij", a,b)
size(c) == (3,)

It feels kind of silly to rely on calling python in order to do tensor math. I also imagine that a julia implementation would have speed advantages. However, I haven't any function for this in julia, and the brute force summation is 1-2 orders of magnitude slower. What functions can I use?


回答1:


Doesn't sum(a.*b,(1,2)) do what you want?



来源:https://stackoverflow.com/questions/22521574/numpy-einsum-for-julia

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