We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Activations step 4 of 9
Implement Leaky ReLU
Leaky ReLU keeps positive inputs unchanged and multiplies negative inputs by a slope called alpha. Unlike ReLU, it can retain a small negative output instead of setting every negative value to zero.
output = x when x > 0
output = alpha * x otherwise
With alpha=0.01, input [-2.0, -1.0, 0.0, 1.0, 2.0] becomes [-0.02, -0.01, 0.0, 1.0, 2.0]. With alpha=0.2, an input of -3.0 instead becomes -0.6. Alpha controls the negative-side slope; it does not scale the positive side.
Your task
Implement leaky_relu(x, alpha=0.01) for a floating-point tensor of any shape. Use the supplied scalar alpha when present and the default 0.01 otherwise. Return a tensor with the same shape, applying the rule independently to each element. Zero remains zero in either branch.
Use basic operations such as an elementwise where. Do not call torch.nn.functional.leaky_relu or jax.nn.leaky_relu. A tensor condition selects values for every element; a Python if cannot make a separate choice for every entry of a multi-element tensor.
The visualization uses alpha=0.01 and compares it with ReLU. Tests can supply a different alpha, so do not hard-code the slope from the plot.
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.