Compute the matrix multiplication.

\[\text{out} = \text{input} \times \text{other} \]

The behavior depends on the shape of input tensors:

  • If both tensors are 1d, computes the vector product.
  • If tensors are 1d and >=2d, computes the vector-matrix multiplication.
  • If tensors are >=2d and 1d, computes the matrix-vector multiplication.
  • If both tensors are >= 2d, computes the matrix-matrix multiplication.
  • If one tensor is >= 3d, applies batching and broadcasting to the computation.


# Vector x Vector
a = torch.ones(2)
b = torch.ones(2)
print(torch.matmul(a, b))
# Vector x Matrix
a = torch.ones(2)
b = torch.ones(2, 3)
print(torch.matmul(a, b))
# Matrix x Vector
a = torch.ones(3, 2)
b = torch.ones(2)
print(torch.matmul(a, b))
# Matrix x Matrix
a = torch.ones(2, 3)
b = torch.ones(3, 2)
print(torch.matmul(a, b))

dragon.Tensor – The output tensor.