liveBook · Manning

GELU

As explained in Learn Generative AI with PyTorch

A smooth activation function used in neural networks to transform layer outputs and improve model performance.

GELU (Gaussian error linear unit) is a smooth activation function used in Transformer feed-forward networks. It combines linear and nonlinear behavior to make more nuanced adjustments during training and can improve performance, especially on language tasks. section 11.2 section 11.2.2

Compared with ReLU, GELU is differentiable everywhere rather than having a kink. Its smoother gradient provides a more continuous signal for backpropagation and may optimize the network more effectively. section 11.2.2

Implementing GELU in PyTorch

Define GELU as an nn.Module and apply its tanh-based approximation to an input tensor x. Plotting its output beside ReLU makes the difference between the smooth curve and ReLU's kink visible. section 11.2.2

class GELU(nn.Module):
   def forward(self, x):
       return 0.5*x*(1.0+torch.tanh(math.sqrt(2.0/math.pi)*\
                      (x + 0.044715 * torch.pow(x, 3.0))))

Inspect the compact PyTorch implementation, including the cubic term, scaling factor, and tensor-valued forward computation. section 11.2.2

Use GELU in the feed-forward sublayer of decoder blocks for GPT-style text models and music Transformers. The same activation pattern appears across models with different layer counts, embedding sizes, vocabularies, and sequence lengths. section 12.3.1 section 14.3.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines GELU, contrasts its differentiability with ReLU, and provides PyTorch code and a comparison plot without deriving the function.
Examples
Implements GELU in PyTorch, plots it against ReLU, and applies it in GPT-style text and music Transformers.
What it assumes
Assumes familiarity with neural-network activations, ReLU, tensors, PyTorch modules, and Transformer decoder architecture.
What you can do after
Builds a GELU module in PyTorch and places it within Transformer decoder feed-forward networks.
How it connects
Connects GELU with ReLU, feed-forward networks, decoder blocks, causal self-attention, backpropagation, and Transformer 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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.