Implement Leaky ReLU
Problem Statement
Implement the Leaky ReLU activation function.
Background
Leaky ReLU fixes the "dying ReLU" problem by allowing a small gradient for negative inputs:
- if
- if
where is a small constant (commonly 0.01).
Your Task
Write a function leaky_relu(values, alpha=0.01) that applies Leaky ReLU element-wise to a list of values.
Output Format
Return a list of floats with Leaky ReLU applied to each element. Round each value to 4 decimal places.
Example
values=[-1.0, 2.0], alpha=0.1
[-0.1, 2.0]
- Inputs:
valuesis a list of real numbers, here , andalphais the slope used on the negative side (it overrides the default of ). - Element : it is not positive, so the leaky branch applies: . The value is shrunk tenfold but keeps its sign instead of becoming .
- Element : it is positive, so the function is the identity: .
- Output:
[-0.1, 2.0], one float per input, in the same order, each rounded to 4 decimal places (nothing here needs trimming). - Notice: falls in the case, giving . With the default , an input of would become . Plain ReLU would have sent it to .
Constraints
- -1000 <= x <= 1000 for each element
- 0 < alpha <= 1
- List length: 1 to 100 elements