0
0
PyTorchml~8 mins

Indexing and slicing in PyTorch - Model Metrics & Evaluation

Choose your learning style9 modes available
Metrics & Evaluation - Indexing and slicing
Which metric matters for Indexing and slicing and WHY

Indexing and slicing are ways to pick parts of data. The main goal is to get the right pieces without mistakes. So, the key metric is data correctness. This means the selected data matches what you want exactly. If you pick wrong parts, your model learns wrong things.

In PyTorch, indexing and slicing help prepare data batches or select features. If the slices are wrong, the model input is wrong, causing bad training results. So, checking the output shape and values after slicing is very important.

Confusion matrix or equivalent visualization
    Example: Selecting a batch of images from a dataset tensor

    Dataset shape: (100, 3, 32, 32)  # 100 images, 3 color channels, 32x32 pixels

    Indexing: batch = dataset[10:20]  # Select images 10 to 19

    Result shape: (10, 3, 32, 32)

    Check:
    - Correct number of images (10)
    - Correct dimensions (channels, height, width)

    If shape is wrong, indexing failed.
    
Precision vs Recall tradeoff with concrete examples

For indexing and slicing, the tradeoff is between selecting enough data (recall) and selecting only the right data (precision).

  • High recall, low precision: You select many data points, but some are wrong. This wastes time and may confuse the model.
  • High precision, low recall: You select only very sure data points, but miss some useful ones. This can limit learning.

Example: If you slice images with dataset[10:20], you get exactly 10 images (high precision and recall). But if you slice with dataset[10:25] but your dataset has only 20 images, you get an error or wrong data (low precision).

What "good" vs "bad" metric values look like for this use case

Good:

  • Output tensor shape matches expected slice size.
  • Data values correspond to the intended slice (e.g., correct images or features).
  • No errors or warnings during slicing.

Bad:

  • Output shape is smaller or larger than expected.
  • Data contains unexpected values or is empty.
  • Index out of range errors or silent wrong slices.
Metrics pitfalls
  • Off-by-one errors: Slicing with wrong start or end index misses or adds extra data.
  • Negative indexing confusion: Negative indices count from the end, which can cause unexpected slices.
  • Shape mismatch: Forgetting that slicing reduces dimensions can cause errors in model input.
  • Data leakage: Accidentally including test data in training slices.
  • Silent errors: Slicing beyond tensor size may not always raise errors but return smaller tensors.
Self-check question

Your PyTorch tensor has shape (50, 10). You slice it with tensor[10:20, 5:15]. The result shape is (10, 5). Is this correct? Why or why not?

Answer: No, it is not correct. The first slice selects rows 10 to 19 (10 rows), which is correct. But the second slice selects columns 5 to 14 (10 columns), but the result has only 5 columns. This means the tensor has fewer than 15 columns, so slicing 5:15 returns fewer columns than expected. You should check the original tensor shape to avoid this mistake.

Key Result
Correct indexing and slicing ensure the selected data matches expected shapes and values, preventing errors and improving model input quality.