We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Activations step 5 of 9
GELU Activation
GELU stands for Gaussian Error Linear Unit. Its exact definition is x times the standard normal cumulative distribution function at x. That multiplier lies between zero and one: positive inputs are mostly preserved when large, while large negative inputs approach zero from below.
This exercise uses the tanh approximation, not the exact normal-CDF formula. Compute the following elementwise expression, where ** means exponentiation:
0.5 * x * (1 + tanh(sqrt(2 / pi) * (x + 0.044715 * x**3)))
For x=[-1.0, 0.0, 1.0], this gives approximately [-0.158808, 0.0, 0.841192]. Unlike ReLU, GELU does not replace every negative input with zero.
Your task
Implement gelu(x) using the tanh formula above for a floating-point tensor of any shape. Return a tensor with the same shape. Use basic arithmetic and tanh rather than torch.nn.functional.gelu or jax.nn.gelu.
Compute the scalar constant sqrt(2 / pi) once in your expression. In Python, write x**3 for the cube; x^3 is a different operator.
The visualization compares GELU with ReLU. The expected values for this exercise specifically follow the tanh approximation, which differs slightly from exact GELU.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Nonlinear activations let a network learn more than a linear map
An affine layer applies weights and adds a bias. Two such layers without a nonlinear activation between them can be combined into one:
$$(xW_1+b_1)W_2+b_2=x(W_1W_2)+(b_1W_2+b_2).$$
An activation adds an input-dependent response that cannot generally be absorbed into a single set of weights and biases.
Common elementwise activations
ReLU, max(0, x), keeps positive values and sets negative values to zero.
For example, [-2, 0, 3] becomes [0, 0, 3]. Its derivative is zero for negative
inputs and one for positive inputs. At zero, the ordinary derivative does not
exist, so an implementation needs a convention.
Leaky ReLU uses a small slope on the negative side instead of setting it to zero. Sigmoid maps finite real inputs to probabilities between zero and one in exact arithmetic. Tanh maps them between negative one and one. For both sigmoid and tanh, large input magnitudes lead to small derivatives, which can slow learning.
SiLU and GELU use smooth gates
SiLU is x * sigmoid(x). Its derivative includes both the direct change in x
and the change in the sigmoid factor.
GELU is x * Phi(x), where Phi(x) is the probability that a standard normal
random variable is at most x. A common implementation approximates this using
tanh. The approximation is close but not identical, so use the version specified
in the exercise.
These activations act independently on each element and preserve shape. Softmax instead combines scores along an axis so the resulting probabilities sum to one. Applying sigmoid to every class score is not the same operation.
Use stable formulas where needed. For example, computing sigmoid directly as
1 / (1 + exp(-x)) can overflow in the exponential for a large negative x,
even though the desired output is close to zero.
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.