Skip to content
← Learn PyTorch

Shapes and Strides

PyTorch

Understand tensor shapes, strides and shared storage. Practise view and reshape, and learn when a shape change can reuse memory or needs a copy.

9 exercises

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. Read the stride Practice · Easy
  2. 2 Not solved yet. When reshape needs a copy Practice · Medium
  3. 3 Not solved yet. NCHW to NHWC without copying Practice · Easy
  4. 4 Not solved yet. Remove the channel axis while preserving the batch Practice · Easy
  5. 5 Not solved yet. Scale every row by its own weight Practice · Easy
  6. 6 Not solved yet. Fold the heads into the batch Practice · Easy
  7. 7 Not solved yet. Where a view starts in storage Practice · Medium
  8. 8 Not solved yet. Sliding windows without copying Practice · Medium
  9. 9 Not solved yet. Move one axis, not all of them 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

Why does flattening this transposed tensor fail with view, while reshape succeeds?

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

import torch

x = torch.arange(24).reshape(2, 3, 4)
y = x.transpose(1, 2)
print(x.stride(), y.stride())
try:
    y.view(-1)
except RuntimeError:
    print("RuntimeError")
print(y.reshape(-1).shape)
Question 1 of 4