mm

paddle. mm ( input: Tensor, mat2: Tensor, name: str | None = None, *, out_dtype: DTypeLike | None = None, out: Tensor | None = None ) → Tensor [source]

Applies matrix multiplication to two tensors.

Currently, the input tensors’ rank can be any, but when the rank of any inputs is bigger than 3, this two inputs’ rank should be equal.

Also note that if the raw tensor \(x\) or \(mat2\) is rank-1 and nontransposed, the prepended or appended dimension \(1\) will be removed after matrix multiplication.

Parameters
  • input (Tensor) – The input tensor which is a Tensor. Support data types: bfloat16, float16, float32, float64, int8, int32, int64, complex64, complex128.

  • mat2 (Tensor) – The input tensor which is a Tensor. Support data types: bfloat16, float16, float32, float64, int8, int32, int64, complex64, complex128.

  • name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.

Keywords Argument:

out_dtype (paddle.dtype|None, optional): The desired output data type. Currently only supports paddle.float32 for CUDA bfloat16 2-D inputs in dynamic graph. Default: None. out (Tensor, optional): The output Tensor. It must have the same data type and shape as the expected output. Default is None, and a new Tensor will be created to store the result.

Returns

The product Tensor. Its data type is the same as input unless out_dtype is specified.

Return type

Tensor

* example 1:

input: [B, ..., M, K], mat2: [B, ..., K, N]
out: [B, ..., M, N]

* example 2:

input: [B, M, K], mat2: [B, K, N]
out: [B, M, N]

* example 3:

input: [B, M, K], mat2: [K, N]
out: [B, M, N]

* example 4:

input: [M, K], mat2: [K, N]
out: [M, N]

* example 5:

input: [B, M, K], mat2: [K]
out: [B, M]

* example 6:

input: [K], mat2: [K]
out: [1]

Examples

>>> import paddle
>>> input = paddle.arange(1, 7).reshape((3, 2)).astype('float32')
>>> mat2 = paddle.arange(1, 9).reshape((2, 4)).astype('float32')
>>> out = paddle.mm(input, mat2)
>>> out
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[11., 14., 17., 20.],
 [23., 30., 37., 44.],
 [35., 46., 57., 68.]])