简体   繁体   English

高维数组乘法

[英]High-dimensional array multiplication

Consider the following code.考虑以下代码。

import numpy as np
array1 = np.random.random((3,3,3))
array2 = np.random.random((3,3,3))
array3 = array1@array2

What does array3 contain? array3包含什么? I know it also has shape (3,3,3) .我知道它也有形状(3,3,3) If array1 and array2 were two-dimensional, then array3 would be the matrix multiplication of the arrays. Has the @ operation a mathematical meaning?如果array1array2是二维的,那么array3就是arrays的矩阵乘法。 @操作有数学意义吗?

This is explained in PEP 465 :这在PEP 465中有解释:

For inputs with more than 2 dimensions, we treat the last two dimensions as being the dimensions of the matrices to multiply, and 'broadcast' across the other dimensions.对于超过 2 维的输入,我们将最后两个维度视为要相乘的矩阵维度,并“广播”到其他维度。 This provides a convenient way to quickly compute many matrix products in a single operation.这提供了一种在单个操作中快速计算许多矩阵乘积的便捷方法。 For example, arr(10, 2, 3) @ arr(10, 3, 4) performs 10 separate matrix multiplies, each of which multiplies a 2x3 and a 3x4 matrix to produce a 2x4 matrix, and then returns the 10 resulting matrices together in an array with shape (10, 2, 4).例如, arr(10, 2, 3) @ arr(10, 3, 4)执行 10 个单独的矩阵乘法,每个乘法将 2x3 和 3x4 矩阵相乘产生 2x4 矩阵,然后将 10 个结果矩阵一起返回在形状为 (10, 2, 4) 的数组中。

So for your code array3[0, :, :] contains the result of the matrix-matrix multiplication array1[0, :, :] @ array2[0, :, :] , and so on.因此,对于您的代码array3[0, :, :]包含矩阵矩阵乘法array1[0, :, :] @ array2[0, :, :]等的结果。

In numpy @ does matrix multiplication在numpy @矩阵乘法

While * does element wise multiplication or Hadamard product*进行元素明智的乘法或Hadamard 乘积

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM