Model Pipeline - Indexing and slicing
This pipeline shows how data tensors are accessed and manipulated using indexing and slicing in PyTorch. It helps select parts of data for further processing or model input.
This pipeline shows how data tensors are accessed and manipulated using indexing and slicing in PyTorch. It helps select parts of data for further processing or model input.
Loss
1.0 | *
0.8 | *
0.6 | *
0.4 | *
0.2 | *
0.0 +---------
1 2 3 4 5
Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.85 | 0.40 | Initial loss is high; accuracy is low as model starts learning. |
| 2 | 0.65 | 0.55 | Loss decreases and accuracy improves after first update. |
| 3 | 0.50 | 0.70 | Model learns important features; metrics improve steadily. |
| 4 | 0.38 | 0.80 | Loss continues to drop; accuracy reaches a good level. |
| 5 | 0.30 | 0.85 | Training converges with low loss and high accuracy. |
tensor[2] do in PyTorch when tensor is a 1D tensor?[2] picks the element at position 2 (0-based index).tensor[2] returns the third element.t to get elements from index 1 to 4 (excluding 4)?start:stop inside square brackets to select elements from start index up to but not including stop index.t[1:4] uses correct slicing syntax t[1:4]. Options A, B, and C use invalid syntax for slicing.t = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), what is the output of t[1:, :2]?t[1:, :2]1: means rows from index 1 to end (rows 1 and 2). :2 means columns from start to index 2 (columns 0 and 1).import torch t = torch.tensor([1, 2, 3, 4, 5]) print(t[::2, 1])
t is 1D with shape (5,). It has only one dimension.t[::2, 1] which tries to index two dimensions (rows and columns). This causes an error because t is 1D.t with shape (4, 5, 6). Which indexing expression extracts all elements from the second dimension at index 3, across all batches and features?: to select all in first and third dims, and 3 to select index 3 in second dim, so t[:, 3, :].Pass the test and this pattern moves unknown