ReLU, Vectorised

Activation FunctionsEasy+10 XP+10

ReLU, Vectorised

ReLU is the activation function that made deep networks trainable, and it is almost embarrassingly simple: keep positive numbers, flatten everything else to zero.

relu(x) = max(0, x)

Task: write relu(x) where x is a list of numbers. Return a list where every negative entry has become 0.0 and everything else is unchanged.

  • Do it elementwise across the whole list at once — reach for np.maximum rather than a Python loop.
  • An empty list in means an empty list out.

ReLU

ReLU(x) = max(0, x) — passes positives through, clips negatives.

x2.0
ReLU(2.0) = 2.0

Examples

x = [1.0, -2.0, 0.0, 3.5]
returns [1.0, 0.0, 0.0, 3.5]
x = [-5.0, -0.1]
returns [0.0, 0.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run