Pros and Cons of the Sigmoid Function
The sigmoid function maps real inputs smoothly into (0, 1), making it ideal for binary classification outputs and multiplicative gates where values represent probabilities or fractions. However, as a hidden-layer activation in deep networks it suffers from severe vanishing gradients (maximum derivative is 0.25), non-zero-centered outputs that slow optimization, and higher exponential compute cost.
Study Card: Pros and Cons of the Sigmoid Function
Direct Answer
The logistic sigmoid function and its derivative are given by:
Pros:
- Probability & Gating Interpretation: Outputs lie strictly in , making sigmoid natural for binary classification outputs (paired with binary cross-entropy) and soft gates (e.g., LSTM/GRU gates, squeeze-and-excitation).
- Smooth & Differentiable: Provides smooth gradients everywhere with an analytical derivative expressed directly in terms of .
Cons:
- Severe Vanishing Gradients: Even at its steepest point (), , and for it saturates near ; multiplying derivatives across hidden layers attenuates gradients by at least .
- Non-Zero-Centered Output: Because all activations are strictly positive (), gradients with respect to downstream weights all share the same sign as the upstream error signal, inducing inefficient zig-zagging updates during SGD.
- Computational Expense: Requires computing exponentials compared to simple thresholding in ReLU.
Key Terms
- Activation Function: Non-linear mapping enabling neural networks to approximate non-linear functions.
- Vanishing Gradients: Exponential shrinkage of gradients during backpropagation through saturated or bounded-derivative layers, stalling early-layer learning.
- Non-Zero-Centered: Strictly positive outputs that bias weight gradient signs for a given input sample.
- Binary Classification: Predicting probabilities over two classes using sigmoid + binary cross-entropy loss.
- Saturation: Flat regions ( or ) where .
Example
In click-through-rate prediction, the final layer applies sigmoid to output e.g. 0.8 (80% click probability). Using sigmoid inside deep hidden layers, however, degrades training: any neuron whose weighted sum drifts to or yields , choking gradient backpropagation to earlier layers, while strictly positive outputs constrain downstream weight updates to move synchronously in the same sign direction on every sample.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(z):
"""
Numerically stable sigmoid supporting scalars and NumPy arrays
without overflow warnings on large negative or positive inputs.
"""
z = np.asarray(z, dtype=float)
out = np.empty_like(z)
pos = z >= 0
out[pos] = 1.0 / (1.0 + np.exp(-z[pos]))
exp_z = np.exp(z[~pos])
out[~pos] = exp_z / (1.0 + exp_z)
return out
# Generate data for plotting
z = np.linspace(-10, 10, 200)
sigmoid_z = sigmoid(z)
sigmoid_grad = sigmoid_z * (1.0 - sigmoid_z)
# Plot sigmoid function
plt.figure(figsize=(8, 5))
plt.plot(z, sigmoid_z)
plt.xlabel("z")
plt.ylabel("sigmoid(z)")
plt.title("Sigmoid Function")
plt.grid(True)
# Plot sigmoid derivative (peaks at 0.25 and vanishes in tails)
plt.figure(figsize=(8, 5))
plt.plot(z, sigmoid_grad)
plt.xlabel("z")
plt.ylabel("sigmoid'(z)")
plt.title("Derivative of Sigmoid Function (Max = 0.25)")
plt.grid(True)
plt.show()
Related Concepts
- Other Activation Functions:
tanhsolves zero-centering (with max derivative1.0) but still saturates; ReLU and GELU avoid positive saturation and dominate modern hidden layers, while softmax generalizes sigmoid to multi-class outputs. - Backpropagation & Logits: In frameworks like PyTorch (
BCEWithLogitsLoss), combining sigmoid and binary cross-entropy into a single log-sum-exp step ensures numerical stability and clean gradients. - Gradient Descent Optimization: Non-zero-centered activations explain why zero-centered activations or normalization layers (BatchNorm / LayerNorm) accelerate convergence.
- Binary Cross-Entropy Loss: Standard paired loss that avoids the vanishing-gradient plateau produced when pairing sigmoid with MSE.