PyTorch torch.matmul() Method

PyTorch torch.matmul() method is “used to calculate the matrix product of two tensors.”

Syntax

torch.matmul(inputother*out=None)

Parameters

  1. input: It is the first input tensor.
  2. other: It is the second input tensor.
  3. out: It is the output tensor (optional).

Some Important Notes

  1. For 2-D tensors: It performs the matrix multiplication just as expected: out = input × other
  2. For 1-D tensors: It performs the inner (dot) product of vectors: out = input ⋅ other
  3. For N-D tensors: If either input or other is N-D (N > 2), it is treated as a stack of matrices residing in the last two dimensions, and batch matrix multiplication is performed.
  4. Broadcasting: If input dimensions and others don’t match exactly, torch.matmul() will try to broadcast them.

Example 1: Simple 2D Matrix Multiplication

import torch

a = torch.tensor([[1, 2],
                  [3, 4]])
b = torch.tensor([[5, 6],
                  [7, 8]])

result = torch.matmul(a, b)

print(result)

Output

tensor([[19, 22],
        [43, 50]])

Example 2: Dot Product of 1D Tensors

import torch

a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])

result = torch.matmul(a, b)

print(result)

Output

tensor(32)

Example 3: Batch Matrix Multiplication

import torch

a = torch.randn(3, 2, 4) # Batch of 3 matrices
b = torch.randn(3, 4, 3) # Batch of 3 matrices

result = torch.matmul(a, b)

print(result)

Output

torch.matmul()

That’s it!

Related posts

torch.split()

torch.unsqueeze()

torch.nn.Conv2d()

torch.clone()

torch.squeeze()

torch.sum()

torch.full()

Leave a Comment