liveBook · Manning

Tensor shape

As explained in Deep Learning with PyTorch, Second Edition

The sizes of an array or tensor along each axis, here specifically the channels-height-width layout for image tensors.

A tensor’s shape gives its size along each axis. For example, a tensor holding three points with two coordinates each has shape (3, 2): three rows, two values per row. In PyTorch, inspect this with `points.shape`. section 3.2.3

For images, axis order matters as much as axis size. PyTorch image tensors commonly use C × H × W: channels, height, width. An RGB image that is 32 pixels high and 32 pixels wide therefore has shape (3, 32, 32). Adding a batch puts the number of images first, giving N × C × H × W. For example, `torch.zeros(len(png_files), 3, 256, 256, dtype=torch.uint8)` makes room for a batch of 256 × 256 RGB images. section 4.1.3 section 7.1.3

Put channels first

An image array may arrive as H × W × C. Convert it with `torch.from_numpy(img_arr)`, then use `permute(2, 0, 1)` to place channels first. A resulting shape of `torch.Size([3, 720, 1280])` describes three channels, 720 rows, and 1280 columns. Alternatively, `torchvision.transforms.ToTensor()` converts a PIL image or NumPy array to a C × H × W tensor; a 32 × 32 RGB image becomes `(3, 32, 32)`. To display that tensor with Matplotlib, permute its axes back to H × W × C. section 4.1.3 section 7.1.3

Shape also helps organize non-image data. Hourly records can be grouped into days with `view(-1, 24, bikes.shape[1])`, producing (730, 24, 17). Transposing axes 1 and 2 produces (730, 17, 24): days × variables × hours. The choice keeps each variable’s daily readings together. Shape is not the whole storage story: a transpose can change strides without reallocating the underlying data. Shape also determines whether tensors can participate in broadcasting. section 4.4.2 section 3.9.1 section 3.4

# In[3]:
img = torch.from_numpy(img_arr)
out = img.permute(2, 0, 1)
out.shape

# Out[3]:
torch.Size([3, 720, 1280])

The listing shows the axis permutation and its resulting C × H × W shape. section 4.1.3

# In[5]:
daily_bikes = daily_bikes.transpose(1, 2)
daily_bikes.shape, daily_bikes.stride()

# Out[5]:
(torch.Size([730, 17, 24]), (408, 1, 17))

The listing shows how transposing a daily time series changes both its shape and strides. section 4.4.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through shape inspection, axis permutation, batching, and time-series reshaping, with brief links to strides and broadcasting.
Examples
Uses RGB image tensors, a batch of PNG files, coordinate points, and hourly records grouped into days.
What it assumes
Builds from Python lists and image arrays, introducing tensor dimensions through coordinate points before using image layouts.
What you can do after
Operate/configure PyTorch tensors by checking shapes and arranging image or time-series axes for the intended use.
How it connects
Connects tensor shape to dataset transforms, tensor views, strides, 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 Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.