Skip to content

← Activations step 1 of 9

Easy Primitives

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…