Indexing and Slicing Tensors
What you'll learn: How to access and extract specific elements, rows, columns, or sub-regions from PyTorch tensors using familiar indexing patterns.
The Core Idea
Just like Python lists or NumPy arrays, PyTorch tensors support indexing (accessing individual elements) and slicing (extracting ranges or sub-tensors). This is essential for data manipulation, extracting mini-batches, selecting specific features, or analyzing model outputs.
Think of a tensor as a multi-dimensional grid. Indexing is like giving the exact coordinates to retrieve one cell, while slicing is like cutting out a rectangular section of that grid.
Basic Indexing
Access individual elements using square brackets with indices for each dimension:
import torch
x = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
element = x[0, 1] # First row, second column → 2
row = x[1] # Entire second row → tensor([4, 5, 6])
Slicing Syntax
Use the colon : to specify ranges with start:stop:step (just like Python lists):
first_two_rows = x[0:2] # Rows 0 and 1
last_column = x[:, -1] # All rows, last column → tensor([3, 6, 9])
every_other = x[::2] # Every second row
sub_block = x[1:, :2] # Rows 1-end, first two columns
Advanced Indexing
Use lists or tensors as indices to select specific rows/columns non-contiguously:
selected_rows = x[[0, 2]] # Rows 0 and 2 only
mask = x > 5 # Boolean tensor
filtered = x[mask] # Elements greater than 5
Key Takeaway: PyTorch tensors support NumPy-style indexing with integers, slices (:), and boolean masks, enabling flexible data extraction for preprocessing, batching, and analysis in deep learning pipelines.