Bird
Raised Fist0
PyTorchml~8 mins

Indexing and slicing in PyTorch - Model Metrics & Evaluation

Choose your learning style10 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.

Practice

(1/5)
1. What does the expression tensor[2] do in PyTorch when tensor is a 1D tensor?
easy
A. Selects every second element starting at index 2
B. Selects elements from index 0 to 2
C. Selects the element at index 2
D. Raises an error because indexing starts at 1

Solution

  1. Step 1: Understand 1D tensor indexing

    In PyTorch, indexing a 1D tensor with [2] picks the element at position 2 (0-based index).
  2. Step 2: Confirm indexing behavior

    Since indexing starts at 0, tensor[2] returns the third element.
  3. Final Answer:

    Selects the element at index 2 -> Option C
  4. Quick Check:

    Indexing with a single integer picks one element [OK]
Hint: Single index picks one element by position [OK]
Common Mistakes:
  • Thinking indexing starts at 1
  • Confusing slicing with indexing
  • Expecting a range instead of one element
2. Which of the following is the correct syntax to slice a 1D tensor t to get elements from index 1 to 4 (excluding 4)?
easy
A. t(1:4)
B. t[1:4]
C. t[1-4]
D. t[1,4]

Solution

  1. Step 1: Recall slicing syntax

    Slicing uses start:stop inside square brackets to select elements from start index up to but not including stop index.
  2. Step 2: Check each option

    t[1:4] uses correct slicing syntax t[1:4]. Options A, B, and C use invalid syntax for slicing.
  3. Final Answer:

    t[1:4] -> Option B
  4. Quick Check:

    Slicing uses colon inside brackets [OK]
Hint: Use colon inside brackets for slicing [OK]
Common Mistakes:
  • Using comma instead of colon for slicing
  • Using parentheses instead of brackets
  • Using minus sign instead of colon
3. Given the tensor t = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), what is the output of t[1:, :2]?
medium
A. [[5, 6], [8, 9]]
B. [[2, 3], [5, 6]]
C. [[1, 2], [4, 5]]
D. [[4, 5], [7, 8]]

Solution

  1. Step 1: Understand slicing 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).
  2. Step 2: Extract the sliced elements

    Rows 1 and 2 are [[4,5,6],[7,8,9]]. Taking columns 0 and 1 gives [[4,5],[7,8]].
  3. Final Answer:

    [[4, 5], [7, 8]] -> Option D
  4. Quick Check:

    Row slice 1: and col slice :2 gives [[4,5],[7,8]] [OK]
Hint: Rows after 1, columns before 2 slice tensor [OK]
Common Mistakes:
  • Mixing row and column indices
  • Including column index 2 mistakenly
  • Confusing slicing ranges
4. What is wrong with this code snippet?
import torch
t = torch.tensor([1, 2, 3, 4, 5])
print(t[::2, 1])
medium
A. Trying to index 2D on a 1D tensor
B. Slicing syntax is incorrect
C. Tensor is not created properly
D. Print statement missing parentheses

Solution

  1. Step 1: Analyze tensor shape

    The tensor t is 1D with shape (5,). It has only one dimension.
  2. Step 2: Check indexing used

    The code uses t[::2, 1] which tries to index two dimensions (rows and columns). This causes an error because t is 1D.
  3. Final Answer:

    Trying to index 2D on a 1D tensor -> Option A
  4. Quick Check:

    1D tensor cannot be indexed with two indices [OK]
Hint: Match number of indices to tensor dimensions [OK]
Common Mistakes:
  • Assuming 1D tensor can be indexed like 2D
  • Confusing slicing syntax with indexing
  • Ignoring tensor shape before indexing
5. You have a 3D tensor t with shape (4, 5, 6). Which indexing expression extracts all elements from the second dimension at index 3, across all batches and features?
hard
A. t[:, 3, :]
B. t[3, :, :]
C. t[:, :, 3]
D. t[3, 3, :]

Solution

  1. Step 1: Understand tensor dimensions

    Tensor shape is (4, 5, 6): first dim = batches, second dim = 5 elements, third dim = 6 features.
  2. Step 2: Select all batches, index 3 in second dim, all features

    Use : to select all in first and third dims, and 3 to select index 3 in second dim, so t[:, 3, :].
  3. Final Answer:

    t[:, 3, :] -> Option A
  4. Quick Check:

    Colon means all, index 3 picks second dim element [OK]
Hint: Use colon for all dims, index for specific dim [OK]
Common Mistakes:
  • Mixing dimension order
  • Using wrong index for dimension
  • Using single index for multiple dims
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard