liveBook · Manning

Layer normalization

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

A normalization technique that standardizes activations within each example rather than across a batch.

Layer normalization (LN) standardizes activations across features within each example. Each example computes its own mean and standard deviation, while all examples share learned scale γ and shift β parameters. Unlike batch normalization, LN does not depend on batch size, making it useful when batches are small or when examples must be normalized independently. section 6.2.1 section 6.2.4

Use the following implementation rules when placing LN in PyTorch models:

  • Place normalization between a linear or convolutional layer and its activation; learned γ and β let training undo or adjust standardization when needed. section 6.2.1
  • Construct fully connected normalization with nn.LayerNorm([D]) for tensors shaped (B, D), where D is the feature count. section 6.2.5
  • Pass all feature dimensions to normalize from right to left; for (B, C, W, H), use [C, W, H]. section 6.2.5
  • Track convolutional spatial dimensions after pooling, because LN’s normalized shape must match the current channel, width, and height. section 6.2.5
  • Prefer LN as a reliable starting point for GANs, where batch normalization can cause problems; replacing it with BN in the generator remains an optional performance experiment. section 9.1.3

Fully connected and convolutional LN

A fully connected block can use nn.Linear(D, n), nn.LayerNorm([n]), and nn.LeakyReLU(...). For convolutional blocks, a helper can pair nn.Conv2d with nn.LayerNorm([out_filters, W//(2**pool_factor), H//(2**pool_factor)]) and an activation. Padding preserves width and height through convolutions, reducing the shape bookkeeping required after pooling. section 6.2.5

LN inside a recurrent model

A stacked GRUCell model can maintain one LayerNorm(hidden_size) per recurrent layer and normalize each updated hidden state before passing it to the next layer. A prediction head can also place LayerNorm(hidden_size) between a linear layer and LeakyReLU. section 7.5.1

B, D = X.shape                 #1
μ = torch.zeros((B))
σ = torch.zeros((B)) 
for j in range(D):
    μ += X[:, j]               #2
μ /= D 
for j in range(D):
    σ += (X[:,j]-μ)*(X[:,j]-μ) #3
σ += 1e-5
σ = torch.sqrt(σ)

Shows the per-example feature mean and standard deviation calculation underlying LN, including the numerical stabilizer added before taking the square root. section 6.2.4

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

How deep it goes
Explains per-example statistics, learned affine parameters, PyTorch shapes, and CNN bookkeeping, but defers deeper reasons for LN’s flexibility.
Examples
Uses fully connected networks, convolutional networks with pooling, GAN generator and discriminator blocks, and stacked GRUCell text models.
What it assumes
Assumes familiarity with neural-network layers, activations, tensor shapes, and batch normalization while explaining LN’s averaging axis explicitly.
What you can do after
Apply it via PyTorch’s nn.LayerNorm by matching normalized dimensions to each tensor shape and inserting it before activations.
How it connects
Connects layer normalization to batch normalization, activation functions, convolutional neural networks, recurrent architectures, and GAN training.

Also explained in

Compare how 7 books explain layer normalization →

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.