Apart from the method @hpaulj outlines in the comments, you can also use the fact that what you are calculating is essentially a pair-wise Minkowski distance:import numpy as npfrom scipy.spatial.distance import cdistk,m,n = 10,20,30A = np.random.random((k,m))B = np.random.random((m,n))method1 = ((A[...,None]+B)**20).sum(axis=1)method2 = cdist(A,-B.T,'m',p=20)**20np.allclose(method1,method2)# True 这篇关于如何在numpy或pytorch中向量化自定义算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-17 01:01