#290Leaky ReLU activationEasyNeural NetworksActivation Functions
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...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example - mixed signs with alpha 0.1
- •Sample - alpha=0 recovers plain ReLU
- •Example - 2D input keeps shape and leaks negatives