求和einsum中多个向量的外积 [英] summing outer product of multiple vectors in einsum

查看:162
本文介绍了求和einsum中多个向量的外积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 einsum手册和ajcr的基本介绍

I have read through the einsum manual and ajcr's basic introduction

我在非编码环境中对爱因斯坦求和的经验为零,尽管我试图通过一些互联网研究来弥补这一点(会提供链接,但尚不超过两个享有声誉).我还尝试使用einsum在python中进行实验,以查看是否可以更好地处理这些问题.

I have zero experience with einstein summation in a non-coding context, although I have tried to remedy that with some internet research (would provide links but don't have the reputation for more than two yet). I've also tried experimenting in python with einsum to see if I could get a better handle on things.

但是,我仍然不清楚是否可以同时进行以下操作:

And yet I'm still unclear on whether it is both possible and efficient to do as follows:

逐行产生(行i:a在b上)的外积加上(行i: b on a),然后将所有外部乘积矩阵求和以输出一个最终矩阵.

on two arrays of arrays (a and b) of equal length (3) and height (n) , row by row produce the outer product of ( row i: a on b) plus the outer product of (row i: b on a), and then sum all the outer product matrices to output one, final matrix.

我知道'i,j-> ij'会在另一个向量上产生一个向量的外积-这是使我迷失的下一步. (绝对不是'ijk,jik-> ij')

I know that 'i,j->ij' produces the outer product of one vector on another-- it's the next steps that have lost me. ('ijk,jik->ij' is definitely not it)

我的另一个可用选项是遍历数组并从我用cython编写的函数(使用内置的numpy外部函数和sum函数调用)中调用基本函数(双重外部乘积和矩阵加法)选项,这太慢了).我很可能最终也会将循环本身移到cython.

my other available option is to loop through the array and call the basic functions (a double outer product, and a matrix addition) from functions I've written in cython (using the numpy built in outer and sum function is not an option, it is far too slow). It is likely I'd end up moving the loop itself to cython as well.

如此:

  1. 我怎么能概括地表达我上面描述的过程?

  1. how can I express einsum-ically the procedure I described above?

与使用Cython进行所有操作相比,它会带来真正的收益吗?还是我不知道其他替代方法? (包括我使用numpy的效率可能不及可能...)

would it offer real gains over doing everything in cython? or are there other alternatives I'm not aware of? (including the possibility that I've been using numpy less efficiently than I could be...)

谢谢.

编辑示例:

A=np.zeros((3,3))
arrays_1=np.array([[1,0,0],[1,2,3],[0,1,0],[3,2,1]])
arrays_2=np.array([[1,2,3],[0,1,0],[1,0,0],[3,2,1]])
for i in range(len(arrays_1)):
  A=A+(np.outer(arrays_1[i], arrays_2[i])+np.outer(arrays_2[i],arrays_1[i]))

(但是请注意,实际上,我们正在处理长度更大的数组(即,每个内部成员的长度仍为3,但最多为数千个此类成员),的代码(不可避免)被调用了多次)

(note, however, that in practice we're dealing with arrays of much greater length (ie still length 3 for each internal member but up to a few thousand such members), and this section of code gets (unavoidably) called many times)

以防万一,这是用来对两个外部乘积求和的cython:

in case it's at all helpful, here's the cython for the summing two outer products:

def outer_product_sum(np.ndarray[DTYPE_t, ndim=1] a_in, np.ndarray[DTYPE_t, ndim=1] b_in):
    cdef double *a = <double *>a_in.data
    cdef double *b = <double *>b_in.data
    return np.array([
[a[0]*b[0]+a[0]*b[0], a[0]*b[1]+a[1]*b[0], a[0] * b[2]+a[2] * b[0]],
[a[1]*b[0]+a[0]*b[1], a[1]*b[1]+a[1]*b[1], a[1] * b[2]+a[2] * b[1]],
[a[2]*b[0]+a[0]*b[2], a[2]*b[1]+a[1]*b[2], a[2] * b[2]+a[2] * b[2]]])

现在,我从"i在range(len(array)))循环中进行调用,如上所示.

which, right now, I call from within a 'i in range(len(array))' loop as shown above.

推荐答案

爱因斯坦求和只能用于问题的乘法部分(即外部乘积).幸运的是,求和不必按元素进行,但是您可以对约简矩阵进行求和.使用示例中的数组:

Einstein summation can only be used for the multiplicative part of question (i.e. the outer product). Luckily the summation does not have to be performed element-wise, but you can do that on the reduce matrices. Using the arrays from your example:

arrays_1 = np.array([[1,0,0],[1,2,3],[0,1,0],[3,2,1]])
arrays_2 = np.array([[1,2,3],[0,1,0],[1,0,0],[3,2,1]])
A = np.einsum('ki,kj->ij', arrays_1, arrays_2) + np.einsum('ki,kj->ij', arrays_2, arrays_1)

输入数组的形状为(4,3),求和发生在第一个索引(名为'k')上.如果求和应在第二个索引上进行,请将下标字符串更改为'ik,jk->ij'.

The input arrays are of shape (4,3), summation takes place over the first index (named 'k'). If summation should take place over the second index, change the subscripts string to 'ik,jk->ij'.

这篇关于求和einsum中多个向量的外积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