Abstract neural network with glowing activation nodes

Understanding Activation Functions in Neural Networks

Understanding Activation Functions in Neural Networks

Activation functions are small mathematical rules that let neural networks model non-linear patterns. This article unpacks their theory, practical behavior, and how to choose them — with examples, code, and tips for beginners.


Introduction — why activation functions matter

If neural networks are the engines of modern AI, activation functions are the spark plugs. They decide whether a neuron should “fire” and more importantly – inject non-linearity into the model so it can learn curved, real-world relationships. Add layers but no activations, and you still have a single linear transformation. Weirdly, depth alone won’t help unless activations do their job.


Quick intuition: what activation does

Every neuron computes a weighted sum z = W·x + b. The activation function transforms z into an output a = φ(z). That transformed output is what flows to the next layer. The activation thus:

  • Introduces non-linearity so the network can approximate complex functions,
  • Shapes gradient flow during training (affects how fast or stable learning is),
  • Controls output range (probabilities, signed values, etc.).

In practice, activation choice influences training speed, final accuracy, numerical stability — and sometimes whether the model trains at all. Small function, big impact.


Classic activations — formulas, properties & trade-offs

Sigmoid (Logistic)

Formula: σ(x) = 1 / (1 + e-x) Range: (0, 1) — useful for probabilities. Properties: smooth, monotonic, differentiable everywhere.

Pros: outputs are bounded and interpretable as probabilities; historically used in binary classifiers. Cons: saturates for |x| ≫ 0 → derivatives near zero → vanishing gradients. That kills learning in deep networks.

Logistic-curve.svg
By Qef (talk) – Created from scratch with gnuplot, Public Domain, Link

Tanh (Hyperbolic Tangent)

Formula: tanh(x) Range: (-1, 1) — zero-centered. Note: tanh(x) = 2σ(2x) − 1 (related to sigmoid).

Zero-centering helps gradient updates (positive and negative signals balance), so tanh often outperforms sigmoid in shallow networks. Still, it suffers from saturation and vanishing gradients at extremes.

ReLU (Rectified Linear Unit)

Formula: ReLU(x) = max(0, x) Range: [0, ∞) Properties: piecewise linear, simple derivative (0 for x<0, 1 for x>0).

ReLU dramatically improved deep learning — it reduces vanishing gradients for positive inputs and is cheap to compute. Many modern networks use it in hidden layers.

Drawback: the dying ReLU problem — if a neuron’s weights push it to negative inputs consistently, it may output 0 forever.

Leaky ReLU, Parametric ReLU (PReLU)

Formula (Leaky ReLU): f(x) = x if x>0 else αx, with α small (e.g., 0.01). PReLU learns α during training. These variants keep a small gradient for negative inputs so neurons don’t die.

Softmax (for classification outputs)

Softmax turns a vector of logits z into probabilities: softmax(zi) = exp(zi) / Σ exp(zj). Used in the final layer for multi-class classification; outputs sum to 1.

Practical note: implement numerically stable softmax using the log-sum-exp trick (subtract max(z) from z before exponentiating).


Newer & advanced activations — why they matter

Researchers keep inventing activations to squeeze extra performance out of architectures:

  • Swish: x * sigmoid(x) — smooth and non-monotonic; sometimes outperforms ReLU.
  • GELU: Gaussian Error Linear Unit — used in Transformers (BERT/GPT family). It blends linearity and stochastic behavior for smoother gradients.
  • ELU / SELU: aim to improve learning dynamics and self-normalize activations.

These functions can help optimization, but they’re slightly more expensive to compute. Use them when you need every bit of performance and you’re running larger models.


Vanishing & exploding gradients — the deep-learning villains

Two practical failure modes arise in training deep networks:

  • Vanishing gradients: gradients shrink as they backpropagate; parameters barely update — common with sigmoid/tanh in deep stacks.
  • Exploding gradients: gradients grow exponentially, causing numerical instability (weights blow up). Often addressed with gradient clipping.

Activation functions influence both behaviors. ReLU reduces vanishing gradients on its positive side, while initialization and normalization strategies (see below) help control both issues.


Initialization & activation go hand-in-hand

How you initialize weights matters a lot and is tied to the activation used:

  • Xavier / Glorot initialization: designed for sigmoid/tanh (keeps variance stable across layers).
  • He initialization: recommended for ReLU variants (accounts for ReLU’s zeroing of negatives).

A rule of thumb: match initialization to activation. It stabilizes forward and backward signal flow and helps training converge faster.


Batch Normalization, Dropout & activations — interactions

Batch Normalization (BatchNorm) normalizes layer inputs which stabilizes training and lets you use higher learning rates. It also reduces sensitivity to activation choice somewhat — networks using BatchNorm are less likely to suffer from vanishing gradients or dying ReLUs.

Dropout (randomly zeroing activations during training) is an effective regularizer; combined with ReLU it encourages sparse, robust representations.


Practical experiments — a quick recipe

If you want to explore activation behavior yourself, try this small experiment:

  1. Pick a small dataset (MNIST, tiny synthetic dataset, XOR).
  2. Fix architecture & hyperparameters (layers, learning rate, optimizer).
  3. Swap hidden activations: sigmoid, tanh, ReLU, Leaky ReLU, GELU.
  4. Record loss curves, training speed, and final accuracy.

You’ll see ReLU train faster and usually give better results on deeper networks. But in some toy tasks, tanh or sigmoid may be comparable. The lesson: measure, don’t guess.


Practical tips & rules of thumb

  • Hidden layers: Start with ReLU (or Leaky ReLU). It’s a reliable default for most problems.
  • Output layer: Use Sigmoid for binary classification, Softmax for multi-class, and Linear for regression.
  • Initialization: Use He init with ReLU; Glorot/Xavier for sigmoid/tanh.
  • BatchNorm: Add it if training is unstable — it often improves convergence.
  • If training stalls: try smaller learning rate, different activation, better init, or gradient clipping.

Code examples — quick references

Numerically stable softmax (NumPy)

import numpy as np

def softmax(x):
    z = x - np.max(x, axis=-1, keepdims=True)
    exp_z = np.exp(z)
    return exp_z / np.sum(exp_z, axis=-1, keepdims=True)
  

He initialization (for ReLU)

def he_init(shape):
    return np.random.randn(*shape) * np.sqrt(2.0 / shape[0])
  

Swap activation in Keras

from tensorflow.keras import layers, models
model = models.Sequential([
  layers.Dense(128, activation='relu', input_shape=(input_dim,)),
  layers.Dense(64, activation='relu'),
  layers.Dense(num_classes, activation='softmax')
])
  

Advanced notes: monotonicity, smoothness, and optimization landscape

Some activation functions are monotonic (sigmoid, tanh), others are not (Swish). Smoothness (differentiability) affects gradient calculations and optimization. Non-monotonic activations can create richer loss surfaces that, paradoxically, sometimes make optimization easier by giving gradients useful structure. That’s why newer functions like Swish and GELU occasionally outperform ReLU on large models.


Final thoughts — experiment, measure, and be pragmatic

Activation functions are small components with outsized influence. ReLU is a practical starting point. Sigmoid and tanh have historical importance and specific uses. Newer activations (Swish, GELU) are exciting tools for advanced architectures. But the best strategy is simple: try, measure, and iterate. Keep an eye on training curves, validation metrics, and numerical stability -your model will tell you what it needs.

Leave a Reply