简体   繁体   English

是numpy.einsum的更快替代方法,用于获取两个向量列表的“元素方式”的点积?

[英]Faster alternative to numpy.einsum for taking the “element-wise” dot product of two lists of vectors?

Let's say you're given two arrays of vectors: 假设您获得了两个向量数组:

v1 = np.array([ [1, 2], [3, 4] ]) v2 = np.array([ [10, 20], [30, 40]]) v1 = np.array([ [1, 2], [3, 4] ]) v2 = np.array([ [10, 20], [30, 40]])

We would like to generate an array that is equivalent to: 我们想生成一个数组,它等效于:

v3 = np.array([ np.dot(v1[0], v2[0]), np.dot(v1[1], v2[1]) ])

Currently I use: 目前,我使用:

v3 = np.einsum('ij,ij->i', v1, v2)

However, I do this a lot in my code, so speed ups here would be very helpful for me. 但是,我在代码中做了很多事情 ,因此此处的加速对我非常有帮助。

How could we speed it up? 我们如何加快速度? np.einsum is already quite efficient, but I wonder if for this particular use-case , there is a faster way? np.einsum已经非常有效,但是我想知道对于这个特定用例 ,是否有更快的方法?

einsum does the best of 3 options that I can think of: einsum尽我所能想到的3种选择中的最佳选择:

In [73]: timeit v3=np.einsum('ij,ij->i',v1,v2)
100000 loops, best of 3: 5.14 us per loop

In [74]: timeit np.diag(np.dot(v1,v2.T))
100000 loops, best of 3: 7.43 us per loop

In [75]: timeit np.sum(v1*v2,axis=1)
100000 loops, best of 3: 16.8 us per loop

Several questions to ask: 要问的几个问题:

  • is this calculation really that expensive ? 这个计算真的那么昂贵吗?
  • if it is relatively expensive do you have to doing so often ? 如果比较昂贵,您是否必须经常这样做?
  • can you consolidate the einsum calls - concatenate arrays ? 您可以合并einsum调用-并置数组吗?

Try inner1d 尝试inner1d

import numpy as np
import cProfile
from numpy.core.umath_tests import inner1d

v1 = np.random.random((10**7,2,))  # 10 million random vectors
v2 = np.random.random((10**7,2,))  # 10 million random vectors
v3 = np.einsum('ij,ij->i', v1, v2) # einsum
v4 = inner1d(v1,v2)                # inner1d (~2x faster than einsum)

cProfile.run("np.einsum('ij,ij->i', v1, v2)") # cProfile: 3 function calls in 0.065 seconds
cProfile.run("inner1d(v1,v2)") # cProfile: 2 function calls in 0.033 seconds

print np.allclose(v3,v4) # Returns True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM