liveBook · Manning

Stride

As explained in Deep Learning with PyTorch, Second Edition

Tensor metadata describing its shape and how elements are laid out and indexed in storage, including step size between positions.

A tensor’s stride describes how far to move through storage when an index increases by one along each dimension. Together with shape and storage offset, it determines how the tensor indexes its underlying data. section 3.9 section 3.9.1

Reading a two-dimensional stride

For points.stride() == (2, 1), moving to the next row skips two storage elements, while moving to the next column skips one. Element [i, j] is found at storage_offset + stride[0] * i + stride[1] * j. section 3.9.1

Stride makes several tensor behaviors easier to understand:

  • Views, subtensors, and transposes can reuse existing storage by changing shape, offset, or stride instead of copying data. section 3.9.1 section 3.9.2
  • Indexing a subtensor can change its offset and dimensionality while preserving the link to the original tensor. section 3.9.1
  • A transposed tensor reverses the stride ordering: points uses (2, 1), while points_t uses (1, 2). section 3.9.2
  • Noncontiguous tensors may need contiguous() before operations such as view; contiguous() creates rearranged storage with a new stride. section 3.9.4

Reshaping hourly data

A tensor shaped [17520, 17] with stride (17, 1) can be viewed as daily_bikes with shape [730, 24, 17] and stride (408, 17, 1). Transposing its hour and feature dimensions changes the stride to (408, 1, 17) without copying storage. section 4.4.2

Figure 3.5 Relationship between a tensor’s offset, size, and stride. Here, the tensor is a view of a larger storage, like one that might have been allocated when creating a larger tensor.
figure

Use the storage diagram to connect shape, storage offset, and per-dimension stride in a tensor view. section 3.9

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Derives the indexing formula and follows stride through views, transposes, reshaping, and contiguity, stopping short of deeper memory-system details.
Examples
Uses point tensors, indexed subtensors, transposes, contiguous copies, daily bike sequences, and size-offset-stride exercises.
What it assumes
Assumes basic tensor indexing and storage ideas while defining shape, offset, and stride through concrete examples.
What you can do after
Diagnose tensor layouts by predicting stride, storage offset, shared storage, and contiguity after indexing or reshaping.
How it connects
Connects tensor shape, storage offset, tensor views, storage, transpose, reshaping, and contiguity.

Also explained in

Compare how 8 books explain stride →

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.