Skip to content
← Learn PyTorch

Reductions and dim

PyTorch

Reduce tensors along selected dimensions and use keepdim to preserve axes. Handle dtypes, numerical accuracy and ties when choosing reduction operations.

7 exercises

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. Find the predicted class for each row Practice · Easy
  2. 2 Not solved yet. Accuracy from a comparison Practice · Easy
  3. 3 Not solved yet. Softmax from the definition Practice · Medium
  4. 4 Not solved yet. Compute row means while ignoring NaNs Practice · Easy
  5. 5 Not solved yet. Per-channel statistics Practice · Medium
  6. 6 Not solved yet. Sum float32 values using float64 accumulation Practice · Medium
  7. 7 Not solved yet. Running totals 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

Which shapes do these two reductions produce?

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

import torch

x = torch.arange(12).reshape(3, 4)
print(x.sum(dim=0).shape)
print(x.sum(dim=0, keepdim=True).shape)
Question 1 of 4