Dense linear algebra
Apply a linear transformation represented by a weight matrix to a 2-D input matrix.
PyTorch · Function
torch.mm multiplies two 2-D tensors as matrices. Compatible shapes (n, m) and (m, p) produce a (n, p) tensor, without broadcasting.
torch.mm computes the matrix product of two 2-D tensors. If the first has shape (n, m) and the second (m, p), the result has shape (n, p); it does not add batch dimensions or broadcast inputs.
torch.mm(input, mat2, *, out=None) → Tensor| Name | Type | Description |
|---|---|---|
input | Tensor | First matrix to be multiplied. |
mat2 | Tensor | Second matrix to be multiplied. |
out | Tensor, keyword-only, optional | Optional destination tensor for the result. |
A Tensor containing the matrix product; for input shape (n, m) and mat2 shape (m, p), its shape is (n, p).
import torch
left = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
right = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
product = torch.mm(left, right)
print(product)
Multiplies a 2×3 matrix by a 3×2 matrix, producing a 2×2 tensor.
Apply a linear transformation represented by a weight matrix to a 2-D input matrix.
Multiply compatible sparse and dense 2-D tensors where the documented sparse layouts are supported.
Compute a fixed-size matrix product without batch broadcasting semantics.
torch.mm accepts two matrices and returns their matrix product. For shapes (n, m) and (m, p), the output shape is (n, p).
Follow-up: How would you handle batched matrix multiplication? Use torch.matmul, which is the documented alternative when broadcasting matrix products are needed.
torch.mm does not broadcast its inputs. Both arguments are 2-D matrices whose inner dimensions must match.
Follow-up: What is the distinction from torch.matmul? torch.matmul supports broadcasting matrix products, while torch.mm is specifically the non-broadcasting matrix operation.
The optional out argument supplies the destination tensor. When it is supplied, its layout is used; otherwise, the result layout is inferred from the first input.
Follow-up: Why might omitting out be preferable? It lets PyTorch infer the result layout instead of imposing a possibly unintended destination layout.
torch.matmultorch.bmmtorch.addmmtorch.sparse.mm