Gated Linear Unit activation function.
Splits the input tensor into two equal halves along dim, then
applies an element-wise gate:
where and are the two halves of along
dim, is element-wise multiplication, and
is the logistic sigmoid. The sigmoid gate controls how
much information from the first half flows through, enabling the network
to learn a soft feature-selection mechanism.
Parameters
dimint= -1Dimension along which the input is split. The size along this
dimension must be even. Default:
-1.Notes
- Input: — size along
dimmust be even. - Output: — output is half the size of
the input along
dim.
GLU was introduced for language modelling with convolutional sequence models and is also used in transformer feed-forward blocks as an alternative to ReLU/GELU projection layers.
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.GLU(dim=-1)
>>> x = lucid.tensor([[1.0, 2.0, 3.0, 4.0]]) # split into [1,2] and [3,4]
>>> m(x)
tensor([[0.9526, 1.964]])
>>> # Feed-forward block with GLU gating
>>> ff = nn.Sequential(nn.Linear(256, 512), nn.GLU(dim=-1))
>>> x = lucid.randn(4, 64, 256)
>>> out = ff(x)
>>> out.shape
(4, 64, 256)