Skip to content
← Learn PyTorch

Indexing and Selection

PyTorch

Select tensor elements with slices, index arrays and masks. Distinguish views from copies and understand how indexing affects shape and shared storage.

9 exercises

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. Update selected rows, including repeated indices Practice · Medium
  2. 2 Not solved yet. Pick columns for each row Practice · Medium
  3. 3 Not solved yet. When two writes land on one slot Practice · Medium
  4. 4 Not solved yet. Zero the padding, keep the tensor Practice · Easy
  5. 5 Not solved yet. Mask the padding before the softmax Practice · Easy
  6. 6 Not solved yet. The value at the argmax Practice · Medium
  7. 7 Not solved yet. Rows 2 through 5, cheaply Practice · Easy
  8. 8 Not solved yet. Everything above the threshold Practice · Easy
  9. 9 Not solved yet. Labels to one-hot Practice · Medium

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

For this two-dimensional tensor, what do the two uses of -1 select?

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

import torch

x = torch.arange(6).reshape(2, 3)
print(x[-1].tolist())
print(x.sum(dim=-1).tolist())
Question 1 of 4