Indexing
- Machine Learning course
- Advanced
- Lesson 17/78
- 5 min
Tensor elements are accessed by index.
Indexing
Indexing and slicing work as in NumPy; in addition there are a few functions often used in batch work:
x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x[1].tolist()) # [4, 5, 6]
print(x[:, -1].tolist()) # [3, 6, 9]
print(x[x > 4].tolist()) # [5, 6, 7, 8, 9] boolean mask
print(x[[0, 2]].tolist()) # [[1, 2, 3], [7, 8, 9]]In classification the predicted class is taken with cikti.argmax(dim=1) and the top k probabilities with torch.topk.
gather and the correct class's score
Taking the correct class's probability for every example is common in loss computations. gather picks the given column index from each row; fancy indexing does the same:
p = torch.tensor([[0.7, 0.2, 0.1], [0.1, 0.1, 0.8]])
y = torch.tensor([0, 2])
print(p.gather(1, y.unsqueeze(1)).squeeze(1).tolist()) # [0.7..., 0.8...]
print(p[torch.arange(2), y].tolist()) # the sameUsing round when printing floating-point values makes the output readable.
Practice Computing accuracy
Get the predicted classes from the given logits with argmax(dim=1) and print them with tolist(). Print the accuracy with (tahmin == y).float().mean().item(), then the correct classes' logits with logit[torch.arange(len(y)), y].tolist().
Starter code
import torch
logit = torch.tensor([[2., 1., 0.], [0., 3., 1.], [1., 0., 4.], [5., 1., 2.]])
y = torch.tensor([0, 1, 1, 0])
# buraya yazSample solution
import torch
logit = torch.tensor([[2., 1., 0.], [0., 3., 1.], [1., 0., 4.], [5., 1., 2.]])
y = torch.tensor([0, 1, 1, 0])
tahmin = logit.argmax(dim=1)
print(tahmin.tolist())
print((tahmin == y).float().mean().item())
print(logit[torch.arange(len(y)), y].tolist())Expected output
[0, 1, 2, 0] 0.75 [2.0, 3.0, 0.0, 5.0]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 For <code>x</code> above, what does <code>x.argmax(dim=1).tolist()</code> give?
- [0, 1, 2]
- [2, 5, 8]
- 8
- [2, 2, 2] Correct answer
Explanation: In every row the largest value is in the last column (index 2).
2 Get the index of each row's largest value.
tahmin = cikti.____(dim=1)
tahmin = cikti.argmax(dim=1)
Explanation: argmax(dim=1) gives the class prediction.
3 Match the expressions.
| Term | Meaning |
|---|---|
x[:, 0] |
First column |
x[x > 0] |
Selection by mask |
argmax(dim=1) |
Max per row |
gather |
Indexed pick per row |