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


