Activation Functions
March 21, 2026 · View on GitHub
Overview & Motivation
An activation function is a non-linear, element-wise transformation applied after the affine map in each neural network layer:
Without activation functions, stacking layers would collapse into a single affine transformation — the network could only represent linear mappings regardless of depth. Activation functions are what give neural networks their expressive power.
The choice of activation function controls gradient flow during back-propagation, output range, and computational cost — all critical on resource-constrained embedded targets.
Mathematical Theory
Forward and Backward
Every activation function exposes two operations:
| Operation | Definition | Purpose |
|---|---|---|
| Forward | Transform the pre-activation | |
| Backward | Provide the local derivative for back-propagation |
The chain rule connects them during training:
Catalogue
ReLU (Rectified Linear Unit)
- Computationally cheapest (single comparison).
- Sparse activations accelerate training.
- Risk: neurons with for all inputs are permanently dead.
Leaky ReLU
where is a small positive constant (typically $0.01z < 0$.
Sigmoid
- Output range — natural for probabilities.
- Risk: saturates for , causing vanishing gradients.
Tanh (Hyperbolic Tangent)
- Output range — zero-centered, unlike Sigmoid.
- Same saturation problem as Sigmoid at extremes.
Softmax
For a vector :
- Outputs form a probability distribution ().
- Used exclusively at the output layer for multi-class classification.
- The Jacobian is a full matrix — more expensive than element-wise activations.
Complexity Analysis
| Activation | Forward (per element) | Backward (per element) | Notes |
|---|---|---|---|
| ReLU | — comparison | — comparison | Fastest |
| Leaky ReLU | — comparison + multiply | Negligible overhead vs ReLU | |
| Sigmoid | — exp + divide | — reuse forward result | Requires exp() |
| Tanh | — exp (twice) | — reuse forward result | Requires exp() |
| Softmax | — vector exp + sum | — full Jacobian | Significantly more expensive |
Step-by-Step Walkthrough
Scenario: Forward and backward pass through a 3-neuron hidden layer with ReLU, given pre-activations .
Forward:
| Neuron | ||
|---|---|---|
| 1 | $0.0$ | |
| 2 | $1.2$ | $1.2$ |
| 3 | $0.0$ | $0.0$ |
Backward with incoming gradient :
| Neuron | ||
|---|---|---|
| 1 | $0$ (dead) | $0.3 \times 0 = 0$ |
| 2 | $1$ | |
| 3 | $0$ (at boundary) | $0.1 \times 0 = 0$ |
Neuron 1 is dead — its gradient is zero and its weights will not update. If this persists across all training samples, the neuron is permanently inactive.
Pitfalls & Edge Cases
- Vanishing gradients with Sigmoid/Tanh. For deep networks, gradients shrink exponentially through saturated activations. Use ReLU or Leaky ReLU in hidden layers.
- Dead ReLU neurons. If a neuron's bias drifts negative enough that no input ever produces , it stops learning. Leaky ReLU or careful initialization (He init) prevents this.
- Softmax numerical instability. Computing directly overflows for large . Subtract before exponentiation: .
- Fixed-point range. Sigmoid outputs and Tanh outputs both fit in Q15/Q31, but the exponential intermediate values do not. Compute in floating-point and convert.
- Non-differentiable points. ReLU is technically non-differentiable at . In practice, assigning works fine.
Variants & Generalizations
| Variant | Key Difference |
|---|---|
| PReLU (Parametric ReLU) | is a learnable parameter per channel |
| ELU (Exponential LU) | for ; smooth and zero-centered |
| GELU (Gaussian Error LU) | ; used in Transformers |
| Swish / SiLU | ; smooth, non-monotonic |
| Hard Sigmoid / Hard Tanh | Piece-wise linear approximations; no transcendentals |
Applications
- Hidden layers — ReLU (or Leaky ReLU) is the default choice for feed-forward and convolutional hidden layers.
- Binary classification output — Sigmoid maps the output to a probability.
- Multi-class classification output — Softmax produces a probability distribution.
- Recurrent networks — Tanh is traditionally used in LSTM/GRU gates.
- Embedded inference — Hard Sigmoid/Tanh avoid expensive
exp()calls on MCUs without FPU.
Connections to Other Algorithms
graph TD
Act["Activation Functions"]
Layer["Dense Layer"]
NN["Neural Network"]
Loss["Loss Functions"]
Act --> Layer
Layer --> NN
NN --> Loss
| Component | Relationship |
|---|---|
| Dense Layer | Applies the activation function after the affine transformation |
| Neural Network | Activations enable the non-linear function approximation that makes deep networks useful |
| Loss Functions | The output activation must match the loss: Sigmoid + BCE, Softmax + CCE |
References & Further Reading
- Nair, V. and Hinton, G.E., "Rectified linear units improve restricted Boltzmann machines", ICML, 2010.
- Glorot, X., Bordes, A., and Bengio, Y., "Deep sparse rectifier neural networks", AISTATS, 2011.
- Clevert, D.-A., Unterthiner, T., and Hochreiter, S., "Fast and accurate deep network learning by exponential linear units (ELUs)", ICLR, 2016.