6

我有向量a
我想计算np.inner(a, a)
但我想知道是否有更漂亮的方法来计算它。

[这种方式的缺点是,如果我想计算它a-b或更复杂的表达式,我必须多写一行。c = a - bnp.inner(c, c)不是somewhat(a - b)]

4

4 回答 4

7

老实说,可能不会有比np.inneror更快的东西np.dot。如果你发现中间变量很烦人,你总是可以创建一个 lambda 函数:

sqeuclidean = lambda x: np.inner(x, x)

np.innernp.dot利用 BLAS 例程,并且几乎肯定会比标准的元素乘法然后求和更快。

In [1]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)
((a - b) ** 2).sum()
   ....: 
The slowest run took 36.13 times longer than the fastest. This could mean that an intermediate result is being cached 
1 loops, best of 100: 6.45 ms per loop

In [2]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)                                                                                                                                                                              
np.linalg.norm(a - b, ord=2) ** 2
   ....: 
1 loops, best of 100: 2.74 ms per loop

In [3]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)
sqeuclidean(a - b)
   ....: 
1 loops, best of 100: 2.64 ms per loop

np.linalg.norm(..., ord=2)在内部使用np.dot,并提供与直接使用非常相似的性能np.inner

于 2016-02-04T23:54:20.663 回答
6

计算 norm2

numpy.linalg.norm(x, ord=2) 

numpy.linalg.norm(x, ord=2)**2正方形

于 2016-02-04T23:25:45.937 回答
5

我不知道性能是否好,但(a**2).sum()计算出正确的值并有你想要的非重复参数。您可以a用一些复杂的表达式替换而不将其绑定到变量,只需记住在必要时使用括号,因为**绑定比大多数其他运算符更紧密:((a-b)**2).sum()

于 2016-02-04T23:36:24.460 回答
0

如果您最终在这里寻找一种获得平方范数的快速方法,这些测试显示distances = np.sum((descriptors - desc[None])**2, axis=1)是最快的。

import timeit

setup_code = """
import numpy as np
descriptors = np.random.rand(3000, 512)
desc = np.random.rand(512)
"""

norm_code = """
np.linalg.norm(descriptors - desc[None], axis=-1)
"""
norm_time = timeit.timeit(stmt=norm_code, setup=setup_code, number=100, )

einsum_code = """
x = descriptors - desc[None]
sqrd_dist = np.einsum('ij,ij -> i', x, x)
"""
einsum_time = timeit.timeit(stmt=einsum_code, setup=setup_code, number=100, )

norm_sqrd_code = """
distances = np.sum((descriptors - desc[None])**2, axis=1)
"""
norm_sqrd_time = timeit.timeit(stmt=norm_sqrd_code, setup=setup_code, number=100, )

print(norm_time) # 0.7688689678907394
print(einsum_time) # 0.29194538854062557
print(norm_sqrd_time) # 0.274090813472867

于 2020-06-04T09:58:33.753 回答