liveBook · Manning

Unsqueeze

As explained in Deep Learning with PyTorch, Second Edition

A tensor operation that inserts a new axis of size 1 at a specified position without changing the underlying data.

`unsqueeze` adds a dimension of size 1 at a position you choose. It changes a tensor’s shape, not its contents: a 1D tensor of shape `[B]` becomes `[B, 1]` with `unsqueeze(1)`. An element previously accessed as `target[0]` is then accessed as `target_unsqueezed[0, 0]`. You can call it as a tensor method or as `torch.unsqueeze(tensor, dimension)`. section 4.3.4 section 4.2.1

Add an axis where the next operation expects one:

  • **A channel for a CT volume:** `torch.unsqueeze(vol, 0)` changes `[D, H, W]` to `[1, D, H, W]`; the leading 1 is the channel dimension. section 4.2.1
  • **A column for class indices:** `target.unsqueeze(1)` changes `[4898]` to `[4898, 1]` so the indices have the same number of dimensions as a two-dimensional one-hot target for `scatter_`. section 4.3.4
  • **A feature column for a linear layer:** `torch.tensor(t_u).unsqueeze(1)` makes 11 scalar inputs shape `[11, 1]`: one row per sample, one feature per row for `nn.Linear(1, 1)`. section 6.2.2
  • **A batch for one image:** `img.unsqueeze(0)` changes a `[3, 32, 32]` image to `[1, 3, 32, 32]`, the batch–channel–height–width shape expected by `nn.Conv2d`. section 8.2

Making indices fit scatter_

Suppose `target` holds one class index per row and `target_onehot` has one row per example and one column per class. Use `target_onehot.scatter_(1, target.unsqueeze(1), 1.0)`. The added column dimension lets each row’s index select the column to fill with 1.0; it does not add another class index. section 4.3.4

# In[8]:
target_onehot = torch.zeros(target.shape[0], 10)
target_onehot.scatter_(1, target.unsqueeze(1), 1.0)

random_indices = torch.randint(0, len(target), (5,))
print(target[random_indices])
print(target_onehot[random_indices])

# Out[8]:
5 random wine quality ratings (original):
tensor([6, 6, 7, 4, 5])

First 5 wine quality ratings (one-hot encoded):
tensor([[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]])

The listing shows `unsqueeze(1)` inside a one-hot encoding call and compares class indices with the resulting rows. section 4.3.4

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Defines the singleton dimension and works through shape and indexing changes, without examining storage internals.
Examples
Shows a DICOM volume, class-index columns, thermometer inputs, weather encodings, and a single image passed to a convolution.
What it assumes
Assumes basic tensor indexing and shapes while explaining what an added singleton dimension means.
What you can do after
Apply it via PyTorch to insert the singleton axis needed before feeding data to scatter_, nn.Linear, or nn.Conv2d.
How it connects
Connects tensor shapes to one-hot encoding, scatter_, nn.Linear, and nn.Conv2d.

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.