Summary - Tensor operations
- Machine Learning course
- Advanced
- Lesson 18/78
- 6 min
In this group you learned tensor shape, dtype, and reshape operations.
Summary: tensor operations
In this group:
- Element-wise operations, broadcasting,
@; choosing axes withdim; in-place operations with_. - Tracking
shape,unsqueeze/squeeze. - Types: float32 (default), float16/bfloat16 (half), int64 (labels), bool (masks); memory = element size × count.
view/reshape,permute,flatten(1).- Indexing,
argmax,gather, computing accuracy.
Practice Batch processing
Apply a per-row softmax to the (5, 3) logits (torch.softmax(logit, dim=1)) and print the row sums as a list with round(..., 3). Then get each row's most likely class and its probability with max(dim=1); print the classes with tolist().
Starter code
import torch
logit = torch.tensor([[1., 2., 0.], [3., 0., 0.], [0., 0., 2.], [1., 1., 3.], [0., 4., 1.]])
# buraya yazSample solution
import torch
logit = torch.tensor([[1., 2., 0.], [3., 0., 0.], [0., 0., 2.], [1., 1., 3.], [0., 4., 1.]])
p = torch.softmax(logit, dim=1)
print([round(v, 3) for v in p.sum(dim=1).tolist()])
olasilik, sinif = p.max(dim=1)
print(sinif.tolist())Expected output
[1.0, 1.0, 1.0, 1.0, 1.0] [1, 0, 2, 2, 1]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which operation is needed before giving a CNN output (32, 64, 7, 7) to a linear layer?
- unsqueeze(0)
- None
- Flattening to (32, 3136) with flatten(1) Correct answer
- permute
Explanation: A linear layer wants (example, feature); 64 × 7 × 7 = 3136.
2 Flatten all axes except the batch dimension.
d = x.____(1)
d = x.flatten(1)
Explanation: flatten(1) keeps the first axis and merges the rest.
3 Match the functions.
| Term | Meaning |
|---|---|
unsqueeze |
Adds an axis |
permute |
Reorders axes |
flatten |
Flattens |
gather |
Picks by index |