DL Notes
Deep dive into the sigmoid activation function - mathematical properties, derivatives, use cases, limitations, and implementation in neural networks.
The sigmoid function (also called the logistic function) maps any real number to a value between 0 and 1. It was historically the most popular activation function but has been largely replaced by ReLU in hidden layers.
Mathematical Definition
Properties
Key properties:
- Output bounded between 0 and 1 (probability interpretation)
- Monotonically increasing
- Differentiable everywhere
- sigma(0) = 0.5
- sigma(-x) = 1 - sigma(x)
Derivative of Sigmoid
import numpy as np
import torch
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
s = sigmoid(x)
return s * (1 - s)
# Demonstrate
x = np.linspace(-6, 6, 100)
print(f"sigmoid(0) = {sigmoid(0):.4f}")
print(f"sigmoid(5) = {sigmoid(5):.6f}")
print(f"sigmoid(-5) = {sigmoid(-5):.6f}")
print(f"Max derivative = {sigmoid_derivative(0):.4f}")
print(f"Derivative at x=5: {sigmoid_derivative(5):.6f}")PyTorch Implementation
The Vanishing Gradient Problem
| Layer L gradient | sigma'(z) = max 0.25 |
| Layer L-1 gradient | sigma'(z) * sigma'(z) = max 0.0625 |
| Layer L-2 gradient | sigma'(z)^3 = max 0.015625 |
| Layer 1 gradient | sigma'(z)^n --> nearly 0 for large n |
When to Use Sigmoid
| Use Case | Appropriate? | Reason |
|---|---|---|
| Output layer (binary classification) | Yes | Outputs probability [0,1] |
| Hidden layers (deep networks) | No | Vanishing gradients |
| Gates in LSTM/GRU | Yes | Need 0-1 gating values |
| Shallow networks (1-2 layers) | Acceptable | Less vanishing gradient risk |
| Multi-label output | Yes | Independent probabilities per class |
Numerical Stability
import numpy as np
# Numerically stable sigmoid
def stable_sigmoid(x):
"""Avoids overflow in exp(-x) for large positive x."""
return np.where(
x >= 0,
1 / (1 + np.exp(-x)),
np.exp(x) / (1 + np.exp(x))
)
# Test with extreme values
print(f"sigmoid(100) = {stable_sigmoid(100)}") # ~1.0
print(f"sigmoid(-100) = {stable_sigmoid(-100)}") # ~0.0
print(f"sigmoid(1000) = {stable_sigmoid(1000)}") # 1.0 (no overflow)Sigmoid vs Other Activations
| Property | Sigmoid | Tanh | ReLU |
|---|---|---|---|
| Range | (0, 1) | (-1, 1) | [0, inf) |
| Zero-centered | No | Yes | No |
| Max gradient | 0.25 | 1.0 | 1.0 |
| Vanishing gradient | Severe | Moderate | None (positive) |
| Computation cost | Expensive (exp) | Expensive (exp) | Cheap (max) |
| Dead neurons | No | No | Yes |
Interview Questions
- Why is sigmoid not used in hidden layers of deep networks?
Its maximum derivative is 0.25, causing gradients to shrink exponentially through layers (vanishing gradient problem), making deep networks nearly untrainable.
- Why is sigmoid output not zero-centered and why does it matter?
Sigmoid outputs are always positive (0,1). This means gradients for weights are always all-positive or all-negative, causing zigzag updates in gradient descent.
- What is the relationship between sigmoid and logistic regression?
Logistic regression uses sigmoid to convert linear predictions into probabilities. A single neuron with sigmoid activation is exactly logistic regression.
- How does sigmoid relate to softmax?
Sigmoid is the special case of softmax with 2 classes. For binary: sigmoid(x) = softmax([x, 0])[0].
- Why is BCEWithLogitsLoss preferred over applying sigmoid then BCELoss?
Combining log and sigmoid in one operation (log-sum-exp trick) avoids numerical instability from log(0) or log(1) edge cases.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Sigmoid Activation Function.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Deep Learning topic.
Search Terms
deep-learning, deep learning, deep, learning, activation, functions, sigmoid, function
Related Deep Learning Topics