當前位置: 首頁>>技術問答>>正文


numpy dot()和Python 3.5+矩陣乘法@的區別

我最近遷移到了Python 3.5,注意到new matrix multiplication operator(新的矩陣乘法雲算符) (@)的行為與numpy dot操作符有所不同。例如,對於3D數組:

import numpy as np

a = np.random.rand(8,13,13)
b = np.random.rand(8,13,13)
c = a @ b  # Python 3.5+
d = np.dot(a, b)

@運算符返回一個形狀數組:

c.shape
(8, 13, 13)

np.dot()函數返回:

d.shape
(8, 13, 8, 13)

我怎樣才能重現與numpy點乘相同的結果?還有其他的重大差異嗎?

numpy python

最佳解決方法

@運算符調用數組的__matmul__方法,而不是dot。該方法也作為函數np.matmul在API中提供。

>>> a = np.random.rand(8,13,13)
>>> b = np.random.rand(8,13,13)
>>> np.matmul(a, b).shape
(8, 13, 13)

從文檔:

matmul differs from dot in two important ways.

  • Multiplication by scalars is not allowed.
  • Stacks of matrices are broadcast together as if the matrices were elements.

最後一點清楚地表明dotmatmul方法在傳遞3D(或更高維)數組時行為不同。更多的引用文檔:

對於matmul

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.

對於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

Matrix-Multiplication

參考資料

本文由《純淨天空》出品。文章地址: https://vimsky.com/zh-tw/article/3737.html,未經允許,請勿轉載。