Gated Linear Units › The GLU Formulation
Language AI HandbookAll chapters Gated Linear Units › The GLU FormulationOne section · Sections in this chapter3 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Now that we understand what gating does, let's formalize how it works mathematically. The Gated Linear Unit was introduced by Dauphin et al. in 2017 for language modeling, and its elegance lies in a simple but powerful insight: instead of applying an activation function to transform features, let the network learn which features to keep and which to suppress.
The Core Idea: Two Pathways, One Output
Standard neural network layers compute a single transformation of their input and apply an activation function. GLU takes a fundamentally different approach: it computes two separate linear projections of the same input and combines them through multiplication. One projection produces the "values" we want to transmit, while the other produces "gates" that control how much of each value passes through.
Think of it like a mixing console in a recording studio. Each channel has both a signal (the audio content) and a fader (the volume control). The final output for each channel is the signal multiplied by the fader position. The GLU works the same way, but learns both the signals and the fader positions from data.
The Mathematical Formulation
The original GLU formula captures this two-pathway structure:
Let's unpack each component to understand what it contributes:
The Value Pathway:
The first term computes a linear transformation of the input. This is the "content" that we might want to pass through to the next layer. Without the gate, this would just be a standard linear layer:
- : the input vector (a token's representation)
- : the "value" projection weights that transform the input
- : the value projection bias
The Gate Pathway:
The second term computes another linear transformation of the same input, but passes it through the sigmoid function to produce gate values:
- : the "gate" projection weights (separate from )
- : the gate projection bias
- : the sigmoid activation function, which squashes values to the range
- : the output dimension of each projection
The Combination: (Element-wise Multiplication)
The symbol denotes the Hadamard product, which simply multiplies corresponding elements. If the value pathway produces and the gate pathway produces , the output is .
Why Sigmoid for Gating?
The sigmoid function produces values between 0 and 1, making it a natural choice for gating:
where is the input value (applied element-wise when the input is a vector). The sigmoid's behavior at extreme values is what makes it effective as a gate:
- As , (gate closes completely)
- As , (gate opens fully)
- At , (gate is half-open)
When is close to 0, the corresponding element of is blocked, meaning that dimension contributes nothing to the output. When it's close to 1, the element passes through unchanged. Values between 0 and 1 allow partial transmission, giving the network fine-grained control over information flow.

From Theory to Code
Let's implement the basic GLU and see these components in action:
def glu(x, W, b, V, c):
"""
Gated Linear Unit.
Args:
x: Input vector, shape (d_model,) or (batch, d_model)
W: Value projection weights, shape (d_model, d)
b: Value projection bias, shape (d,)
V: Gate projection weights, shape (d_model, d)
c: Gate projection bias, shape (d,)
Returns:
Output vector, shape (d,) or (batch, d)
"""
value = x @ W + b
gate = sigmoid(x @ V + c)
return value * gate
# Example dimensions
d_model = 512
d_output = 256
# Initialize weights
W = np.random.randn(d_model, d_output) * np.sqrt(2.0 / (d_model + d_output))
b = np.zeros(d_output)
V = np.random.randn(d_model, d_output) * np.sqrt(2.0 / (d_model + d_output))
c = np.zeros(d_output)
# Test with a single input
x_test = np.random.randn(d_model)
y_glu = glu(x_test, W, b, V, c)GLU basic example: Input dimension: 512 Output dimension: 256 Input shape: (512,) Output shape: (256,) Output range: [-1.7685, 2.3423]
The output range shows that GLU produces both positive and negative values, unlike ReLU which only produces non-negative outputs. This is because the sigmoid gate modulates but doesn't constrain the sign of the value pathway.
The Dimension Trade-off
Notice something important about GLU's output dimension. If we project from to , we get outputs, not . This is because we use two projections (value and gate), each producing dimensions, and multiply them together element-wise to get outputs. The multiplication combines the two pathways rather than concatenating them.
This has clear implications for parameter efficiency that we'll explore shortly. First, let's see how GLU fits into the larger transformer architecture.