We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Activations step 7 of 9
GLU Activation
A Gated Linear Unit combines two projections of the same input. One produces values; the other controls how much of each value passes through.
In this exercise, w is the gate matrix and v is the value matrix:
gate = sigmoid(x @ w)
value = x @ v
output = gate * value
The @ operations are matrix products. The final multiplication is elementwise. Sigmoid maps gate inputs to numbers between zero and one, so a gate near zero suppresses its value and a gate near one preserves it. At a gate input of zero, sigmoid is 0.5, so the corresponding value is halved.
For example, let x=[2.0, -4.0], w be a 2-by-2 zero matrix, and v be the 2-by-2 identity matrix. The gate is [0.5, 0.5], the value projection is [2.0, -4.0], and the result is [1.0, -2.0]. A negative value can remain negative.
Your task
Implement glu(x, w, v) as sigmoid(x @ w) * (x @ v), without bias terms.
- x has shape (..., d_in), where ... represents any leading batch dimensions.
- w has shape (d_in, d_out) and produces the gate inputs.
- v has shape (d_in, d_out) and produces the values.
- Return shape (..., d_out), preserving x's leading dimensions.
Do not use torch.nn.functional.glu or jax.nn.glu. Those convenience functions split an already-projected input into halves; this exercise instead supplies the two projection matrices explicitly. Sigmoid itself is allowed.
The curve below shows only the sigmoid gate, not the complete GLU mapping.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Multiply each feature by a learned gate
A gated layer computes two branches from the same input: values to carry forward and coefficients that control their contribution. With a sigmoid gate,
$$g=\sigma(xW_g),\qquad v=xW_v,\qquad h=g\odot v.$$
The symbol odot means elementwise multiplication. The branch shapes must be
compatible; this is not a matrix product between g and v.
For values [2, -3] and gates [0.25, 0.8], the output is [0.5, -2.4].
A sigmoid gate near zero suppresses its feature. A gate near one passes most of
that feature through. Each feature has its own gate value rather than sharing
one decision for the entire example.
SwiGLU uses a different gate function
SwiGLU uses SiLU, a * sigmoid(a), in its gate branch. Unlike sigmoid, SiLU is
not limited to [0, 1]. Its output can amplify a value or reverse its sign.
Some tasks ask you to project an input into two branches. Others supply a tensor that already contains both branches and ask you to split the last axis. Follow the Problem tab's input contract and branch order.
When computing derivatives, include both branches: changing the input can change both the gate and the value. In a transformer's feed-forward layer, an output projection usually converts the gated hidden features back to the width expected by the residual connection.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.