What is an activation function in a neural network?
An activation function is a nonlinear function applied to each neuron's weighted sum before the result passes to the next layer. Without it, a neural network of any depth computes only a linear function of its input. Common choices are sigmoid, tanh, ReLU, and smooth ReLU variants such as GELU and Swish.
The choice of activation affects what a network can represent and how well gradients flow during training. ReLU made deep convolutional networks practical around 2012. GELU is the default in BERT and GPT-style transformers, and a Swish-based gated unit (SwiGLU) is used in the feed-forward layers of LLaMA. Sigmoid survives at output layers for binary classification and in the gates of LSTMs.
Key ideas
- Without a nonlinear activation, stacked layers collapse into a single linear layer.
- Sigmoid and tanh saturate for large inputs, where their gradients approach zero.
- Tanh is a shifted and rescaled sigmoid with zero-centered outputs.
- ReLU is cheap, does not saturate for positive inputs, and is the default for many hidden layers.
- Leaky ReLU, ELU, Swish, and GELU keep a nonzero response for negative inputs.
- Output layers use task-specific activations: sigmoid for binary, softmax for multi-class, none for regression.
Common questions
Which activation function should I use?
For hidden layers, start with ReLU in convolutional and plain feed-forward networks, and GELU or SwiGLU in transformers. For the output layer, the choice follows the task: sigmoid for one probability, softmax for several mutually exclusive classes, and a linear output for regression.
What is the difference between an activation function and softmax?
Most activation functions act on each neuron independently. Softmax acts on a whole vector: it exponentiates every entry and divides by the sum, so the outputs are positive and add to 1. It is used at the output of classifiers to turn scores into class probabilities.
Does the activation function need to be differentiable?
It needs a usable gradient almost everywhere, because training relies on backpropagation. ReLU is not differentiable at 0, but that single point has no practical effect and frameworks assign it a derivative of 0. A step function, by contrast, has zero gradient everywhere it is defined, so gradient descent cannot train through it.
Why is GELU used in transformers?
GELU weights each input by the probability that a standard normal variable falls below it, giving a smooth curve instead of ReLU's sharp corner. Its authors reported small gains over ReLU across vision and language tasks, and BERT and GPT adopted it. Later models such as LLaMA moved to SwiGLU, a gated unit built on Swish.
Related explainers
- Neural Networks — Neurons, layers, and how networks learn
- The Perceptron — The simplest neural network — one neuron, one decision boundary
- Backpropagation — How networks learn from their mistakes
- Vanishing & Exploding Gradients — Why deep networks struggle and how to fix it
- Weight Initialization — Xavier, He, and why random weights matter
- Softmax, Logits & Temperature — Turning raw scores into probabilities
References
- Hendrycks & Gimpel (2016), "Gaussian Error Linear Units (GELUs)"
- Ramachandran, Zoph & Le (2017), "Searching for Activation Functions"
- Clevert, Unterthiner & Hochreiter (2015), "Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs)"
- He et al. (2015), "Delving Deep into Rectifiers"
- Goodfellow, Bengio & Courville (2016), "Deep Learning", section 6.3: Hidden Units
Last updated