a = [1, 2, 3]
b = [10, 10, 10]
np.matmul(a, b) 结果是 60。
numpy 如何乘以 (3,) 和 (3,) 维度并返回点积而不是外积(3 * 3)或抛出错误“维度不匹配”?
最佳答案
这直接来自 numpy.matmul()
的文档:
- If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
- If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.
因此,输入的形状
a
和 b
转换为 (1, 3)
和 (3,1)
分别在矩阵乘法过程中。根据矩阵乘法的规则,我们知道:
1 x 3
3 x 1
| |-------- ===> 总结。
因此,我们将结果作为标量。
关于numpy - 理解一维向量上的 np.matmul,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712688/