Skip to content
← Learn PyTorch

Matmul and einsum

PyTorch

Express matrix products, batch dimensions and tensor contractions with matmul and einsum. Check dimension labels and broadcasting before computing a result.

6 exercises

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. One weight matrix, every item Practice · Easy
  2. 2 Not solved yet. Queries against keys Practice · Medium
  3. 3 Not solved yet. Subscripts instead of transposes Practice · Medium
  4. 4 Not solved yet. Elementwise or matrix multiplication? Practice · Easy
  5. 5 Not solved yet. Choose the order of matrix multiplications Practice · Medium
  6. 6 Not solved yet. One matrix, many batches Practice · Easy

Check yourself

4 questions · not saved while you're signed out

Use these questions to check your understanding and compare explanations. Your score does not affect track completion.

0 / 4 correct

What happens in these two operations on matrices with shapes (2, 3) and (3, 4)?

Choose one answer to reveal the explanations. Answers are kept only while this page is open.

import torch

a = torch.ones(2, 3)
b = torch.ones(3, 4)
print((a @ b).shape)
try:
    a * b
except RuntimeError:
    print("RuntimeError")
Question 1 of 4