Module 10 — Feed Forward Network
The Feed Forward Network (FFN) is the second major component of every Transformer block after Multi-Head Attention.
Unlike Self-Attention, which allows tokens to communicate with each other, the Feed Forward Network processes each token independently using the same neural network.
Every token passes through two linear layers separated by a non-linear activation function (typically GELU in modern Transformers).
Topics
- Dense Layer
- Hidden Layer
- GELU
- Output Projection
1. Feed Forward Network
The complete Feed Forward Network consists of
- Input Projection
- Hidden Layer
- GELU Activation
- Output Projection
The complete equation is
2. Input
Suppose the input embedding is
where
- = Model dimension
3. First Dense Layer
The first linear layer expands the embedding dimension.
Formula
where
Output
Typically
Example
Transformer Base
4. Hidden Layer
The hidden representation is
This higher-dimensional space allows the model to learn more expressive features.
5. GELU Activation
Modern Transformer models use the Gaussian Error Linear Unit (GELU).
Exact Formula
where
is the cumulative distribution function of the standard normal distribution.
Approximation
Activated hidden layer
6. Output Projection
The activated hidden representation is projected back to the model dimension.
Formula
where
Output
7. Complete FFN Equation
Combining both dense layers and GELU activation,
This is the standard Feed Forward Network used in the original Transformer architecture.
8. Matrix Dimensions
Suppose
- Sequence Length =
- Model Dimension =
- Hidden Dimension =
Input
First Layer
Hidden Output
Second Layer
Final Output
9. Parallel Computation
Unlike recurrent neural networks, the Feed Forward Network is applied independently to every token.
For a sequence
the output is
This enables efficient parallel computation on GPUs.
10. Why Feed Forward Networks Matter
The Feed Forward Network
- Increases model capacity
- Learns non-linear feature transformations
- Processes each token independently
- Improves representation learning
- Complements the attention mechanism
It is an essential component of
- Transformer
- BERT
- RoBERTa
- ALBERT
- GPT
- GPT-2
- GPT-3
- GPT-4
- T5
- Vision Transformer (ViT)
- LLaMA
- Gemma
- Mistral
- Qwen
- DeepSeek
- Phi
- Modern Large Language Models (LLMs)
Summary
| Concept | Formula |
|---|---|
| First Dense Layer | |
| Hidden Layer |