FFN Activation Functions › Why Nonlinearity Is Non-Negotiable

Michael BrenndoerferUpdated June 14, 20253 min read
Language AI HandbookAll chapters
Part 15 of 67Transformer Blocks
Chapter 108FFN Activation Functions › Why Nonlinearity Is Non-Negotiable
Reading section 2 of 15 · Why Nonlinearity Is Non-Negotiable · 3 minSection 2 of 15 · 3 min

Book contents

Section 2 of 15 · 3 min read

FFN Activation Functions › Why Nonlinearity Is Non-NegotiableOne section · Sections in this chapter2 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Before examining specific activation functions, it is worth building a deeper intuition for why nonlinearity matters so much. The feed-forward sublayer in a transformer takes a vector of dimension dmodeld_{\text{model}}, projects it up to a higher-dimensional space of size dffd_{\text{ff}}, applies an activation, and then projects back down. In mathematical terms, a two-layer FFN computes:

FFN(x)=W2⋅act(W1x+b1)+b2\text{FFN}(\mathbf{x}) = W_2 \cdot \text{act}(W_1 \mathbf{x} + b_1) + b_2

where:

  • x∈Rdmodel\mathbf{x} \in \mathbb{R}^{d_{\text{model}}}: the input vector from the attention sublayer
  • W1∈Rdff×dmodelW_1 \in \mathbb{R}^{d_{\text{ff}} \times d_{\text{model}}}: the first projection (expands dimensionality)
  • b1∈Rdffb_1 \in \mathbb{R}^{d_{\text{ff}}}: the first bias vector
  • act(⋅)\text{act}(\cdot): the activation function applied elementwise
  • W2∈Rdmodel×dffW_2 \in \mathbb{R}^{d_{\text{model}} \times d_{\text{ff}}}: the second projection (reduces dimensionality back)
  • b2∈Rdmodelb_2 \in \mathbb{R}^{d_{\text{model}}}: the second bias vector

If act\text{act} were the identity function (no nonlinearity), the composition W2⋅(W1x+b1)+b2W_2 \cdot (W_1 \mathbf{x} + b_1) + b_2 would simplify to W2W1x+W2b1+b2W_2 W_1 \mathbf{x} + W_2 b_1 + b_2, which is just a linear transformation. Stacking twenty such layers would still produce one linear transformation. All that computation would buy you nothing beyond a single matrix multiply.

The universal approximation theorem tells us that a network with a single hidden layer and a nonlinear activation can approximate any continuous function to arbitrary precision, given enough hidden units. The key requirement is exactly that nonlinearity. In practice, deeper networks with smaller hidden layers learn more efficiently than shallow networks with enormous hidden layers, but the theoretical foundation is the same: nonlinearity is the ingredient that maps matrix multiplication into universal function approximation.

For language modeling specifically, the FFN layers are thought to store factual knowledge. Research by Geva et al. (2021) showed that individual FFN layers function like key-value memories: the first layer's columns act as "keys" that match patterns in the input, and the second layer's rows act as "values" that contribute to the output distribution. The activation function is what allows these keys to be selective, activating strongly for certain input patterns and weakly for others. Without nonlinearity, every key would always partially match every input, making the retrieval process incoherent.

Notice that the FFN's nonlinearity is fundamentally different from attention's nonlinearity. Attention creates nonlinear interactions between positions (which tokens attend to which). The FFN creates nonlinear transformations within each position independently. Together, these two nonlinearities give the transformer its expressive power.

Historical Context: The Search for Better Activations

The history of activation functions mirrors the history of deep learning itself. Early neural networks used sigmoid and tanh activations, which saturate for large input values, causing vanishing gradients in deep networks. ReLU, introduced to the deep learning community by Nair and Hinton in 2010 and popularized by Krizhevsky et al. in AlexNet (2012), solved the vanishing gradient problem for positive inputs and enabled training of networks dozens of layers deep. The decade following ReLU saw dozens of proposed alternatives, including Leaky ReLU and PReLU as well as ELU and SELU, each addressing edge cases. GELU and SiLU represent the current generation, optimized for the specific demands of large-scale transformer training.