Visualized ML
Topics

A · One neuron

The Sigmoid Function

A smooth S-shaped curve that squashes any number into 0 to 1: a soft, differentiable version of the perceptron's step.

Try it: A5 · Logistic Regression, B2 · Activation Functions

Intuition

The perceptron's step jumps from 0 to 1 at zero. That makes it impossible to learn by small adjustments: nudge a weight a little, and the output either doesn't change at all or flips completely. There is no "a bit more right" to aim for.

The sigmoid is a step with its corner sanded smooth. Far below zero it is nearly 0, far above nearly 1, and in between it rises gradually through 0.5 at zero. Every small change in the input now makes a small change in the output, and that is exactly what gradient descent needs. As a bonus, an output between 0 and 1 can be read as a probability.

The sigmoid \(\sigma(kz)\) as its steepness \(k\) grows: the steeper it gets, the closer it comes to the perceptron's step. The dashed curve is its slope, highest at zero and nearly flat at both ends.

The math

\[ \sigma(z) = \frac{1}{1 + e^{-z}} \]

A few properties, each of which shows up somewhere else on this site:

  • Range and centre. \(0 < \sigma(z) < 1\) and \(\sigma(0) = \tfrac12\).
  • Symmetry. \(\sigma(-z) = 1 - \sigma(z)\). The probability of class 0 is the sigmoid of minus the score.
  • Its inverse is the log-odds. If \(p = \sigma(z)\) then \(z = \log\frac{p}{1 - p}\), the logit. So a linear model followed by a sigmoid says "the log-odds are linear in the inputs", which is the defining assumption of logistic regression.
  • A derivative made of itself. \[ \sigma'(z) = \sigma(z)\,\big(1 - \sigma(z)\big) \] It peaks at \(\tfrac14\) at \(z = 0\) and decays to almost nothing for large \(|z|\). Once \(\sigma(z)\) is computed, its slope costs one multiplication, which mattered for early neural networks.
  • Relation to tanh. \(\tanh(z) = 2\sigma(2z) - 1\): the same S, stretched to run from −1 to 1 and centred on zero.

Worked example. \(\sigma(0) = 0.5\), \(\sigma(2) = 1/(1 + e^{-2}) = 1/1.135 \approx 0.881\), \(\sigma(-2) \approx 0.119\) (and indeed \(0.881 + 0.119 = 1\)). The slope at 2 is \(0.881 \cdot 0.119 \approx 0.105\), less than half the peak 0.25. At \(z = 10\) the slope is about \(0.000045\): a weight feeding that neuron hardly receives any gradient at all.

Why the sigmoid fell out of favour in hidden layers

That last line is the problem. A sigmoid neuron whose input is large in either direction is saturated: its slope is almost zero. Backpropagation multiplies slopes layer by layer, and since \(\sigma'\) is at most \(\tfrac14\), each sigmoid layer shrinks the gradient by at least four times. Through ten layers that is a factor of a million: the vanishing gradient. Early layers of a deep sigmoid network barely learn. Its output is also always positive, never centred on zero, which makes the next layer's updates zigzag. Hidden layers moved to tanh and then to ReLU, which has slope 1 for every positive input; see Activation functions.

Where the sigmoid is still the right tool

  • Binary outputs. The last layer of a yes/no classifier, paired with cross-entropy. Together their gradient is simply \(\sigma(z) - y\), so saturation does no harm there.
  • Multi-label outputs. When an image can be "cat" and "outdoors", each label gets its own independent sigmoid (unlike softmax, which makes classes compete).
  • Gates. Inside LSTMs and GRUs, and in gated activations like SiLU, \(z\,\sigma(z)\), a sigmoid acts as a soft switch between "let through" and "block".

Numerical care

For large negative \(z\), \(e^{-z}\) overflows. Implementations compute \(\sigma(z) = e^{z}/(1 + e^{z})\) when \(z < 0\), and rarely compute \(\log \sigma(z)\) through \(\sigma\) at all, using \(\log\sigma(z) = -\log(1 + e^{-z})\) (the "softplus") instead.

History

The curve is older than computing: Pierre François Verhulst introduced the logistic function in 1838 to model population growth that levels off as resources run out. Statisticians adopted it for binary outcomes (Joseph Berkson's "logit", 1944). It became the standard neuron in the 1980s because the backpropagation papers needed a smooth activation, and remained so until ReLU replaced it in hidden layers around 2010.

Connections

Builds on

Leads to

Further reading