liveBook · Manning

Indexing

As explained in Deep Learning with PyTorch, Second Edition

Selecting tensor elements or slices by using tensor values as indices or masks.

Indexing selects elements, rows, columns, or slices from tensors. It follows familiar zero-based Python list syntax, then extends it across multiple tensor dimensions so data can be manipulated expressively and efficiently. section 3.2.1 section 3.3

Common forms include:

  • Use a single position such as a[0], or assign through an index to change an element. section 3.2.1
  • Use ranges such as points[1:], points[1:, :], or points[:, 0] to select portions of one or more dimensions. section 3.3
  • Use a Boolean tensor as advanced indexing to filter rows whose corresponding mask entries are True. section 4.3.6

Filtering wines by quality

A comparison creates a Boolean index: bad_indexes = target <= 3. Applying data[bad_indexes] keeps the 20 matching wine rows and all 11 columns. Combining masks, as in (target > 3) & (target < 7), groups wines into quality ranges; another mask can compare predicted and actual categories. section 4.3.6

# In[54]:
points[1:]      #1
points[1:, :]     #2
points[1:, 0]       #3
points[None]         #4

This listing makes multidimensional range indexing concrete by contrasting row slices, column selection, and adding a dimension with None. section 3.3

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains list-like ranges, multidimensional slices, Boolean filtering, and storage-level indexing, but defers broader tensor operations.
Examples
Uses Python lists, two-dimensional points, wine-quality masks, threshold predictions, and image, text, and source-code exercises.
What it assumes
Builds from zero-based Python list indexing before extending the same intuition to tensor dimensions and Boolean masks.
What you can do after
Apply indexing in PyTorch to slice dimensions, filter rows with Boolean masks, and compare selected data categories.
How it connects
Connects indexing with tensors, Boolean masks, slicing, storage, broadcasting, and data representation.

Also explained in

Compare how 15 books explain indexing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.