liveBook · Manning

Concatenation

As explained in Inside Deep Learning: Math, Algorithms, Models

The operation of joining tensors along a chosen axis, requiring the other axes to have matching sizes.

Concatenation joins tensors along one chosen axis. Every other axis must already have the same size, so the operation stacks values without changing their alignment across those axes. section 6.3.2 section 6.3

For tensors shaped (B, D) and (B, H), concatenating on dim=1 produces (B, D+H). In PyTorch, torch.cat([x, h], dim=1) expresses this directly: dim=1 is the feature axis, while the batch axis remains unchanged. section 6.3

Check these conditions before concatenating:

  • Choose the axis whose size is allowed to grow. section 6.3.2
  • Match every other axis exactly, including spatial dimensions in image tensors. section 6.3.2
  • Account for the expanded input size in the following layer; concatenating channels changes the next layer’s channel count. section 6.3 section 8.3.1

Skip connections and U-Net

A convolutional skip connection can join (B, C, W, H) with (B, C/2, W, H) on dim=1, producing (B, 3C/2, W, H). It cannot join tensors across a pooling step when the spatial shapes differ, such as (B, C, W, H) and (B, C, W/2, H/2). U-Net uses the same idea: it concatenates a full-resolution result with the bottleneck result before applying its output layers, preserving information from multiple resolutions. section 6.3.2 section 8.3.1

def forward(self, x): 
    full_scale_result = self.in_model(x)                   #1

    if self.bottleneck is not None:                        #2 
        bottle_result = self.bottleneck(full_scale_result) #3

        full_scale_result = torch.cat(                     #4 
        [full_scale_result, bottle_result], dim=1)

    return self.out_model(full_scale_result)               #5

Inspect the U-Net forward pass to see torch.cat combine full-scale and bottleneck results on the channel axis. section 8.3.1

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains axis compatibility, resulting shapes, and PyTorch usage, but stops short of implementation internals beyond torch.cat.
Examples
Demonstrates feature vectors, convolutional feature maps, U-Net paths, and conditional GAN latent vectors.
What you can do after
Apply it via PyTorch’s torch.cat while preserving matching non-concatenated dimensions.
How it connects
Connects concatenation to tensors, skip connections, U-Net, pooling, channels, and conditional GANs.

Also explained in

Compare how 10 books explain concatenation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.