Chapter 8: Residual Connections & Layer Normalization

By ·

Chapter 8: Residual Connections & Layer Normalization

The Gradient Flow Problem

Why do we need residual connections? Let's understand the problem they solve.

Imagine training a very deep network (many layers stacked):

Input → Layer 1 → Layer 2 → Layer 3 → ... → Layer 50 → Output

During training, we compute gradients (how to improve) through backpropagation, which flows BACKWARDS:

Output → Layer 50 → Layer 49 → ... → Layer 2 → Layer 1 → Input

The problem: As gradients flow backward through many layers, they can:

  • Vanish: Get multiplied by small numbers repeatedly → become tiny → early layers barely learn
  • Explode: Get multiplied by large numbers repeatedly → become huge → training becomes unstable

Analogy: The telephone game:

  • Person 1 whispers a message to Person 2
  • Person 2 whispers to Person 3
  • ...
  • Person 50 whispers to Person 51

By the time the message reaches Person 51, it's completely garbled! Information degrades through many hops.

Same with gradients: After flowing through 50 layers, the gradient signal becomes too weak or distorted for early layers to learn effectively.

Residual Connections (The Highway Solution)

The brilliant fix: Create a "shortcut" or "highway" that bypasses layers!

After attention, we add the original input back:

\text{Output} = \text{LayerNorm}(\text{Attention}(x) + x)

And after FFN:

\text{Output} = \text{LayerNorm}(\text{FFN}(x) + x)

The "+x" is the residual connection!

Example calculation:

After attention: [0.41, -0.18, 0.55, 0.29, -0.07, 0.38] Original input: [0.25, -0.31, 0.42, 0.18, -0.09, 0.33]

\begin{align} \text{Sum} &= [0.41, -0.18, 0.55, 0.29, -0.07, 0.38] + [0.25, -0.31, 0.42, 0.18, -0.09, 0.33] \\ &= [0.66, -0.49, 0.97, 0.47, -0.16, 0.71] \end{align}

The Highway Analogy (Understanding Why This Matters)

Imagine driving from City A to City B (50 miles away):

Route Option 1: Only Local Roads (No Residual)

City A → Small town (stop) → Another town (stop) → Another (stop) → ... → City B

- Must pass through EVERY town
- 50 stop lights
- If one road is blocked (gradient vanishes), you're stuck!
- Takes 2 hours

Route Option 2: Highway + Local Roads (With Residual)

City A → Split:
         Path 1: Highway (direct, fast, always clear) → City B  
         Path 2: Local roads (scenic, processes towns) → City B
         
Both paths arrive! You get:
- Speed of highway (gradient flows!)
- Scenery from local roads (transformation learned!)

In transformers:

Without residual (bad):

Input → Attention → (signal weakens) → More layers → (signal weakens more) → Output

After 50 layers, the original input signal is completely lost! Early layers can't learn because gradients can't reach them.

With residual (good):

Input → Attention → Add back input → More layers → ... → Output
        ↓                ↑
        (processes)  (preserves original)

The original input is ALWAYS present! Even after 100 layers, we still have the original signal plus all the transformations.

Why Residual Connections Work So Well

Three critical reasons:

  1. Gradient Highway: Provides direct path for gradients to flow backward through 100+ layers without vanishing

    • Like having a highway for gradients—they can zoom straight through!
  2. Learn Deltas (Differences): Instead of learning f(x) = y (the complete transformation), the layer learns f(x) = y - x (just the change/residual). Much easier!

    • Like learning "add 5 to what you started with" instead of "transform 10 into 15"
  3. Safety Net: If a layer hasn't learned anything useful yet, the identity path passes information through unchanged

    • A useless layer outputs ≈0, so output ≈ x (input passes through safely!)

The Water Pipe Analogy

Think of information like water flowing through pipes:

Without residual:

Input (100L water) → Pipe 1 → (leak 10L) → 90L → Pipe 2 → (leak 10L) → 80L → ...
After 10 pipes: Only 0L left! (all leaked away)

With residual (bypass pipes):

