EasyActivation Functions

ELU Activation

Activation Functions

Easy

Problem

The Exponential Linear Unit (ELU) is an activation function that pushes mean activations closer to zero, which speeds up learning. Unlike ReLU, ELU produces a smooth curve for negative inputs, reducing the impact of the vanishing gradient problem.

Given a list of values and a parameter alpha, apply the ELU activation to each element.

Formula

ELU(x) = x \quad \text{if } x > 0

ELU(x) = \alpha \cdot (e^x - 1) \quad \text{if } x \le 0

The parameter alpha controls the negative saturation value. As x approaches negative infinity, ELU(x) approaches -alpha.

Theory

ReLU (\max(0, x)) revolutionized deep learning, but it has two issues:

  1. Dead neurons: when the input is negative, the output and gradient are both zero. Neurons can die permanently and never recover.
  2. Non-zero mean outputs: ReLU only outputs zero or positive values, so its average output is always positive. This introduces a bias shift that can slow down learning in deeper layers.

Leaky ReLU fixes the dead neuron problem by allowing a small slope for negative inputs (\alpha x). But Leaky ReLU's negative side is just a straight line, which does not address the mean shift issue.


ELU: Exponential Curve for Negatives

ELU (Exponential Linear Unit) takes a different approach to the negative side. Instead of a straight line, it uses an exponential curve that smoothly saturates at -\alpha:

\text{ELU}(x) = \begin{cases} x & \text{if } x > 0 \\ \alpha \cdot (e^x - 1) & \text{if } x \leq 0 \end{cases}

Some values to build intuition (with \alpha = 1.0):

The negative side curves smoothly from 0 toward -\alpha and stays there.


Why the Exponential Shape Helps

The exponential curve has three benefits over both ReLU and Leaky ReLU:

1. Mean activations closer to zero

Since ELU outputs negative values that saturate at -\alpha, the average output across a layer is closer to zero than with ReLU. This is important because:

2. No dead neurons

For any negative input x, the gradient is:

\frac{d}{dx} \text{ELU}(x) = \alpha \cdot e^x

This is always positive (never zero), so gradients always flow. Unlike ReLU, neurons cannot die.

3. Smooth transition at zero

ELU is continuous and differentiable everywhere, including at x = 0. There is no sharp corner like ReLU has. The smooth curve means:


The Alpha Parameter

\alpha controls the negative saturation value:

As x \to -\infty, ELU approaches -\alpha. So \alpha directly sets the floor for the negative output.


ELU vs. ReLU vs. Leaky ReLU

At x = -1 (with \alpha = 1.0 for ELU, \alpha = 0.01 for Leaky ReLU):

Key differences:


Where ELU Shows Up

Examples

Example 1

Input
x = [1, -1, 0, 2, -0.5], alpha = 1
Output
[1, -0.6321205588, 0, 2, -0.3934693403]
Explanation
Positive values pass through, while nonpositive values use the exponential branch.

Example 2

Input
x = [-1, -2, -3], alpha = 2
Output
[-1.2642411177, -1.7293294335, -1.9004258633]

Hints

  1. Use the original value when it is positive.
  2. Use alpha times exp(value) minus one for the nonpositive branch.

Requirements

Constraints

Starter Code

import math

def elu(x: list, alpha: float = 1.0) -> list:
    """
    Returns ELU applied elementwise to the input values.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Mixed valuespublic
Alpha=2public