liveBook · Manning

Bottleneck layer

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

A layer design that reduces and then restores dimensionality to make residual blocks compatible with shape constraints.

A residual block adds a learned long path to a short path. Addition requires both paths to have the same shape, which becomes a problem when a network changes its channel count after pooling. A residual bottleneck handles the channel change while keeping the short path simple, so information and gradients have a relatively direct route through deep networks. section 6.5.1 section 6.5.3

For input shape (B, C, W, H) and desired output shape (B, C′, W, H), the long path uses a 1 × 1 convolution to change channel width, a normal convolution to do the main work, and another 1 × 1 convolution to reach C′ channels. The short path uses an identity operation when C = C′; otherwise, a 1 × 1 convolution followed by batch normalization changes C to C′. It has no activation. The two outputs can then be added. section 6.5.3 section 6.5.4

Figure 6.11 Example of a bottleneck connection where the input has (B, C, W, H) as its input shape and the goal is to have an output shape of (B, C′,W, H). The short path has to change shape, so a 1 × 1 convolution is used to do the minimum work needed to change the number of channels. The long path is supposed to be a bottleneck that encourages compression, so it starts with a 1 × 1 convolution to shrink the number of channels, followed by a normal convolution and ending with another 1 × 1 convolution to expand the number of channels back up to the desired size C′.

The diagram tracks both paths from C input channels to matching C′ output channels, showing where the long path narrows and where the shortcut changes shape. section 6.5.3

PyTorch block design

Define an nn.Module with self.F for the long path and self.shortcut for the short path. self.F combines nn.BatchNorm2d, nn.LeakyReLU, and three nn.Conv2d layers with kernel sizes 1, kernel_size, and 1. Set self.shortcut to nn.Identity() unless the channel counts differ; then use a 1 × 1 convolution and batch normalization. Give the middle convolution padding that preserves width and height so its result can be added to the shortcut. Check the chosen intermediate width: the illustrated implementation uses max(out_channels//4, in_channels), which need not actually narrow the input channels. section 6.5.4

Narrowing the middle path can reduce parameters and memory, leaving room for more layers. Compression is also a representation constraint, but a narrower width is not automatically better. In an autoencoder, for example, a two-dimensional bottleneck makes embeddings easy to plot yet can lose enough information to reconstruct a digit incorrectly. Choose width against the task’s measured results. section 6.5.3 section 7.2.2

“Bottleneck” also names the small representation between an autoencoder’s encoder and decoder, or the pooled subnetwork inside a U-Net block. Those uses share a reduction-and-restoration pattern, but neither is the same as a residual block’s two paths joined by addition. section 7.2 section 8.3.1

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

How deep it goes
Works through channel shapes and a PyTorch module with separate long and shortcut paths, including the identity-versus-projection choice.
Examples
Builds a ResidualBottleNeck in PyTorch and places channel-changing bottlenecks among residual blocks in a Fashion-MNIST classifier.
What it assumes
Assumes familiarity with convolutional channels, pooling, and neural-network modules; develops the residual path and its shape requirement.
What you can do after
Build it from scratch as a PyTorch residual module that projects mismatched shortcut channels before addition.
How it connects
Connects residual blocks, skip connections, 1 × 1 convolutions, batch normalization, pooling, autoencoders, and U-Net.

Also explained in

Compare how 5 books explain bottleneck layer →

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.