Input (100L) → Pipe 1 + Bypass pipe 1 → Still 100L (bypass keeps it!)
            → Pipe 2 + Bypass pipe 2 → Still 100L
            → ...
After 10 pipes: Still 100L! (bypasses prevented leakage)

The bypass pipes (residual connections) ensure water (information/gradients) makes it through!

Mathematical insight:

\frac{\partial (x + f(x))}{\partial x} = 1 + \frac{\partial f(x)}{\partial x}

The "+1" ensures gradient flow even if \frac{\partial f(x)}{\partial x} \to 0

Layer Normalization

The Test Score Standardization Analogy

Imagine two classes took different math tests:

Class A's test (easy test):

  • Student 1: 95/100
  • Student 2: 92/100
  • Student 3: 88/100
  • Average: 91.7, everyone did great!

Class B's test (hard test):

  • Student 1: 45/100
  • Student 2: 42/100
  • Student 3: 38/100
  • Average: 41.7, everyone struggled!

Problem: Can you compare Student 1 from Class A (95) with Student 1 from Class B (45)? Not fairly! The tests were different difficulties.

Solution: Standardize the scores!

For each student, calculate: "How far from the class average are you, in standard units?"

Class A Student 1:

Score: 95
Class average: 91.7
Distance from average: 95 - 91.7 = 3.3 (above average)
After standardization: +0.8 (slightly above average)

Class B Student 1:

Score: 45
Class average: 41.7
Distance from average: 45 - 41.7 = 3.3 (above average by same amount!)
After standardization: +0.8 (also slightly above average!)

Now they're comparable! Both students were equally good relative to their class, even though raw scores were very different (95 vs 45).

LayerNorm does EXACTLY this for neuron activations!

Formula:

\text{LayerNorm}(x) = \gamma \odot \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}} + \beta

Breaking down the scary symbols:

\mu (mu) = mean (average) of all numbers in the vector

  • Like the "class average"

\sigma^2 (sigma squared) = variance (how spread out the values are)

  • \sigma (sigma) = standard deviation
  • Like measuring "how different are the test scores from each other?"

x - \mu = How far is each value from the average?

  • Like "Student 1 scored 3.3 points above class average"

\frac{x - \mu}{\sigma} = How far from average, in "standard units"?

  • Dividing by \sigma (standard deviation) normalizes the scale

\epsilon (epsilon) = 0.00001 (tiny number)

  • Prevents dividing by zero if all values happen to be the same

\gamma (gamma) = learned scale (starts at 1)

  • Lets the model learn "maybe I want bigger values"

\beta (beta) = learned shift (starts at 0)

  • Lets the model learn "maybe I want to shift everything up or down"

\odot = element-wise multiplication

  • Multiply corresponding positions: [a, b] \odot [x, y] = [a×x, b×y]

Example:

[2, 3, 4] \odot [0.5, 2, 0.1] = [2 \times 0.5, 3 \times 2, 4 \times 0.1] = [1, 6, 0.4]

This lets each dimension be scaled independently!

Hand calculation:

Input: [0.66, -0.49, 0.97, 0.47, -0.16, 0.71]

Step 1: Calculate mean

\mu = \frac{0.66 + (-0.49) + 0.97 + 0.47 + (-0.16) + 0.71}{6} = \frac{2.16}{6} = 0.36

Step 2: Calculate variance

What is variance? It measures how spread out the numbers are from the mean.

Formula: \sigma^2 = \frac{1}{n}\sum_{i=1}^{n}(x_i - \mu)^2

In words: "Take each number, subtract the mean, square it, then average all those squared differences"

\begin{align} \sigma^2 &= \frac{(0.66-0.36)^2 + (-0.49-0.36)^2 + (0.97-0.36)^2 + (0.47-0.36)^2 + (-0.16-0.36)^2 + (0.71-0.36)^2}{6} \end{align}

Let's calculate each squared difference:

