gpt4 book ai didi

python - 为什么 numpy.dot 会这样?

转载 作者:太空狗 更新时间:2023-10-30 00:27:45 26 4
gpt4 key购买 nike

我试图理解为什么 numpy 的 dot 函数会这样运行:

M = np.ones((9, 9))
V1 = np.ones((9,))
V2 = np.ones((9, 5))
V3 = np.ones((2, 9, 5))
V4 = np.ones((3, 2, 9, 5))

现在 np.dot(M, V1)np.dot(M, V2) 表现为预期的。但是对于 V3V4 结果出乎意料我:

>>> np.dot(M, V3).shape
(9, 2, 5)
>>> np.dot(M, V4).shape
(9, 3, 2, 5)

我预计 (2, 9, 5)(3, 2, 9, 5) 分别。另一方面,np.matmul做我所期望的:矩阵乘法被广播在第二个参数的第一个 N - 2 维和结果具有相同的形状:

>>> np.matmul(M, V3).shape
(2, 9, 5)
>>> np.matmul(M, V4).shape
(3, 2, 9, 5)

所以我的问题是:这样做的理由是什么np.dot 表现如何?它是否用于某些特定目的,还是应用某些一般规则的结果?

最佳答案

来自 the docs for np.dot :

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b:

dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

对于np.dot(M, V3),

(9, 9), (2, 9, 5) --> (9, 2, 5)

For np.dot(M, V4),

(9, 9), (3, 2, 9, 5) --> (9, 3, 2, 5)

The strike-through represents dimensions that are summed over, and are therefore not present in the result.


In contrast, np.matmul treats N-dimensional arrays as 'stacks' of 2D matrices:

The behavior depends on the arguments in the following way.

  • If both arguments are 2-D they are multiplied like conventional matrices.
  • If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.

The same reductions are performed in both cases, but the order of the axes is different. np.matmul essentially does the equivalent of:

for ii in range(V3.shape[0]):
out1[ii, :, :] = np.dot(M[:, :], V3[ii, :, :])

for ii in range(V4.shape[0]):
for jj in range(V4.shape[1]):
out2[ii, jj, :, :] = np.dot(M[:, :], V4[ii, jj, :, :])

关于python - 为什么 numpy.dot 会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33982359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com