Neural Mastery
← Back to Practice

Sigmoid Derivative (Vectorized, Numerically Stable)

Difficulty: Medium · Concept: Activation Functions — Historical / Saturating Functions

Backpropagation through a sigmoid layer needs σ(z)\sigma'(z), not just σ(z)\sigma(z). Sigmoid has an unusually clean derivative in terms of its own output: σ(z)=σ(z)(1σ(z))\sigma'(z) = \sigma(z)\big(1 - \sigma(z)\big) — no separate derivation needed once you already have σ(z)\sigma(z).

Two things make this harder than Sigmoid Activation From Scratch: it operates on a list of values, not just one, and — unlike the original problem's moderate test range — it has to stay numerically correct at extreme magnitudes. Sigmoid Activation's solution notes that 1 / (1 + math.exp(-z)) overflows for z below roughly -709, since that requires computing math.exp(1e3)-scale numbers. This problem actually requires fixing that, not just mentioning it.

Your task: implement sigmoid_derivative(z_list), returning σ(z)\sigma'(z) for every z in z_list. Must not raise OverflowError even for very large-magnitude inputs (e.g. -1000 or 1000).

Implement it yourself
assert abs(sigmoid_derivative([0])[0] - 0.25) < 1e-9 assert abs(sigmoid_derivative([2])[0] - 0.8807970779778823 * (1 - 0.8807970779778823)) < 1e-9 assert abs(sigmoid_derivative([-2])[0] - 0.11920292202211755 * (1 - 0.11920292202211755)) < 1e-9 assert sigmoid_derivative([-1000, 1000])[0] < 1e-6 assert sigmoid_derivative([-1000, 1000])[1] < 1e-6

Next: Numerically Stable Softmax From Scratch

Last updated Sep 5, 2026Edit this pageReport an issue