Nonlinear intermediate activation
Transformer and feed-forward networks
PyTorch · Class
nn.GELU is a Gaussian Error Linear Units activation module. It supports the default formulation or a tanh-based approximation and preserves the input shape.
GELU transforms each input element using the Gaussian cumulative distribution function. Setting approximate="tanh" uses the documented tanh-based estimate instead of the default exact formulation; the module preserves the input shape.
class torch.nn.GELU(approximate='none')| Name | Type | Description |
|---|---|---|
approximate | str, optional | Selects the GELU approximation algorithm; accepted values are "none" and "tanh", with "none" as the default. |
A Tensor with the same shape as the input.
import torch
from torch import nn
m = nn.GELU()
x = torch.randn(2)
y = m(x)
print(y.shape)Creates the default GELU module, applies it element-wise to a two-element tensor, and prints the unchanged output shape.
Transformer and feed-forward networks
Batched or sequence tensor processing
Performance-sensitive model variants
nn.GELU is an activation module that applies GELU element-wise. With the default setting, it computes x times the Gaussian cumulative distribution function at x.
Follow-up: What does approximate="tanh" change? It replaces the default formulation with the documented tanh-based estimate involving x and x cubed.
The approximate argument selects the algorithm and accepts either "none" or "tanh". It is optional and defaults to "none".
Follow-up: How would you select the approximation? Construct the module as nn.GELU(approximate="tanh").
The input may have any number of dimensions, represented by (*). The output retains exactly the same shape, while forward returns a Tensor.
Follow-up: Can it process a batch or sequence tensor? Yes; those dimensions are permitted because the documented input shape allows any number of dimensions.
torch.nn.functional.gelutorch.nn.modules.activation.GELUtorch.nn.TransformerEncoderLayer