The Sigmoid Function
Intuition
The sigmoid squashes any real number into the interval (0, 1): very negative inputs give values near 0, very positive inputs give values near 1, and 0 gives exactly 0.5. That makes it a natural way to turn a score into a probability.
Precise explanation
- The derivative is largest at () and nearly 0 for large — the reason deep sigmoid networks learn slowly ("vanishing gradients").
- Inverse (logit): for , and .
- Overflow: Python's
math.exp(1000)raisesOverflowError. For compute instead (multiply top and bottom by ); then the exponent is always and nothing overflows.
Worked example
; ; ; ; .
Common mistakes
- Writing
1 / 1 + math.exp(-x)— precedence makes it . - Using the one-line formula for and crashing with
OverflowError. - Calling
logit(0)orlogit(1), which are infinite.
Why this matters
Logistic regression (ML-11) predicts , the derivative formula gives its gradient, and neural-network activations (DL-01) start here.
Library implementation
SciPy implements both functions as scipy.special.expit (the sigmoid) and
scipy.special.logit, vectorised over NumPy arrays and stable for large inputs. In NumPy code
later on (DL-01) you will write the same branch with np.where. The comparison
below runs only when SciPy is installed.
References
- Mathematics for Machine Learning — Marc Peter Deisenroth, A. Aldo Faisal, Cheng Soon Ong (Cambridge University Press) (
S009) - Dive into Deep Learning — Aston Zhang, Zachary C. Lipton, Mu Li, Alexander J. Smola (
S013) - math — Mathematical functions — Python Software Foundation (
S050)