Chapter 7: Feed-Forward Network (Individual Processing)

By ·

Chapter 7: Feed-Forward Network (Individual Processing)

The Two-Stage Process: Gather, Then Think

We've just finished attention (Chapter 5)—words gathered information from each other. But gathering information isn't enough! Now each word needs to PROCESS that information.

Think of it like this:

Attention (Chapter 5): Group discussion

"I" asks everyone: "What's relevant to me?"
"love" asks everyone: "What's relevant to me?"
"pizza" asks everyone: "What's relevant to me?"

Result: Each word now has context-enriched information

Feed-Forward Network (This Chapter): Individual thinking

"I" thinks independently: "Given what I learned, what patterns do I recognize?"
"love" thinks independently: "Based on the context, how should I interpret myself?"
"pizza" thinks independently: "What conclusions can I draw from the gathered info?"

Result: Each word processes its information independently (no communication)

The analogy: A team meeting at work:

During the meeting (Attention):

  • Everyone shares updates
  • You ask questions: "How does your work affect mine?"
  • You gather information from teammates

After the meeting (FFN):

  • You go back to your desk
  • You think independently: "Based on what I heard, what should I do?"
  • Each person processes simultaneously but SEPARATELY
  • No more communication—just deep individual thinking

Why Do We Need This?

"Why not just use attention repeatedly?"

Attention is a communication mechanism—it's great at gathering and mixing information from different positions, but it's linear in nature (just weighted averaging of vectors).

The FFN adds non-linear transformation! This is critical because:

Problem with only linear operations:

If we only had linear operations (matrix multiplications, additions):

Layer 1: Linear transformation
Layer 2: Linear transformation  
Layer 3: Linear transformation

Mathematical fact: Multiple linear operations = ONE big linear operation!

No matter how many layers, we'd only be able to learn simple, linear patterns!

FFN introduces non-linearity:

  • Through the ReLU activation: \text{ReLU}(x) = \max(0, x)
  • This allows the network to learn complex, non-linear patterns
  • Like the difference between learning "2x + 3" vs learning "if x < 0, then A, otherwise B²"

The Architecture

Hyperparameters:

  • Input: d_{\text{model}} = 6
  • Hidden: d_{ff} = 24 (typically 4 \times d_{\text{model}}; GPT-3 uses 4 \times 12288 = 49152)
  • Output: d_{\text{model}} = 6

The two-step process:

  1. Expand: 6 dimensions → 24 dimensions (4× bigger!)
  2. Contract: 24 dimensions → 6 dimensions (back to original)

Why expand to 4×?

The "thinking space" analogy:

Imagine you're solving a complex problem:

  1. Gather information: Collect all relevant facts (attention did this)
  2. Spread out on a big table: Lay everything out where you have space to work (expansion to 24D)
  3. Process: Make connections, recognize patterns, do calculations (in the high-dimensional space)
  4. Summarize: Write down your conclusions on one page (contraction to 6D)

The expansion to 24 dimensions gives the network "room to think"! It can create intermediate representations, find complex patterns, and then compress the insights back down.

Why specifically 4×? This is empirically found to work well:

  • Too small (2×): Not enough computational capacity
  • Too large (16×): Wastes memory and computation
  • 4× is the "Goldilocks" value that balances capacity with efficiency

Fun fact: The FFN contains about 2/3 of all parameters in a transformer! In GPT-3:

  • Attention mechanisms: ~58 billion parameters
  • FFN layers: ~117 billion parameters

The FFN is where most of the "knowledge" and "reasoning" lives!

Formula:

\text{FFN}(x) = \text{ReLU}(x\mathbf{W}_1 + \mathbf{b}_1)\mathbf{W}_2 + \mathbf{b}_2

Where:

  • \mathbf{W}_1: 6 \times 24 matrix
  • \mathbf{b}_1: 24 bias vector
  • \mathbf{W}_2: 24 \times 6 matrix
  • \mathbf{b}_2: 6 bias vector

