Leaky ReLU: A Small Slope Below Zero

Activation FunctionsEasy+10 XP+10

Leaky ReLU: A Small Slope Below Zero

Plain ReLU outputs zero for every negative input, and its gradient there is zero too. A unit that drifts negative across the whole training set stops receiving gradient entirely and never recovers — a dead neuron.

Leaky ReLU leaves a small slope on the negative side so something always flows back:

1f(x) = x if x > 0
2f(x) = alpha*x otherwise

Task: write leaky_relu(values, alpha) applying this element-wise, rounded to 4 decimal places.

  • alpha is typically small, around 0.01, but your code shouldn't assume any particular value.
  • Exactly zero takes the second branch — alpha * 0 is 0 either way.

The leak is a slope, not an offset. Negative inputs stay negative and stay ordered, so the unit keeps some information about how negative its input was rather than collapsing it all to a single zero.

Examples

values = [-2.0, -0.5, 0.0, 1.5, 3.0]
alpha = 0.01
returns [-0.02, -0.005, 0.0, 1.5, 3.0]
values = [-10.0, 10.0]
alpha = 0.1
returns [-1.0, 10.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