mm¶
- paddle. mm ( input, mat2, name=None ) [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.
mat2 (Tensor) – The input tensor which is a Tensor.
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
- Returns
-
The product Tensor.
- 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.]])