Activation Functions: Sigmoid, Tanh, ReLU
Imagine stacking ten layers of a neural network. But every layer only computes a weighted sum of its inputs. No matter how many layers you stack, the entire network collapses algebraically. It becomes a single linear map — a fancy way of computing . All the depth, all the extra parameters, buy you nothing. They add nothing beyond what one layer could already do. This is the concrete problem that activation functions solve. They inject nonlinearity between layers. This way, depth actually increases what a network can represent. Activation functions are one of the smallest pieces of code in deep learning. Often they are a single line. Yet they shape almost everything about how a network trains. The choice between sigmoid, tanh, and ReLU matters greatly. It determines whether gradients vanish into numerical dust after a few layers. It determines whether units silently die and never fire again. It also determines whether a network trains in minutes or fails to converge at all. Every modern architecture, from convolutional networks to transformers, is built on some descendant of these three functions. This article treats activation functions as the bridge. They connect The Artificial Neuron and everything that comes after it. The The Multilayer Perceptron is exactly a chain of neurons. Its nonlinearities make stacking meaningful. The The Universal Approximation Theorem leans directly on the nonlinearity of the chosen activation. The The Vanishing and Exploding Gradient Problem is largely a story too. It is about which activation function you picked. It also depends on how its derivative behaves across many layers. By the end you will be able to write down each function and its derivative from memory. You will explain precisely why linear networks cannot gain expressive power from depth. You will trace a forward and backward pass numerically through a tiny network. You will also recognize the classic failure modes — saturation, dying units, and exploding pre-activations. Each activation is prone to one of these.
- A neural network without nonlinear activations collapses into a single linear transformation, regardless of depth.
- Sigmoid squashes inputs to ; smooth and interpretable as a probability, but saturates and its gradient vanishes for large .
- Tanh squashes inputs to , is zero-centered (helping optimization), but still saturates at the extremes.
- ReLU outputs : cheap, non-saturating for positive inputs, but can permanently "die" for negative inputs (zero gradient).
- Each function's derivative is the key object: it multiplies into every upstream gradient during backpropagation, so its shape controls training dynamics.
- Modern default: ReLU and its variants (Leaky ReLU, GELU, Swish) dominate hidden layers; sigmoid/tanh survive mainly at output layers and inside gates (e.g., LSTMs).
Nonlinear activation functions are what let stacked layers compute more than a single linear map — and the shape of their derivative determines how well gradients survive backpropagation through depth.
The idea
An activation function is applied elementwise to the pre-activation produced by a neuron, giving the neuron's output . Without (or with the identity), a network of layers computes
which is an affine function of : expand the products and you get for some single matrix and vector . Depth added zero expressive power. This is the precise reason nonlinearity is necessary, not merely conventional: composing linear maps yields a linear map, but composing nonlinear maps yields a much richer function class, which is the content later formalized by the The Universal Approximation Theorem. Three functions dominate the historical and practical landscape. Sigmoid (logistic function) maps and was the original biologically-motivated choice, since it mimics a neuron's firing rate saturating at 0 or 1. Tanh maps and is simply a rescaled, shifted sigmoid — but the rescaling matters enormously because tanh is zero-centered while sigmoid is not. ReLU (Rectified Linear Unit) abandons smoothness and boundedness entirely in favor of computational simplicity and a derivative that does not shrink for positive inputs. The property that separates these functions in practice is saturation: a region where the function is nearly flat, so its derivative is near zero. Sigmoid and tanh saturate on both sides (); ReLU saturates only on the negative side (it is exactly zero, with zero gradient, for all ) and is unbounded and gradient-preserving on the positive side. This asymmetry is the single fact that explains most of the empirical differences between the three functions.
How it works
Step 1 — Compute the pre-activation. Each neuron computes , a linear combination of inputs (the mechanism already described in The Artificial Neuron).
Step 2 — Apply the nonlinearity elementwise. Choose and compute . In a layer with many units, this is applied coordinate-wise to a vector.
Step 3 — Forward propagate. The output becomes the input to the next layer, repeating steps 1–2 across all layers — this is exactly the forward pass of a The Multilayer Perceptron.
Step 4 — Backpropagate through the nonlinearity. During training, the chain rule requires , the local derivative, at every unit. This value multiplies the upstream gradient flowing backward, so its magnitude directly controls how much gradient signal survives — this is the mechanism at the heart of the The Vanishing and Exploding Gradient Problem. The three derivatives, which you should be able to derive from the definitions:
Sigmoid's derivative is derived from the quotient rule on : , confirmed by substitution. Tanh's identity follows similarly from via the quotient rule, simplifying to . ``` function FORWARD_LAYER(x, W, b, activation): z = W · x + b # pre-activation, vector of size n if activation == "sigmoid": a = 1 / (1 + exp(-z)) # elementwise else if activation == "tanh": a = (exp(z) - exp(-z)) / (exp(z) + exp(-z)) else if activation == "relu": a = max(0, z) # elementwise return a, z # cache z for the backward pass function BACKWARD_LAYER(dA, z, activation):
# dA: upstream gradient dL/dA, same shape as z if activation == "sigmoid": s = 1 / (1 + exp(-z)) dPhi = s (1 - s) else if activation == "tanh": t = (exp(z) - exp(-z)) / (exp(z) + exp(-z)) dPhi = 1 - t t else if activation == "relu": dPhi = (z > 0) ? 1 : 0 # subgradient convention at z = 0 dZ = dA * dPhi # elementwise product (chain rule) return dZ # pass on to compute dW, db, and dX ``` The pseudocode is deliberately structured to mirror real autodiff frameworks: `FORWARD_LAYER` caches because `BACKWARD_LAYER` needs it, and the backward step is always "multiply the incoming gradient by the local derivative," which is the elementary chain-rule step repeated at every layer during backpropagation.
Complexity & correctness
Time complexity. Each activation is applied elementwise, so for a layer with units the forward cost is evaluations of , and the backward cost is evaluations of . The constant factor differs meaningfully: sigmoid and tanh require an exponential or (a transcendental function, typically 10–20× the cost of a comparison), while ReLU requires only a comparison against zero, `max(0, z)`. Across a network with layers and total width , the total activation cost is per forward or backward pass — asymptotically the same order as the matrix multiplications (), but with ReLU's tiny constant it is usually negligible next to the matmuls, whereas sigmoid/tanh's exponential evaluations can be a measurable fraction of runtime on large, wide layers.
Space complexity. Backpropagation must cache the pre-activation (or equivalently , since can often be written in terms of : , ) for every unit in every layer, since the backward pass needs it. This costs additional memory per example, matching the memory footprint of storing activations generally — it is not activation-specific overhead but a property of backpropagation itself.
Correctness of the derivative computations. The chain rule guarantees that if and , then , which is exactly the multiplication `dA * dPhi` in the pseudocode. The correctness of each closed-form derivative follows from ordinary single-variable calculus (quotient rule, as sketched above), and can be double-checked numerically: for any , as (central finite differences), which is the standard gradient-checking technique used to catch bugs in hand-derived backward passes.
Why nonlinearity is necessary — the collapse argument, formally. Suppose every layer uses . Then layer computes . Substituting recursively:
for some vector built from the 's and the 's. Since is a single matrix and a single vector, the entire -layer network is exactly one affine map — no more expressive than a single-layer perceptron, regardless of how large or the hidden widths are. Any nonlinear, non-affine breaks this collapse, since is generically not affine in ; this is the minimal condition the The Universal Approximation Theorem requires of an activation function.
Tradeoffs, stated honestly.
- Sigmoid/tanh are smooth and bounded, giving stable, interpretable outputs, but as means gradients shrink multiplicatively across layers — the core mechanism of vanishing gradients.
- ReLU avoids saturation on the positive side ( exactly, no shrinkage), which is why it trains deep networks far more reliably, but a unit with forever (e.g., after a large negative weight update) has zero gradient forever and can "die," never updating again — an asymmetric failure mode sigmoid/tanh do not have in the same way.
- ReLU is not differentiable at ; frameworks sidestep this by defining a subgradient (0 or 1), which is mathematically imprecise but practically harmless since has measure zero under continuous input distributions.
Worked example
Consider a single hidden unit with weight , bias , receiving scalar input , so the pre-activation is . We compute the forward output and the local derivative under each activation, then propagate an upstream gradient of (say, coming from a downstream loss) back to .
| Activation | at | at | |
|---|---|---|---|
| Sigmoid | |||
| Tanh | |||
| ReLU | (since ) |
Now push into the negative region by changing the input to , giving :
| Activation | at | at | |
|---|---|---|---|
| Sigmoid | |||
| Tanh | |||
| ReLU | (since ) |
This side-by-side trace makes the saturation phenomenon concrete: at (near zero) all three gradients are of reasonable magnitude, but at sigmoid and tanh gradients have shrunk to nearly nothing (0.054 and 0.0147 respectively), and ReLU's gradient is exactly zero — this unit is now "dead" for this input and contributes no learning signal at all through this path, illustrating both the vanishing-gradient behavior of the saturating functions and the dying-ReLU phenomenon in one worked comparison.
Common pitfalls & traps
| Pitfall | Why it happens | Consequence |
|---|---|---|
| Using sigmoid/tanh in many stacked hidden layers | Gradients multiply (sigmoid) or (tanh) at every layer | Gradient magnitude decays geometrically with depth — the The Vanishing and Exploding Gradient Problem |
| Initializing weights too large with ReLU | Large weights push many pre-activations deep negative | Large fractions of units "die" permanently (zero gradient forever) |
| Forgetting sigmoid is not zero-centered | , always positive | Gradients w.r.t. weights in the next layer share the same sign, causing inefficient zig-zag optimization paths |
| Treating as a real edge case for ReLU | ReLU is non-differentiable exactly at | Frameworks silently pick a subgradient (0 or 1); rarely matters but can confuse gradient-check debugging |
| Assuming ReLU has no saturation at all | Only the positive side is unsaturated | Negative inputs are fully saturated (flat at 0, gradient exactly 0) — arguably a harsher saturation than sigmoid's asymptotic approach |
| Applying activation before batch/layer normalization inconsistently | Order matters for training dynamics | Common source of subtly divergent-looking results between reimplementations |
Pioneers & history
The mathematical shapes of sigmoid and tanh predate neural networks by over a century — the logistic function was studied by Pierre François Verhulst in the 1830s for population growth — but their adoption as neuron nonlinearities is tied to the perceptron and connectionist tradition. Frank Rosenblatt's perceptron (1958) used a hard threshold, not a smooth sigmoid; smooth, differentiable activations became essential once gradient-based training via backpropagation required a well-defined derivative everywhere. Sigmoid and tanh dominated neural network research through the 1980s and 1990s precisely because they are differentiable substitutes for the threshold units of earlier perceptron models.
Origins · 1 minGeoffrey Hinton and how this was discovered
history 1958 — Rosenblatt's perceptron uses a hard-threshold nonlinearity, the conceptual ancestor of later smooth activations. 1986 — Rumelhart, Hinton, and Williams popularize backpropagation, driving widespread use of differentiable sigmoid and tanh units. 2000 — Hahnloser, Sarpeshkar, Mahowald, Douglas, and Seung describe rectified linear-like dynamics in biological neural circuit models. 2010 — Nair and Hinton show ReLU improves training of restricted Boltzmann machines and deep networks over sigmoid/tanh units. 2012 — AlexNet uses ReLU throughout a deep convolutional network, contributing to a large ImageNet accuracy jump and cementing ReLU as the default hidden-layer nonlinearity.
Where this matters
Activation functions are not a historical footnote — they are a live design choice in every network trained today. Convolutional networks for image classification (ResNet, EfficientNet, and their descendants) use ReLU or its smoother relatives (Leaky ReLU, GELU, Swish) almost universally in hidden layers because non-saturating gradients let gradient signal survive tens or hundreds of layers. Recurrent architectures such as LSTMs and GRUs still rely heavily on sigmoid and tanh internally: sigmoid gates control what information to keep or forget (its range naturally represents a "how much" fraction), while tanh squashes candidate cell-state updates into a bounded, zero-centered range that keeps hidden states numerically stable across long sequences. Output layers for binary classification use sigmoid to produce a probability in ; multi-class classifiers generalize this with softmax, a multivariate sibling of sigmoid. Transformer architectures, the backbone of large language models, typically use GELU or Swish — smooth cousins of ReLU designed to keep ReLU's gradient-preserving benefits while avoiding its exact-zero dead zone. Any framework you use — PyTorch, TensorFlow, JAX — ships all three functions as primitive, hardware-accelerated operations, because they sit on the critical path of essentially every forward and backward pass executed in deep learning.
Ask the tutor: How does the choice between sigmoid, tanh, and ReLU concretely change what happens during backpropagation in a 50-layer network?
Why can't we just use a linear activation and add more layers instead?
Adding layers with linear (identity) activations does not increase expressive power, because the composition of affine maps is itself an affine map. Concretely, if layer computes , substituting recursively across layers yields for a single combined matrix and vector . This means a 100-layer linear network computes exactly the same function class as a single-layer one — just with more parameters and more compute wasted getting there. Nonlinear activations break this collapse: is not generally affine in , so composing many nonlinear layers can represent function classes a single layer cannot, which is the foundation of the The Universal Approximation Theorem.
Is ReLU always better than sigmoid or tanh?
Not universally — it depends on where the nonlinearity sits in the network. In hidden layers of deep feedforward or convolutional networks, ReLU usually trains faster and more reliably because its gradient does not shrink for positive pre-activations, avoiding much of the vanishing-gradient problem. But sigmoid and tanh remain the right choice in specific roles: sigmoid for output layers needing a probability, and sigmoid/tanh inside gated recurrent units (LSTM, GRU) where their bounded, saturating behavior is exactly what is needed to control information flow. The "always use ReLU" heuristic applies mainly to hidden layers of standard feedforward and convolutional stacks, not universally.
Why is sigmoid's lack of zero-centering considered a problem?
Because sigmoid outputs are always strictly positive (), the gradient of the loss with respect to the weights of the next layer, which depends on the sign of the incoming activation, tends to be all-positive or all-negative simultaneously for a given example. This forces gradient descent to take a zig-zagging path toward the optimum rather than moving directly, since weight updates cannot independently choose different sign directions. Tanh avoids this because its output range is centered at zero, allowing both positive and negative activations and thus more balanced gradient directions — one reason tanh often outperforms sigmoid in hidden layers even though both saturate similarly.
What exactly is the derivative of ReLU at z = 0, and does it matter in practice?
Mathematically, has a sharp corner at , so the derivative is undefined there in the strict calculus sense — the left derivative is 0 and the right derivative is 1. In practice, deep learning frameworks adopt a convention (commonly setting the derivative to 0 or 1 at exactly ) since this single point has probability zero of being hit exactly under continuous-valued inputs and floating-point arithmetic. This is a subgradient choice rather than a true derivative, and it causes no measurable harm in practice, though it is worth knowing when debugging gradient checks that seem to disagree at isolated points.
How do vanishing gradients connect specifically to the choice of activation function?
During backpropagation, the gradient at any layer is the upstream gradient multiplied by the local derivative at every unit, chained across all layers back to the input. If for some constant at every layer (true for sigmoid, where , and tanh, where but is rarely achieved exactly), the product across layers scales roughly like , shrinking exponentially with depth. This is precisely the mechanism behind the The Vanishing and Exploding Gradient Problem, and it is why non-saturating activations like ReLU, whose derivative is exactly 1 (not less than 1) for all positive pre-activations, dramatically improve trainability of very deep networks.
Why does the exact numeric formula for tanh relate so closely to sigmoid?
Algebraically, , which you can verify by substituting the definition of and simplifying the exponentials. This means tanh is literally a rescaled and shifted sigmoid: it stretches sigmoid's output range from to and compresses the input by evaluating sigmoid at instead of . Despite being mathematically equivalent up to an affine transform, tanh's zero-centering changes optimization dynamics enough that the two functions behave quite differently as hidden-layer activations, illustrating that seemingly small transformations of an activation function can have outsized practical consequences.
Do modern architectures still use sigmoid and tanh at all, or has ReLU fully replaced them?
Both are still very much in use, just in more specialized roles rather than as the default hidden-layer choice in feedforward and convolutional networks. LSTM and GRU recurrent units rely on sigmoid gates to control read/write/forget behavior (values naturally interpreted as fractions between 0 and 1) and tanh for producing bounded candidate updates to the cell state. Sigmoid remains standard for binary classification output layers, and its multivariate generalization, softmax, is standard for multi-class classification outputs. ReLU and its smoother relatives (Leaky ReLU, GELU, Swish) dominate hidden layers of feedforward, convolutional, and transformer architectures, but the older functions persist wherever a bounded, saturating output is the actual desired behavior rather than a limitation to avoid.