脾气暴躁:点乘积以最大值代替总和 [英] Numpy: Dot product with max instead of sum
问题描述
numpy中是否有一种方法可以执行以下操作(或者是否有通用的数学术语):
Is there a way in numpy to do the following (or is there a general mathematical term for this):
假定普通点积:
M3[i,k] = sum_j(M1[i,j] * M2[j,k])
现在,我想用其他运算总和来代替总和,即最大值:
Now I would like to replace the sum by sum other operation, say the maximum:
M3[i,k] = max_j(M1[i,j] * M2[j,k])
如您所见,它与上面的内容完全平行,只是我们对所有j
取了max
而不是总和.
As you can see it is completely parallel to the above, just we take max
over all j
and not the sum.
其他选项可能是min
,prod
,以及将序列/集合转换为值的任何其他操作.
Other options could be min
, prod
, and whatever other operation that turns a sequence/set into a value.
推荐答案
正常的点积应为(使用numpy广播)
Normal dot product would be (using numpy broadcasting)
M3 = np.sum(M1[:, :, None] * M2[None, :, :], axis = 1)
您可以对具有axis
关键字的任何功能执行相同的操作.
You can do the same thing with any function you want that has an axis
keyword.
M3 = np.max(M1[:, :, None] * M2[None, :, :], axis = 1)
这篇关于脾气暴躁:点乘积以最大值代替总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!