mindspore.ops.matmul

mindspore.ops.matmul(input, other)[源代码]

计算两个数组的乘积。

说明

不支持NumPy参数 outcastingordersuboksignatureextobj 。在GPU上支持的数据类型为np.float16和np.float32。在CPU上支持的数据类型为np.float16和np.float32。

参数:
  • input (Tensor) - 输入Tensor,不支持Scalar, input 的最后一维度和 other 的倒数第二维度相等,且 inputother 彼此支持广播。

  • other (Tensor) - 输入Tensor,不支持Scalar, input 的最后一维度和 other 的倒数第二维度相等,且 inputother 彼此支持广播。

返回:

Tensor或Scalar,输入的矩阵乘积。仅当 inputother 为一维向量时,输出为Scalar。

异常:
  • TypeError - input 的dtype和 other 的dtype不一致。

  • ValueError - input 的最后一维度和 other 的倒数第二维度不相等,或者输入的是Scalar。

  • ValueError - inputother 彼此不能广播。

支持平台:

Ascend GPU CPU

样例:

>>> # case 1 : Reasonable application of broadcast mechanism
>>> input = Tensor(np.arange(2*3*4).reshape(2, 3, 4), mindspore.float32)
>>> other = Tensor(np.arange(4*5).reshape(4, 5), mindspore.float32)
>>> output = ops.matmul(input, other)
>>> print(output)
[[[  70.   76.   82.   88.   94.]
[ 190.  212.  234.  256.  278.]
[ 310.  348.  386.  424.  462.]]
[[ 430.  484.  538.  592.  646.]
[ 550.  620.  690.  760.  830.]
[ 670.  756.  842.  928. 1014.]]]
>>> print(output.shape)
(2, 3, 5)
>>> # case 2 : the rank of `input` is 1
>>> input = Tensor(np.ones([1, 2]), mindspore.float32)
>>> other = Tensor(np.ones([2,]), mindspore.float32)
>>> output = ops.matmul(input, other)
>>> print(output)
[2.]
>>> print(output.shape)
(1,)