\begin{align} (0.66-0.36)^2 &= (0.30)^2 = 0.09 \\ (-0.49-0.36)^2 &= (-0.85)^2 = 0.7225 \\ (0.97-0.36)^2 &= (0.61)^2 = 0.3721 \\ (0.47-0.36)^2 &= (0.11)^2 = 0.0121 \\ (-0.16-0.36)^2 &= (-0.52)^2 = 0.2704 \\ (0.71-0.36)^2 &= (0.35)^2 = 0.1225 \end{align}

Now sum and divide:

\begin{align} \sigma^2 &= \frac{0.09 + 0.7225 + 0.3721 + 0.0121 + 0.2704 + 0.1225}{6} \\ &= \frac{1.5896}{6} = 0.2649 \end{align}

Intuition:

  • Small variance (e.g., 0.01) → numbers are close together
  • Large variance (e.g., 100) → numbers are spread far apart

Step 3: Calculate standard deviation

\sigma = \sqrt{0.2649 + 0.00001} = \sqrt{0.26491} = 0.5147

Step 4: Normalize each value

\begin{align} \text{norm}_0 &= \frac{0.66 - 0.36}{0.5147} = 0.583 \\ \text{norm}_1 &= \frac{-0.49 - 0.36}{0.5147} = -1.651 \\ \text{norm}_2 &= \frac{0.97 - 0.36}{0.5147} = 1.185 \\ \text{norm}_3 &= \frac{0.47 - 0.36}{0.5147} = 0.214 \\ \text{norm}_4 &= \frac{-0.16 - 0.36}{0.5147} = -1.010 \\ \text{norm}_5 &= \frac{0.71 - 0.36}{0.5147} = 0.680 \end{align}

Step 5: Apply learned scale and shift

Assume \gamma = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0] and \beta = [0, 0, 0, 0, 0, 0] (initial values)

\text{Final} = \gamma \odot [0.583, -1.651, 1.185, 0.214, -1.010, 0.680] + \beta
= [0.583, -1.651, 1.185, 0.214, -1.010, 0.680]

Why Layer Normalization?

Layer normalization solves several critical problems in deep learning:

1. Prevents numerical instability: Without normalization, as you stack 96 layers, values can explode (become millions) or vanish (become 0.000001). Imagine a game of telephone where each person speaks 2× louder than the last—by person 10, they're screaming! LayerNorm resets the volume at each layer.

2. Stabilizes training: Different layers might naturally operate at different scales. Layer 1 outputs might be around [-1, 1], while Layer 50 outputs might be around [-100, 100]. This makes it very hard to choose a single learning rate that works for all layers. By normalizing to mean=0 and variance=1 at each layer, all layers operate in the same numerical range.

3. Reduces sensitivity to initialization: If you start with bad random weights, unnormalized networks might take forever to recover. LayerNorm gives each layer a "fresh start" with normalized inputs, making training more robust.

4. Speeds up convergence: With stable, normalized values, gradient descent can take more confident steps. Training reaches good solutions faster—often 2-3× fewer steps needed.

Think of it like standardizing test scores. If one class's test was easy (average 90/100) and another class's test was hard (average 40/100), you can't compare students fairly. You normalize: calculate each student's distance from their class mean, measured in standard deviations. Now students from both classes are on the same scale. LayerNorm does this for neuron activations.

LayerNorm vs BatchNorm (an important distinction):

  • BatchNorm (used in CNNs): Normalizes across the batch dimension. Takes all examples at once and normalizes each feature across those examples. Problem: doesn't work well with variable-length sequences and small batches.

  • LayerNorm (used in Transformers): Normalizes across the feature dimension. Each individual example is normalized independently, looking at all its features. This works great for sequences of any length and any batch size, even batch size of 1!

Pre-LayerNorm vs Post-LayerNorm:

Original transformers (2017) used Post-LayerNorm: LayerNorm(x + Sublayer(x))

Modern transformers often use Pre-LayerNorm: x + Sublayer(LayerNorm(x))

The difference? Pre-LayerNorm normalizes before the operation instead of after. This creates a cleaner gradient path and makes very deep models (100+ layers) easier to train. The residual connection gets the unchanged signal, while the sublayer gets normalized input.


Course navigation: Previous: Chapter 7 - Feed-Forward Network | Next: Chapter 9 - Stacking Transformer Blocks