这是另一种执行方式:
(ab)^ 2 = a ^ 2 + b ^ 2-2ab
与
np.einsum前两个条款
dot-product的第三个-
import numpy as npnp.einsum('ij,ij->i',A,A)[:,None] + np.einsum('ij,ij->i',B,B) - 2*np.dot(A,B.T)
运行时测试
方法-
def loopy_app(A,B): m,n = A.shape[0], B.shape[0] out = np.empty((m,n)) for i,a in enumerate(A): out[i] = np.sum((a - B)**2,1) return outdef broadcasting_app(A,B): return ((A[:,np.newaxis,:] - B)**2).sum(-1)# @Paul Panzer's solndef outer_sum_dot_app(A,B): return np.add.outer((A*A).sum(axis=-1), (B*B).sum(axis=-1)) - 2*np.dot(A,B.T)# @Daniel Forsman's solndef einsum_all_app(A,B): return np.einsum('ijk,ijk->ij', A[:,None,:] - B[None,:,:], A[:,None,:] - B[None,:,:])# Proposed in this postdef outer_einsum_dot_app(A,B): return np.einsum('ij,ij->i',A,A)[:,None] + np.einsum('ij,ij->i',B,B) - 2*np.dot(A,B.T)
时间-
In [51]: A = np.random.randn(1000,100) ...: B = np.random.randn(1000,100) ...:In [52]: %timeit loopy_app(A,B) ...: %timeit broadcasting_app(A,B) ...: %timeit outer_sum_dot_app(A,B) ...: %timeit einsum_all_app(A,B) ...: %timeit outer_einsum_dot_app(A,B) ...: 10 loops, best of 3: 136 ms per loop1 loops, best of 3: 302 ms per loop100 loops, best of 3: 8.51 ms per loop1 loops, best of 3: 341 ms per loop100 loops, best of 3: 8.38 ms per loop
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)