Step-by-Step Calculation

Input (from attention): [0.25, -0.31, 0.42, 0.18, -0.09, 0.33]

Step 1: Expand to 24 dimensions

Weight matrix \mathbf{W}_1 (showing first 6 of 24 columns for illustration):

\mathbf{W}_1 = \begin{bmatrix} 0.1 & -0.2 & 0.3 & 0.15 & 0.05 & -0.1 & \ldots \\ 0.2 & 0.3 & -0.1 & 0.25 & -0.15 & 0.2 & \ldots \\ -0.1 & 0.1 & 0.2 & -0.15 & 0.3 & 0.05 & \ldots \\ 0.3 & -0.15 & 0.1 & 0.2 & -0.05 & 0.25 & \ldots \\ 0.15 & 0.25 & -0.2 & 0.1 & 0.35 & -0.15 & \ldots \\ -0.2 & 0.1 & 0.3 & -0.1 & 0.2 & 0.15 & \ldots \end{bmatrix}_{6 \times 24}

Bias \mathbf{b}_1 = [0.1, -0.05, 0.2, 0.15, 0.08, -0.12, \ldots] (24 values)

How matrix multiplication works:

To get hidden neuron h_1 (first neuron), we take the dot product of our input with the first column of \mathbf{W}_1:

\begin{align} h_1 &= [0.25, -0.31, 0.42, 0.18, -0.09, 0.33] \cdot [0.1, 0.2, -0.1, 0.3, 0.15, -0.2] + 0.1 \\ &= (0.25)(0.1) + (-0.31)(0.2) + (0.42)(-0.1) + (0.18)(0.3) + (-0.09)(0.15) + (0.33)(-0.2) + 0.1 \\ &= 0.025 - 0.062 - 0.042 + 0.054 - 0.0135 - 0.066 + 0.1 \\ &= -0.0045 \end{align}

For hidden neuron h_2 (second neuron), we use the second column:

\begin{align} h_2 &= [0.25, -0.31, 0.42, 0.18, -0.09, 0.33] \cdot [-0.2, 0.3, 0.1, -0.15, 0.25, 0.1] + (-0.05) \\ &= (0.25)(-0.2) + (-0.31)(0.3) + (0.42)(0.1) + (0.18)(-0.15) + (-0.09)(0.25) + (0.33)(0.1) - 0.05 \\ &= -0.05 - 0.093 + 0.042 - 0.027 - 0.0225 + 0.033 - 0.05 \\ &= -0.1675 \end{align}

For hidden neuron h_3:

h_3 = [0.25, -0.31, 0.42, 0.18, -0.09, 0.33] \cdot [0.3, -0.1, 0.2, 0.1, -0.2, 0.3] + 0.2

For hidden neuron h_4:

h_4 = [0.25, -0.31, 0.42, 0.18, -0.09, 0.33] \cdot [0.15, 0.25, -0.15, 0.2, 0.1, -0.1] + 0.15

