Leaky ReLU activationEasynumpyactivationneural-netrelu
Leaky ReLU activation
Background
Leaky ReLU fixes ReLU's "dying neuron" problem by letting a small gradient flow for negative inputs: instead of zeroing them, it scales them by a small slope . This keeps every unit slightly active so it can recover during training.
Problem statement
Implement leaky_relu(x, alpha=0.01):
Input
x—np.ndarray: input (any shape).alpha—float: the negative slope (default 0.01).
Output
Returns an np.ndarray of the same shape.
Examples
Example 1
Input: x = [-2, -0.5, 0, 3], alpha = 0.1
Output: [-0.2, -0.05, 0.0, 3.0]
Explanation: positive inputs pass through unchanged; negatives are scaled by , so and .
Constraints
- Positive (and zero) inputs are unchanged; negatives are multiplied by
alpha. - Elementwise; preserves shape.
- recovers plain ReLU.
Notes
- Unlike ReLU, the negative slope keeps gradients non-zero everywhere, so a unit pushed negative is never permanently dead.
- PReLU makes a learnable per-channel parameter; Leaky ReLU keeps it fixed (commonly 0.01).
Python
Loading...
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example
- •Positive inputs pass through unchanged
- •alpha = 0 recovers ReLU
- •Negatives are scaled by alpha