mindspore.ops.matmul

mindspore.ops.matmul(x1, x2, dtype=None)[source]

Returns the matrix product of two arrays.

Note

Numpy arguments out, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16 and np.float32. On CPU, the supported dtypes are np.float16 and np.float32.

Parameters
  • x1 (Tensor) – Input tensor, scalar not allowed. The last dimension of x1 must be the same size as the second last dimension of x2. And the shape of x1 and x2 could be broadcast.

  • x2 (Tensor) – Input tensor, scalar not allowed. The last dimension of x1 must be the same size as the second last dimension of x2. And the shape of x1 and x2 could be broadcast.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

Returns

Tensor or scalar, the matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors.

Raises
  • ValueError – If the last dimension of x1 is not the same size as the second-to-last dimension of x2, or if a scalar value is passed in.

  • ValueError – If the shape of x1 and x2 could not broadcast together。

Supported Platforms:

Ascend GPU CPU

Examples

>>> # case 1 : Reasonable application of broadcast mechanism
>>> x1 = Tensor(np.arange(2*3*4).reshape(2, 3, 4), mindspore.float32)
>>> x2 = Tensor(np.arange(4*5).reshape(4, 5), mindspore.float32)
>>> output = ops.matmul(x1, x2)
>>> 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 `x1` is 1
>>> x1 = Tensor(np.ones([1, 2]), mindspore.float32)
>>> x2 = Tensor(np.ones([2,]), mindspore.float32)
>>> output = ops.matmul(x1, x2)
>>> print(output)
[2.]
>>> print(output.shape)
(1,)