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

σ(x)=11+e−x,σ(−x)=1−σ(x),σ′(x)=σ(x) (1−σ(x)).\sigma(x) = \frac{1}{1 + e^{-x}}, \qquad \sigma(-x) = 1 - \sigma(x), \qquad \sigma'(x) = \sigma(x)\,(1 - \sigma(x)).
  • The derivative is largest at x=0x = 0 (0.250.25) and nearly 0 for large ∣x∣|x| — the reason deep sigmoid networks learn slowly ("vanishing gradients").
  • Inverse (logit): logit⁡(p)=ln⁡p1−p\operatorname{logit}(p) = \ln\dfrac{p}{1-p} for 0<p<10 < p < 1, and σ(logit⁡(p))=p\sigma(\operatorname{logit}(p)) = p.
  • Overflow: Python's math.exp(1000) raises OverflowError. For x<0x < 0 compute σ(x)=ex1+ex\sigma(x) = \dfrac{e^{x}}{1 + e^{x}} instead (multiply top and bottom by exe^x); then the exponent is always ≤0\le 0 and nothing overflows.

Worked example

σ(0)=1/2\sigma(0) = 1/2; σ(2)=1/(1+e−2)≈0.8808\sigma(2) = 1/(1 + e^{-2}) \approx 0.8808; σ(−2)≈0.1192=1−0.8808\sigma(-2) \approx 0.1192 = 1 - 0.8808; σ′(0)=0.5⋅0.5=0.25\sigma'(0) = 0.5 \cdot 0.5 = 0.25; logit⁡(0.8808)≈2\operatorname{logit}(0.8808) \approx 2.

Common mistakes

  • Writing 1 / 1 + math.exp(-x) — precedence makes it 1+e−x1 + e^{-x}.
  • Using the one-line formula for x=−1000x = -1000 and crashing with OverflowError.
  • Calling logit(0) or logit(1), which are infinite.

Why this matters

Logistic regression (ML-11) predicts σ(w⋅x+b)\sigma(w\cdot x + b), 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