Search

Search papers and lessons

Sigmoid Function

Level 1 · 8 min read

The sigmoid squashes any real number into the open interval from 0 to 1 along an S-shaped curve, so it turns an unbounded score into something that reads like a probability.

On this page

  • Colour = topic mix
  • Neural network basics
  • Discrete math
  • Sign in to see your progress

TL;DR

The sigmoid function, usually written σ(x)=11+e−x\sigma(x) = \dfrac{1}{1 + e^{-x}}, 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.

−6−4−2024600.20.40.60.81xoutput
  • σ(x)
  • slope σ'(x)
The sigmoid rises from near 0 to near 1, passing through one half at x = 0. Its slope, shown dashed, peaks at 0.25 at the centre and fades toward zero in both tails.

The math

The sigmoid is

σ(x)=11+e−x,x∈R.\sigma(x) = \frac{1}{1 + e^{-x}}, \qquad x \in \mathbb{R}.

The symbol ee is Euler's number, about 2.71828, and e−xe^{-x} is the exponential function evaluated at the negative of xx (see Exponential Function). Read the formula inside out: take xx, negate it, raise ee to that power, add 1, and take the reciprocal.

Range and key values

Because e−xe^{-x} is always positive, the denominator is always greater than 1, so 0<σ(x)<10 \lt \sigma(x) \lt 1 for every real xx. The output never reaches 0 or 1 exactly. The two limits are

lim⁡x→+∞σ(x)=1,lim⁡x→−∞σ(x)=0.\lim_{x \to +\infty} \sigma(x) = 1, \qquad \lim_{x \to -\infty} \sigma(x) = 0.

At the centre, σ(0)=11+1=12\sigma(0) = \dfrac{1}{1 + 1} = \dfrac{1}{2}.

Symmetry

The graph is symmetric about the point (0,12)(0, \tfrac{1}{2}). Algebraically,

σ(−x)=1−σ(x).\sigma(-x) = 1 - \sigma(x).

To check: σ(−x)=11+ex\sigma(-x) = \dfrac{1}{1 + e^{x}}, and 1−σ(x)=1−11+e−x=e−x1+e−x=1ex+11 - \sigma(x) = 1 - \dfrac{1}{1 + e^{-x}} = \dfrac{e^{-x}}{1 + e^{-x}} = \dfrac{1}{e^{x} + 1}. The two expressions match.

The derivative

The slope has a tidy form. Differentiating with the chain rule and the derivative of e−xe^{-x} (which is −e−x-e^{-x}) gives

σ′(x)=e−x(1+e−x)2.\sigma'(x) = \frac{e^{-x}}{(1 + e^{-x})^2}.

Rewrite it using σ\sigma itself. Since e−x1+e−x=1−σ(x)\dfrac{e^{-x}}{1 + e^{-x}} = 1 - \sigma(x), we get

σ′(x)=σ(x)(1−σ(x)).\sigma'(x) = \sigma(x)\bigl(1 - \sigma(x)\bigr).

This form is the one that matters in practice. If you already have the output σ(x)\sigma(x) stored from the forward pass, the slope costs one multiplication and one subtraction. The slope is largest at the centre, where σ(0)=12\sigma(0) = \tfrac{1}{2} gives σ′(0)=12⋅12=0.25\sigma'(0) = \tfrac{1}{2} \cdot \tfrac{1}{2} = 0.25, and it shrinks toward 0 as ∣x∣|x| grows.

Inverse: the logit

The sigmoid can be undone. Set y=σ(x)y = \sigma(x) and solve for xx:

x=ln⁡ ⁣(y1−y).x = \ln\!\left(\frac{y}{1 - y}\right).

The quantity y1−y\dfrac{y}{1-y} 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

Example
Evaluating at three points

Compute σ(x)\sigma(x) at x=0x = 0, x=2x = 2 and x=−3x = -3.

  1. At x=0x = 0: σ(0)=11+e0=11+1=0.5\sigma(0) = \dfrac{1}{1 + e^{0}} = \dfrac{1}{1 + 1} = 0.5.
  2. At x=2x = 2: e−2≈0.13534e^{-2} \approx 0.13534, so σ(2)=11+0.13534=11.13534≈0.8808\sigma(2) = \dfrac{1}{1 + 0.13534} = \dfrac{1}{1.13534} \approx 0.8808.
  3. At x=−3x = -3: e3≈20.0855e^{3} \approx 20.0855, so σ(−3)=11+e3=121.0855≈0.0474\sigma(-3) = \dfrac{1}{1 + e^{3}} = \dfrac{1}{21.0855} \approx 0.0474.

Interpretation: the score 0 is a coin flip, the score 2 leans strongly toward the positive class at about 88%, and the score −3-3 leans strongly toward the negative class, with only about 5% on the positive side.

Example
Checking the symmetry and the slope

Use the symmetry and derivative identities to answer two questions without a calculator for the slope.

  1. Symmetry: σ(−2)=1−σ(2)=1−0.8808=0.1192\sigma(-2) = 1 - \sigma(2) = 1 - 0.8808 = 0.1192.
  2. Slope at x=2x = 2 using σ′(x)=σ(x)(1−σ(x))\sigma'(x) = \sigma(x)(1 - \sigma(x)): σ′(2)=0.8808×0.1192≈0.1050\sigma'(2) = 0.8808 \times 0.1192 \approx 0.1050.
  3. Compare with the slope at the centre: σ′(0)=0.25\sigma'(0) = 0.25, 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.

SituationWhat goes wrongBetter choice
Deep stack of sigmoid layersThe 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 classesIndependent sigmoids do not sum to 1Softmax over the class scores
Output should be unbounded, such as a regression targetThe bounded range clips the answerA linear output with no squashing
Very large inputse−xe^{-x} can overflow in floating point for large negative xxUse 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 σ(x)\sigma(x) as x→+∞x \to +\infty and as x→−∞x \to -\infty, and what is σ(0)\sigma(0)?Show answer

As xx grows large and positive, σ(x)\sigma(x) approaches 1. As xx becomes large and negative, σ(x)\sigma(x) approaches 0. At the centre, σ(0)=11+e0=12\sigma(0) = \tfrac{1}{1 + e^{0}} = \tfrac{1}{2}.

QuestionComputation: evaluate σ(1)\sigma(1) to three decimal places. Use e≈2.718e \approx 2.718.Show answer

We need e−1≈1/2.718≈0.3679e^{-1} \approx 1/2.718 \approx 0.3679. Then σ(1)=11+0.3679=11.3679≈0.731\sigma(1) = \dfrac{1}{1 + 0.3679} = \dfrac{1}{1.3679} \approx 0.731.

QuestionWhy: explain why the derivative of the sigmoid can be written as σ(x)(1−σ(x))\sigma(x)(1 - \sigma(x)), and why this form is convenient during training.Show answer

Differentiating 11+e−x\dfrac{1}{1 + e^{-x}} gives e−x(1+e−x)2\dfrac{e^{-x}}{(1 + e^{-x})^2}. Splitting this into the product of 11+e−x\dfrac{1}{1 + e^{-x}} and e−x1+e−x\dfrac{e^{-x}}{1 + e^{-x}} gives σ(x)\sigma(x) and 1−σ(x)1 - \sigma(x). During training the forward pass has already computed σ(x)\sigma(x), 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 −4-4 mean for the same model?Show answer

A score of 0 gives σ(0)=0.5\sigma(0) = 0.5, so the model is undecided. A score of −4-4 gives σ(−4)=11+e4≈11+54.6≈0.018\sigma(-4) = \dfrac{1}{1 + e^{4}} \approx \dfrac{1}{1 + 54.6} \approx 0.018, 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