liveBook · Manning

Tensor reshaping

As explained in Inside Deep Learning: Math, Algorithms, Models

An operation that changes a tensor's shape without changing its total number of values, used here to turn a network output into an image-shaped tensor.

Tensor reshaping changes how values are arranged without changing their total count. A tensor containing six values can become a vector, a 2×3 matrix, or another compatible shape. section 2.1.4

Use reshaping carefully in PyTorch:

  • Specify known dimensions and use -1 for one dimension whose size PyTorch should infer. section 2.1.4
  • Use reshape to change a convolutional output from (C, W, H) into (C×W×H) before a linear layer; this operation is called flattening. section 3.4.3
  • Prefer reshape for straightforward, safe code; view can use less memory and run faster but may fail inappropriately. section 2.1.4

Six values, several shapes

A tensor with N=6 values can become shape (3, 2), because 3×2=6. In dataset preparation, X.reshape(-1, 1) turns each scalar feature into a one-column input. For convolutional networks, nn.Flatten performs the analogous collapse of spatial dimensions. section 2.1.4 section 3.4.3

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains compatible dimensions, -1 inference, view tradeoffs, and flattening, but stops short of advanced memory-layout details.
Examples
Uses six-value tensors, X.reshape(-1,1), and nn.Flatten to demonstrate compatible shapes and feature collapsing.
What it assumes
Assumes basic tensors and dimensions while teaching compatible shape arithmetic and PyTorch reshape behavior directly.
What you can do after
Apply it via PyTorch to align dataset and network tensor shapes without changing the number of values.
How it connects
Connects tensor shapes, flattening, convolutional layers, linear layers, NumPy, and PyTorch.

Also explained in

Compare how 3 books explain tensor reshaping →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.