Overview
ReLU sets every negative input to zero, which also sets its slope to zero. ELU keeps the positive side of ReLU but replaces the flat negative side with an exponential curve. A slightly negative input gives a slightly negative output. A very negative input settles at and goes no lower. Because outputs can be negative, the average output of a layer sits closer to zero than with ReLU, and the slope on the negative side is small but never exactly zero.
A short history
Djork-Arné Clevert, Thomas Unterthiner and Sepp Hochreiter introduced ELU in a 2015 paper. ReLU outputs are never negative, so the average activation of a layer sits above zero and shifts the inputs of the next layer. ELU lets negative inputs produce small negative outputs that level off at a floor, which pulls the average closer to zero and sped up training of deep networks without batch normalisation.
How it works
- Take the input that a layer has computed for one unit.
- If , return unchanged.
- If , return . This is 0 at and approaches as falls.
- During training, multiply the incoming gradient by 1 on the positive side and by on the negative side.
Watch out for
- The exponential costs more to compute than ReLU's comparison, which matters on very small or latency-bound models.
- For very negative inputs the slope becomes tiny, so a unit pushed far below zero still learns slowly. The problem is reduced, not removed.
- With the slope jumps from to 1 at zero, so the curve has a corner there. Only gives a smooth slope.
Use cases
When to use it
Feed-forward and convolutional networks where ReLU units stop learning, or where you want activations centred near zero without adding a normalisation layer.
In industry
- Computer vision
- Convolutional image classifiers trained without batch normalisation use ELU so that layer outputs stay centred near zero.
- Reinforcement learning
- Small policy and value networks for continuous control often use ELU because a smooth curve with negative outputs trains steadily on noisy returns.
- Graph learning
- Graph attention networks apply ELU after each attention layer when classifying nodes in citation and protein graphs.
Choosing it: what must hold
- A network where a mean activation near zero helps, for example one with little or no normalisation.
- Enough compute to pay for an exponential on every negative input, which is slower than ReLU.
- Inputs scaled to a moderate range, so the negative side is used and not saturated at its floor.
When to choose something else
| If | Use instead |
|---|---|
| Speed matters most and the network already uses batch or layer normalisation | ReLU |
| You want a plain feed-forward network that keeps its own activations normalised | SELU, with LeCun normal initialisation |
| The model is a transformer | GELU |
Math foundations
The activation
Identity on the positive side, a shifted exponential on the negative side. Both pieces equal 0 at , so the curve has no jump.
The floor
As falls, goes to 0 and the output settles at . This bounded negative part is what pulls the mean activation toward zero.
Its slope
The factor backpropagation multiplies by. On the negative side it is positive for every finite , unlike ReLU's zero.
A shortcut for the negative slope
Since , the slope can be read from the output already computed. Frameworks use this to save one exponential.
Symbols
- the input to the activation (one unit's pre-activation)
- the positive constant that sets the negative floor;
alphain the config, default 1 - the exponential function
Worked by hand
Compute ELU and its slope at and with . Use .
At the output is and the slope is 0.3679, which equals the output plus . ReLU would give 0 and 0 there.
Visual example
Loading the example.
Try this
- Move across the curve and read the output and the slope together.
- Find where the slope is close to zero: a unit there has stopped learning.
Code sample
import numpy as np
from impl import ELU
from config import ELUConfig
act = ELU(ELUConfig(alpha=1.0))
y = act.forward(np.array([-1.0, 0.0, 2.0])) # [-0.6321, 0.0, 2.0]
dy = act.backward(np.array([-1.0, 0.0, 2.0])) # [0.3679, 1.0, 1.0]Read the implementation (36 lines)
"""ELU: exponential linear unit, f(x) = x for x > 0 and alpha * (exp(x) - 1) for x <= 0.
NumPy only. `forward` computes the activation, `backward` the gradient with respect to the input.
"""
from __future__ import annotations
import numpy as np
try:
from .config import ELUConfig
except ImportError: # run from the template's own folder
from config import ELUConfig
class ELU:
"""f(x) = x for x > 0, alpha * (exp(x) - 1) for x <= 0."""
def __init__(self, config: ELUConfig | None = None) -> None:
self.config = config or ELUConfig()
def forward(self, x: np.ndarray) -> np.ndarray:
"""Apply the activation element by element. x: any shape. Returns the same shape."""
x = np.asarray(x, dtype=float)
negative = self.config.alpha * np.expm1(np.minimum(x, 0.0)) # the minimum keeps exp from overflowing
return np.where(x > 0, x, negative)
def backward(self, x: np.ndarray, grad_output: np.ndarray | None = None) -> np.ndarray:
"""d f / d x at x, times `grad_output` when given (the chain rule step of backpropagation).
f'(x) = 1 for x > 0 and alpha * exp(x) for x <= 0.
"""
x = np.asarray(x, dtype=float)
negative = self.config.alpha * np.exp(np.minimum(x, 0.0))
local = np.where(x > 0, 1.0, negative)
return local if grad_output is None else local * np.asarray(grad_output, dtype=float)
__call__ = forward- Repository
- gitlab.com/designsbyduhart/solomon-templates/activation-elu
- Needs
- Python 3.10 and NumPy
- Tests
- 7, run with
python3 -m unittest -v
Practice
Build ELU(ELUConfig(alpha=1.0)) and call forward and backward on 201 points between -6 and 6. Check backward against a finite difference of forward. Then set alpha=0.5 and find the input where the slope jumps.
Check on learning
- A ReLU unit whose inputs are all negative receives no gradient. What does an ELU unit in the same position receive, and is that enough to recover?
A way to think about it
Evaluate the slope at and at . Recovery depends on how far below zero the inputs sit.
- Why might a layer whose mean output is near zero train faster than one whose outputs are all non-negative?
A way to think about it
Think about the next layer. If every input it receives is positive, the gradients for all weights of one unit share a sign, which forces zig-zag updates.
- You raise from 1 to 3. What changes in the curve, and what new problem appears at ?
A way to think about it
Compare the slope just left of zero, , with the slope just right of zero, 1. Consider what a jump in slope means for a smooth optimiser.
- When would you choose ELU over GELU or SiLU, which are also smooth and also allow negative outputs?
A way to think about it
Compare the negative tails. ELU saturates at a nonzero floor and is monotonic. GELU and SiLU return to zero and dip. Ask which behaviour your layer statistics need.