点积 numpy 中的矩阵列表

Dot-product a list of Matrices in numpy

让我们生成一个 'list of three 2x2 matrices',我称之为 M1、M2 和 M3:

import numpy as np
arr = np.arange(4*2*2).reshape((3, 2, 2))

我想对所有这些矩阵进行点积:

 A = M1 @ M2 @ M3

最简单、最快的方法是什么?我基本上是在寻找类似于“.sum(axis=0)”的东西,但用于矩阵乘法。

您可能正在寻找 np.linalg.multi_dot:

arr = np.arange(3*2*2).reshape((-1, 2, 2))
np.linalg.multi_dot(arr)

会给你 arr[0]arr[1]arr[2] 之间的点积。正如 arr[0] @ arr[1] @ arr[2].