liveBook · Manning

Tensor shape

As explained in Fast Python: High performance techniques for large datasets

A tensor shape is an ordered sequence of dimension sizes specifying the extent of a tensor or array along each axis.

A NumPy array’s shape gives the number of elements along each axis. A grayscale image that is 182 pixels wide and 45 pixels high has array shape (45, 182): rows come before columns. Check this ordering before reshaping or indexing image data. section 4.1.1

Shape describes the extent of a view, not how its elements are reached in memory. Strides supply the byte step along each axis. Starting with ten contiguous uint32 values, reshaping to (2, 5) and transposing to (5, 2) can produce different perspectives on shared data without copying it. Inspect both `.shape` and `.strides`, then use `np.shares_memory` to check whether an operation made a view. section 4.1.2

Table 4.3 Shapes and strides of axes swap, transposing, and rotation (view table figure)

Operation

Shape

Stride

swapaxes

182 45

45 1

transpose

182 45

1 182

rot90

182 45

-1 182

slice

30 105

182 1

Compare the reported shapes and strides: reshaping, transposing, and rotating can yield shape (182, 45) while traversing the image differently. Shape alone does not establish the resulting image or memory layout. section 4.1.3

Use shape as a practical check when processing large arrays:

  • For flips and slices, inspect changed strides as well as the resulting shape. These operations can retain shared data instead of allocating another large array. section 4.1.3
  • For arithmetic, account for broadcasting rather than requiring equal shapes. Adding one scalar to a large array avoids allocating a same-shaped array of ones. section 4.2.2
  • Do not assume every reshape remains a view: reshaping a stepped slice can require a copy. Avoid setting arbitrary shapes and strides with `as_strided` without extreme care. section 4.1.2 section 4.1.3

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Works through shapes alongside byte strides and shared memory, while deferring fuller allocation details.
Examples
Uses a ten-element array, grayscale image transformations, and scalar addition to expose shape-dependent behavior.
What it assumes
Assumes working familiarity with Python and NumPy arrays, but develops view metadata from a simple linear array.
What you can do after
Diagnose/debug with it by checking `.shape`, `.strides`, and shared memory before assuming an array transformation avoids a copy.
How it connects
Connects shape to NumPy views, strides, reshaping, transposition, slicing, and broadcasting.

Also explained in

Compare how 5 books explain tensor shape →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.