liveBook · Manning

GELU

As explained in Build a Large Language Model (From Scratch)

A smooth activation function defined as x times the Gaussian CDF, used here in transformer feed-forward submodules.

GELU, or Gaussian error linear unit, is a smooth activation function defined as GELU(x) = x·Φ(x), where Φ is the standard Gaussian cumulative distribution function. Practical implementations commonly use a cheaper approximation. section 4.3

In a GPT transformer block, GELU sits between two linear layers in the feed-forward network. The first expands each token representation, the activation transforms it smoothly, and the second projects it back to the embedding size. section 4.3 section 4.8

Compared with ReLU, GELU:

  • Provides a smooth nonlinear response rather than a piecewise-linear cutoff. section 4.3
  • Maintains a non-zero gradient across almost all negative inputs, supporting more nuanced parameter updates. section 4.3

Feed-forward shape flow

With an input shaped [2, 3, 768], a GPT-style feed-forward module expands the final dimension to 3,072, applies GELU, and contracts it back to [2, 3, 768]. section 4.3

Listing 4.3 An implementation of the GELU activation function
class GELU(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        return 0.5 * x * (1 + torch.tanh(
            torch.sqrt(torch.tensor(2.0 / torch.pi)) * 
            (x + 0.044715 * torch.pow(x, 3))
        ))

Shows a PyTorch GELU module using the computationally cheaper tanh-based approximation. section 4.3

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Defines the Gaussian-CDF form, implements a tanh approximation, compares ReLU behavior, and traces feed-forward tensor shapes.
Examples
Uses PyTorch code, GELU-versus-ReLU plots, and a [2, 3, 768] feed-forward shape example.
What you can do after
Build it from scratch as a PyTorch module and place it correctly in a GPT-style feed-forward network.
How it connects
Connects GELU with ReLU, SwiGLU, PyTorch, feed-forward networks, transformer blocks, and GPT models.

Also explained in

Compare how 2 books explain GELU →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.