Skip to content
← Learn PyTorch

Broadcasting

PyTorch

Predict which tensor shapes can broadcast and what shape the result will have. Add dimensions deliberately and catch unintended pairwise operations.

6 exercises

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. A stride of zero Practice · Easy
  2. 2 Not solved yet. Compute pairwise Euclidean distances Practice · Medium
  3. 3 Not solved yet. Add row sums without changing the destination shape Practice · Medium
  4. 4 Not solved yet. What shape comes out Practice · Easy
  5. 5 Not solved yet. Every pair of vector entries Practice · Easy
  6. 6 Not solved yet. One bias, every position 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

The addition produces shape (3, 4). Can a.add_(b) store that result in the original a?

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

import torch

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