Continuing this process for all 24 neurons (we'll abbreviate the rest rather than dump 24 nearly identical dot products):

\text{Hidden layer} = [-0.0045, -0.1675, \ldots, 0.156, -0.089, 0.213, -0.134, 0.067, 0.189, \ldots]

(24 values total)

Step 2: Apply ReLU activation

The Bouncer at a Club Analogy

ReLU (Rectified Linear Unit) is actually very simple! It's like a bouncer at a club:

\text{ReLU}(x) = \max(0, x)

The rule: "If the number is positive, let it through. If it's negative, block it (set to zero)."

Imagine a nightclub bouncer checking IDs:

  • Person shows ID with age 25 → Bouncer: "You're over 21, come in!" (keeps 25)
  • Person shows ID with age 18 → Bouncer: "Sorry, under 21, can't enter!" (blocks, becomes 0)
  • Person shows ID with age 30 → Bouncer: "Welcome!" (keeps 30)
  • Person shows ID with age -5 → Bouncer: "That doesn't even make sense, blocked!" (becomes 0)

ReLU does the same with numbers:

ReLU(5) = max(0, 5) = 5   ← Positive, keep it!
ReLU(-3) = max(0, -3) = 0 ← Negative, block it!
ReLU(0.5) = max(0, 0.5) = 0.5 ← Positive, keep it!
ReLU(-100) = max(0, -100) = 0 ← Negative, block it!

It's literally just: "Compare the number to zero, keep whichever is bigger."

Why do this? This introduces non-linearity—the network can learn complex, conditional patterns like "if this feature is active (positive), do X, otherwise do nothing (zero)." Without this, the entire network would just be fancy addition and multiplication, which can only learn simple patterns!

Example with our numbers:

This "activates" only positive neurons, introducing non-linearity. We literally compare each number to zero and keep the larger value:

\begin{align} &\text{ReLU}([-0.0045, -0.1675, 0.294, 0.0775, 0.156, -0.089, 0.213, -0.134, 0.067, 0.189, \ldots]) \\ &= [0, 0, 0.294, 0.0775, 0.156, 0, 0.213, 0, 0.067, 0.189, \ldots] \end{align}

Notice how all negative values became 0! This creates sparsity—only about half the neurons are active. This is actually good:

  • Different inputs activate different neurons (specialization)
  • Sparse networks are easier to interpret
  • Computational efficiency (skip zero neurons in hardware)

Why ReLU?

  • Allows non-linear transformations (essential for complex patterns)
  • Computationally cheap (just compare to zero)
  • Prevents vanishing gradients (gradient is either 0 or 1)
  • Models stacked linear layers without activation would collapse to a single linear layer!

Step 3: Contract to 6 dimensions

Weight matrix \mathbf{W}_2 (24×6), bias \mathbf{b}_2 (6 values)

Result (after calculation):

\text{FFN output} = [0.41, -0.18, 0.55, 0.29, -0.07, 0.38]

Why FFN After Attention?

The attention and feed-forward layers serve complementary roles:

Attention (communication): Gathers information from other positions. Each word asks "What did everyone else say that's relevant to me?" and collects contextual information. This is a mixing operation—information flows between positions.

FFN (processing): Takes that gathered information and processes it independently at each position. Each word thinks "Given everything I just learned, what patterns do I recognize?" This is position-independent—no communication between words, just deep thinking at each location.

Think of it like a group project meeting:

  • During the meeting (Attention): Everyone shares updates, discusses, and exchanges information. You're gathering context from teammates.
  • After the meeting (FFN): You go back to your desk and independently process what you learned. You think "Based on what I heard, I should update my approach." Each team member does their own processing simultaneously but separately.

Or in everyday life:

  • Attention = Listening to a lecture: The professor explains, you gather information from their words
  • FFN = Taking notes and reflecting: After absorbing the information, you process it in your own mind, making connections and understanding

This two-stage process—gather then process—repeats at every layer, with each layer building more sophisticated understanding. Early layers might gather basic grammatical information and process it to recognize parts of speech. Later layers gather semantic relationships and process them to understand abstract concepts and reasoning.

An important architectural detail: The FFN contains roughly two-thirds of all parameters in a transformer! In GPT-3, the attention mechanisms get about 58 billion parameters, while the FFN layers get about 117 billion parameters. The FFN is where most of the "knowledge" and "reasoning" capacity lives.

Modern variations: While we use simple ReLU activation, newer transformers experiment with more sophisticated activations:

  • GLU (Gated Linear Units): Uses one linear layer to gate another, allowing more nuanced control
  • SwiGLU: Combines Swish activation (smooth version of ReLU) with GLU gating
  • GeGLU: Uses GELU activation (Gaussian Error Linear Unit) with GLU

These variants often improve performance by allowing the network to learn more complex, context-dependent transformations. But the core idea—expand to higher dimension, apply non-linearity, contract back—remains the same.


Course navigation: Previous: Chapter 6 - Dropout | Next: Chapter 8 - Residual Connections & Layer Normalization