numpy.einsum for朱莉娅? [英] numpy.einsum for Julia?
问题描述
我想知道如何在Julia中获得类似于numpy.einsum的功能.
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).
当前,我正在使用PyCall,以便可以像这样使用numpy.einsum函数:
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,)
依赖于调用python来进行张量数学是一种愚蠢的选择.我还认为,实施茱莉亚会带来速度优势.但是,我在julia中没有任何功能,蛮力求和要慢1-2个数量级.我可以使用哪些功能?
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?
推荐答案
sum(a.*b,(1,2))
不需要做什么吗?
这篇关于numpy.einsum for朱莉娅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!