liveBook · Manning

Tensor shape

As explained in Learn Generative AI with PyTorch

The tuple of dimension sizes describing the structure of a multidimensional object.

A tensor shape is a tuple of dimension sizes describing a multidimensional tensor. A shape of (2, 3) means two rows and three columns; a one-dimensional shape such as (46) contains 46 values. Checking shapes matters because incompatible shapes cause errors during tensor operations. section 2.1.1 section 2.1.3

Use shape information to read, combine, and reorganize tensors:

  • Read a tensor's dimensions with its shape attribute; heights_tensor is one-dimensional with 46 values. section 2.1.3
  • Concatenate tensors along a selected dimension with cat(); dim=0 joins the first dimension, producing 92 values from two 46-value tensors. section 2.1.3
  • Reshape the result to (2, 46), then use one index per dimension to retrieve or slice values. section 2.1.3

From measurements to a two-dimensional tensor

Convert heights from centimeters to feet, concatenate the original and converted tensors, and reshape the 92 values into two rows of 46. The expression heights_reshaped[1,-2] selects the second-to-last value in the second row, returning 6.2664 feet. section 2.1.3

print(heights_tensor.shape)

Inspect a tensor's shape directly with print(heights_tensor.shape), then compare the reported dimensions with the number of indexes needed to access its values. section 2.1.3

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines dimensions, demonstrates shape inspection and transformation, and stops short of broader tensor algebra or model-specific shape rules.
Examples
Demonstrates height measurements with shapes (46), (92), and (2, 46), plus a music object shaped (4, 2, 16, 84).
What it assumes
Builds tensor-shape understanding from zero while assuming basic Python list and indexing familiarity.
What you can do after
Apply it via PyTorch to inspect dimensions, combine tensors, reshape data, and access values with dimension-matched indexes.
How it connects
Connects tensor shape with PyTorch tensors, concatenation, reshaping, indexing, GAN labels, and multidimensional music representations.

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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.