mm

paddle.mm ( input, mat2, out=None, name=None ) [源代码]

用于两个输入矩阵的相乘。

两个输入的形状可为任意维度,但当任一输入维度大于3时,两个输入的维度必须相等。

如果原始 Tensor input 或 mat2 的秩为 1 且未转置,则矩阵相乘后的前置或附加维度 1 将移除。

参数:

  • input (Tensor) : 输入变量,类型为 Tensor 或 LoDTensor。

  • mat2 (Tensor) : 输入变量,类型为 Tensor 或 LoDTensor。

  • out (Tensor, 可选) – 指定存储运算结果的Tensor。如果设置为None或者不设置,将创建新的Tensor存储运算结果,默认值为None。

  • name (str,可选)- 具体用法请参见 Name ,一般无需设置,默认值为None。

返回:

  • Tensor,矩阵相乘后的结果。
  1. * 1:
  2. input: [B, ..., M, K], mat2: [B, ..., K, N]
  3. out: [B, ..., M, N]
  4. * 2:
  5. input: [B, M, K], mat2: [B, K, N]
  6. out: [B, M, N]
  7. * 3:
  8. input: [B, M, K], mat2: [K, N]
  9. out: [B, M, N]
  10. * 4:
  11. input: [M, K], mat2: [K, N]
  12. out: [M, N]
  13. * 5:
  14. input: [B, M, K], mat2: [K]
  15. out: [B, M]
  16. * 6:
  17. input: [K], mat2: [K]
  18. out: [1]
  19. * 7:
  20. input: [M], mat2: [N]
  21. out: [M, N]

代码示例

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