v9.1.1
Menu

Implement Leaky ReLU

easyPROActivation FunctionsDeep LearningDeep Learning 1

Problem Statement

Implement the Leaky ReLU activation function.

Background

Leaky ReLU fixes the "dying ReLU" problem by allowing a small gradient for negative inputs:

  • f(x)=xf(x) = x if x>0x > 0
  • f(x)=α⋅xf(x) = \alpha \cdot x if x≤0x \leq 0

where α\alpha 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

Input
values=[-1.0, 2.0], alpha=0.1
Output
[-0.1, 2.0]
Reasoning
  • Inputs: values is a list of real numbers, here [−1.0, 2.0][-1.0,\ 2.0], and alpha =0.1=0.1 is the slope used on the negative side (it overrides the default of 0.010.01).
  • Element −1.0-1.0: it is not positive, so the leaky branch applies: f(−1.0)=α⋅(−1.0)=0.1×(−1.0)=−0.1f(-1.0) = \alpha \cdot (-1.0) = 0.1 \times (-1.0) = -0.1. The value is shrunk tenfold but keeps its sign instead of becoming 00.
  • Element 2.02.0: it is positive, so the function is the identity: f(2.0)=2.0f(2.0) = 2.0.
  • Output: [-0.1, 2.0], one float per input, in the same order, each rounded to 4 decimal places (nothing here needs trimming).
  • Notice: x=0x = 0 falls in the x≤0x \le 0 case, giving α⋅0=0.0\alpha \cdot 0 = 0.0. With the default α=0.01\alpha = 0.01, an input of −5.0-5.0 would become −0.05-0.05. Plain ReLU would have sent it to 00.

Constraints

  • -1000 <= x <= 1000 for each element
  • 0 < alpha <= 1
  • List length: 1 to 100 elements

Learn this in

More from Deep Learning 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.