Latent decoder › Query MLP
GELU
×no parameters
Activate each feature with the exact GELU.
- in
- [B batch, T positions, 3072 d_ff]
- out
- [B batch, T positions, 3072 d_ff]
The exact form, not the tanh approximation the denoiser’s MLP uses.
Explanation
is the cumulative distribution function of the standard Gaussian.
Codeactivation.py 669–709
class GELU(Module):
r"""Applies the Gaussian Error Linear Units function.
.. math:: \text{GELU}(x) = x * \Phi(x)
where :math:`\Phi(x)` is the Cumulative Distribution Function for Gaussian Distribution.
When the approximate argument is 'tanh', Gelu is estimated with:
.. math:: \text{GELU}(x) = 0.5 * x * (1 + \text{Tanh}(\sqrt{2 / \pi} * (x + 0.044715 * x^3)))
Args:
approximate (str, optional): the gelu approximation algorithm to use:
``'none'`` | ``'tanh'``. Default: ``'none'``
Shape:
- Input: :math:`(*)`, where :math:`*` means any number of dimensions.
- Output: :math:`(*)`, same shape as the input.
.. image:: ../scripts/activation_images/GELU.png
Examples::
>>> m = nn.GELU()
>>> input = torch.randn(2)
>>> output = m(input)
"""
__constants__ = ['approximate']
approximate: str
def __init__(self, approximate: str = 'none') -> None:
super().__init__()
self.approximate = approximate
def forward(self, input: Tensor) -> Tensor:
return F.gelu(input, approximate=self.approximate)
def extra_repr(self) -> str:
return f'approximate={repr(self.approximate)}'
Sources2
Direct evidence
The Perceiver blocks’ MLP: LayerNorm, an affine layer, exact GELU, dropout, an affine layer.
The implementation, pinned at revision e18c42acdfb7, not yet public
Direct evidence
nn.GELU defaults to approximate=none and evaluates x times the standard Gaussian CDF.
torch-exact-gelu · pytorch/pytorch / torch/nn/modules/activation.py
Pinned revision ee1b6804381c ↗ · lines 669–709
Read code excerpt
class GELU(Module):
r"""Applies the Gaussian Error Linear Units function.
.. math:: \text{GELU}(x) = x * \Phi(x)
where :math:`\Phi(x)` is the Cumulative Distribution Function for Gaussian Distribution.
When the approximate argument is 'tanh', Gelu is estimated with:
.. math:: \text{GELU}(x) = 0.5 * x * (1 + \text{Tanh}(\sqrt{2 / \pi} * (x + 0.044715 * x^3)))
Args:
approximate (str, optional): the gelu approximation algorithm to use:
``'none'`` | ``'tanh'``. Default: ``'none'``
Shape:
- Input: :math:`(*)`, where :math:`*` means any number of dimensions.
- Output: :math:`(*)`, same shape as the input.
.. image:: ../scripts/activation_images/GELU.png
Examples::
>>> m = nn.GELU()
>>> input = torch.randn(2)
>>> output = m(input)
"""
__constants__ = ['approximate']
approximate: str
def __init__(self, approximate: str = 'none') -> None:
super().__init__()
self.approximate = approximate
def forward(self, input: Tensor) -> Tensor:
return F.gelu(input, approximate=self.approximate)
def extra_repr(self) -> str:
return f'approximate={repr(self.approximate)}'