DL0045 Dimension in FFN

In Transformers, why does the feed-forward network expand the hidden dimension (e.g.,  d_{\text{model}} 4 d_{\text{model}} ) before reducing it back?

Answer

The FFN expands each token’s representation from d_{\text{model}} to 4 d_{\text{model}} to create a wide nonlinear workspace: in the higher-dimensional space the activation function can carve out far richer feature combinations than the residual stream’s width allows. It then projects back down so the result matches the residual stream for the skip connection and the next sub-layer. It is a bottleneck design that buys expressiveness where it is cheap while keeping the inter-layer interface narrow.

(1) Extra Capacity: The wider hidden layer holds more features; the FFN is where most of a block’s parameters (8 d^2, about two-thirds) live.
(2) Nonlinear Mixing: The activation (ReLU/GELU/SwiGLU) acts in the expanded space, letting the network represent sparse, high-order interactions that a d-wide layer cannot.
(3) Projection Back: Returning to d_{\text{model}} keeps residual compatibility and a uniform interface across all layers.

Mathematical Formulation:
\mathrm{FFN}(x) = W_2\, \sigma(W_1 x + b_1) + b_2

Where:

  • x \in \mathbb{R}^{d_{\text{model}}} is the per-token input from the residual stream.
  • W_1 \in \mathbb{R}^{4 d_{\text{model}} \times d_{\text{model}}} expands the dimension, \sigma is the nonlinearity, and W_2 \in \mathbb{R}^{d_{\text{model}} \times 4 d_{\text{model}}} projects back down.
Feed-forward network flow: input of width d-model expands through linear W1 to 4 times d-model, passes an activation, then linear W2 reduces it back to d-model output.

Figure 1: The FFN hourglass: expand d \to 4d, activate in the wide space, reduce 4d \to d to rejoin the residual stream.

Empirical Note: The 4x factor dates to the original Transformer and was kept by BERT/GPT because it reliably improves loss; modern SwiGLU models (LLaMA) use ~2.7x with three matrices so the parameter count stays comparable.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *