TL;DR
The sigmoid function, usually written , takes any real number and returns a value strictly between 0 and 1. Large positive inputs land close to 1, large negative inputs land close to 0, and the input 0 maps exactly to one half. Its graph is a smooth S-shape.
Where you'll see it: in logistic regression, where it turns a linear score into a probability, in the gates of LSTM cells, and in older neural networks as the activation that squashes each unit's output.
Intuition
A dial that saturates
Picture a volume knob that can be turned as far as you like in either direction. Near the middle, a small turn makes a noticeable change. Turn it far past the middle and nothing much changes any more: the sound is already as loud as the speaker can go. The sigmoid behaves like that knob run through a squeezing function. Near zero, the output moves quickly as the input changes. Far from zero in either direction, the output flattens out and approaches a ceiling of 1 or a floor of 0.
Why would you want that? Many problems produce a raw score that can be any number, positive or negative, and you want a value you can read as a fraction, a confidence, or a switch. A spam filter might compute a score of 4.2 for one email and -3.1 for another. Those numbers are not probabilities. The sigmoid maps them to about 0.985 and 0.043, which can be read as "probably spam" and "probably not spam". The score's order is kept, since larger inputs always give larger outputs, but the scale is now bounded.
The sigmoid is also smooth. It has a slope everywhere, which is what lets gradient-based training adjust the score a little at a time. A hard step function that jumps from 0 to 1 would give no useful slope to learn from.
- σ(x)
- slope σ'(x)
The math
The sigmoid is
The symbol is Euler's number, about 2.71828, and is the exponential function evaluated at the negative of (see Exponential Function). Read the formula inside out: take , negate it, raise to that power, add 1, and take the reciprocal.
Range and key values
Because is always positive, the denominator is always greater than 1, so for every real . The output never reaches 0 or 1 exactly. The two limits are
At the centre, .
Symmetry
The graph is symmetric about the point . Algebraically,
To check: , and . The two expressions match.
The derivative
The slope has a tidy form. Differentiating with the chain rule and the derivative of (which is ) gives
Rewrite it using itself. Since , we get
This form is the one that matters in practice. If you already have the output stored from the forward pass, the slope costs one multiplication and one subtraction. The slope is largest at the centre, where gives , and it shrinks toward 0 as grows.
Inverse: the logit
The sigmoid can be undone. Set and solve for :
The quantity is the odds that the event happens, and its logarithm (see Logarithm) is called the log-odds or logit. So the sigmoid converts log-odds into probability. This is why logistic regression, which models log-odds as a linear function of the inputs, produces probabilities through a sigmoid.
Worked examples
Compute at , and .
- At : .
- At : , so .
- At : , so .
Interpretation: the score 0 is a coin flip, the score 2 leans strongly toward the positive class at about 88%, and the score leans strongly toward the negative class, with only about 5% on the positive side.
Use the symmetry and derivative identities to answer two questions without a calculator for the slope.
- Symmetry: .
- Slope at using : .
- Compare with the slope at the centre: , which is more than twice as steep.
Interpretation: a unit change in the score shifts the probability much more near the centre than in the tails. This is why a classifier is most sensitive to its inputs when its score is close to zero, and why an example the model is already sure about gives almost no learning signal.
Tradeoffs and failure modes
The sigmoid is simple and useful, but its flattening is a double-edged property.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Deep stack of sigmoid layers | The slope is at most 0.25, so gradients shrink with each layer and early layers learn slowly (the vanishing gradient problem) | ReLU or another activation with a slope near 1 for positive inputs |
| Output needs to be a probability over several mutually exclusive classes | Independent sigmoids do not sum to 1 | Softmax over the class scores |
| Output should be unbounded, such as a regression target | The bounded range clips the answer | A linear output with no squashing |
| Very large inputs | can overflow in floating point for large negative | Use a numerically stable implementation, which libraries provide |
The vanishing gradient problem is the main reason sigmoid activations fell out of favour for hidden layers. It does not stop them being useful at the output of a binary classifier, where a single probability is exactly what is needed.
History
The logistic curve was studied in the 1830s and 1840s by the Belgian mathematician Pierre-François Verhulst, who used it to model population growth that levels off as it approaches a limit. In the twentieth century statisticians adopted it for binary outcomes, leading to logistic regression, which became a standard tool in medicine and the social sciences.
Its use in neural networks came later. Early networks used the sigmoid as the nonlinearity in each unit because it is smooth and bounded, which matched the simple biological picture of a neuron firing more as input grows, up to a saturation point. When training deeper networks became difficult in the 2000s, the sigmoid's flat tails were identified as a main cause, and activations with better gradient flow took over in hidden layers.
Check yourself
QuestionRecall: what are the limits of as and as , and what is ?Show answer
As grows large and positive, approaches 1. As becomes large and negative, approaches 0. At the centre, .
QuestionComputation: evaluate to three decimal places. Use .Show answer
We need . Then .
QuestionWhy: explain why the derivative of the sigmoid can be written as , and why this form is convenient during training.Show answer
Differentiating gives . Splitting this into the product of and gives and . During training the forward pass has already computed , so the slope is one multiplication and one subtraction rather than a fresh exponential.
QuestionApplied: a network hidden layer uses sigmoid activations, and training stalls after a few layers. What is the most likely cause, and what would you try first?Show answer
The most likely cause is vanishing gradients. The sigmoid's slope is at most 0.25, and it is close to 0 for inputs far from the centre, so the gradient shrinks as it passes back through many layers. Replacing the hidden-layer sigmoids with ReLU, whose slope is 1 for positive inputs, is the usual first fix. The sigmoid can remain at the final output if the task is binary classification.
QuestionApplied: a model outputs a score of 0 for an email. What probability does the sigmoid assign, and what does a score of mean for the same model?Show answer
A score of 0 gives , so the model is undecided. A score of gives , meaning the model assigns about a 2% chance to the positive class. The sign of the score says which side of one half the probability lies on, and its size says how far away from the boundary the model is.
Sources
- Sigmoid function (Wikipedia): https://en.wikipedia.org/wiki/Sigmoid_function
- Logistic function (Wikipedia): https://en.wikipedia.org/wiki/Logistic_function
- Logistic regression (Wikipedia): https://en.wikipedia.org/wiki/Logistic_regression