We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Activations step 1 of 9
Implement ReLU
ReLU stands for Rectified Linear Unit. It applies the same simple rule to each element: keep positive values and replace negative values with zero. Zero stays zero.
For input [-2.0, -1.0, 0.0, 1.0, 2.0], the result is [0.0, 0.0, 0.0, 1.0, 2.0]. In a matrix or higher-dimensional tensor, apply that rule independently without changing the shape.
Your task
Implement relu(x) for a floating-point tensor. Return an elementwise maximum of x and zero, preserving its shape. Build it from basic tensor operations. Do not call torch.relu, torch.nn.functional.relu or jax.nn.relu.
This exercise checks forward values; it does not specify a derivative at the nondifferentiable point x=0.
The curve below shows the elementwise input-output relationship.
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?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.