0
0
PyTorchml~12 mins

Indexing and slicing in PyTorch - Model Pipeline Trace

Choose your learning style9 modes available
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.

Data Flow - 4 Stages
1Input tensor
1 tensor of shape (4, 5)Create a 2D tensor with 4 rows and 5 columns1 tensor of shape (4, 5)
[[10, 20, 30, 40, 50], [60, 70, 80, 90, 100], [110, 120, 130, 140, 150], [160, 170, 180, 190, 200]]
2Row indexing
1 tensor of shape (4, 5)Select the 2nd row using indexing1 tensor of shape (5,)
[60, 70, 80, 90, 100]
3Column slicing
1 tensor of shape (4, 5)Select columns 1 to 3 (exclusive) for all rows using slicing1 tensor of shape (4, 2)
[[20, 30], [70, 80], [120, 130], [170, 180]]
4Combined indexing and slicing
1 tensor of shape (4, 5)Select the last two columns of the 3rd row1 tensor of shape (2,)
[140, 150]
Training Trace - Epoch by Epoch
Loss
1.0 | *       
0.8 |  *      
0.6 |   *     
0.4 |    *    
0.2 |     *   
0.0 +---------
      1 2 3 4 5
      Epochs
EpochLoss ↓Accuracy ↑Observation
10.850.40Initial loss is high; accuracy is low as model starts learning.
20.650.55Loss decreases and accuracy improves after first update.
30.500.70Model learns important features; metrics improve steadily.
40.380.80Loss continues to drop; accuracy reaches a good level.
50.300.85Training converges with low loss and high accuracy.
Prediction Trace - 4 Layers
Layer 1: Input tensor
Layer 2: Row indexing
Layer 3: Column slicing
Layer 4: Combined indexing and slicing
Model Quiz - 3 Questions
Test your understanding
What is the shape of the tensor after selecting the 2nd row from a (4, 5) tensor?
A(1, 5)
B(4,)
C(5,)
D(4, 5)
Key Insight
Indexing and slicing let us pick specific parts of data tensors easily. This is useful to focus on relevant features or samples before feeding data into a model. Understanding shapes after these operations helps avoid errors and ensures correct data flow.