Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsPre-calculus Refresher

The sigmoid, tanh and other squashing functions

P1.A540 min●○○ BeginnerReviewed Nov 30, 2026

Why this matters for machine learning

Models compute scores that can be any real number, and squashing functions turn those scores into bounded, usable outputs: probabilities, gates and activations. Their flat tails are also the reason some deep networks learn slowly.

  • Logistic regression outputs σ(w⋅x+b)\sigma(\mathbf{w}\cdot\mathbf{x} + b) as the probability of the positive class. (P3.A7: Logistic regression, coming soon)
  • Sigmoid and tanh are classic activation functions, applied to the output of each neuron in a neural network. (P4.4: Activation functions, coming soon)
  • When sigmoid or tanh saturate, their slope is nearly 0, so learning signals shrink as they pass back through many layers (vanishing gradients). (P4.16: Vanishing and exploding gradients, coming soon)
  • LSTM gates use sigmoid (a number from 0 to 1 saying how much to let through) and tanh (a value from −1 to 1). (P6.7: LSTM, coming soon)

Before this: Functions and graphs, Exponents and logarithms

After this lesson you can

  • Build σ(z) = 1/(1 + e^(−z)) step by step, state its range (0, 1), σ(0) = 0.5 and σ(−z) = 1 − σ(z), and use the logit ln(p/(1 − p)) to undo it
  • State tanh's formula and range (−1, 1), and show that tanh(z) = 2σ(2z) − 1
  • Explain saturation: measure the slope of a curve numerically and see that it is largest at 0 (0.25 for σ, 1 for tanh) and almost 0 far out
  • Stretch and shift σ(a·z + b) and find where it crosses 0.5, and name other squashing functions (step, hard sigmoid, softsign), including how softmax generalizes σ
  • Compute the sigmoid safely in code, and know why the naive formula can overflow

A model computes a score from its inputs. That score can be any real number: −3.2-3.2, 00, 4141. But often we need something tamer. A probability must be between 0 and 1. A gate that decides “how much to let through” must be between 0 and 1. A feature that should not explode should stay in a small range.

The tool for this is a squashing function: a function that takes any real number and returns a number in a fixed, bounded range. In this lesson a squashing function is an S-shaped curve that rises (or at least never falls) and levels off at both ends. This is the last lesson of the precalculus module, so it uses everything before it: functions and transformations (P1.A1), exe^x (P1.A2) and notation (P1.A4). And it ends with a question, “how steep is this curve?”, that is the doorway to calculus.

Build the sigmoid, one step at a time

The most famous squashing function is the sigmoid (also called the logistic function):

σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

(σ\sigma is the Greek letter sigma; in this course it is the sigmoid, as in P1.A4. The name comes from “S-shaped”.) Build it in four small steps, starting from zz:

  1. Flip the sign: −z-z.
  2. Exponentiate: e−ze^{-z}. This is always positive. It is huge when zz is very negative and tiny when zz is large and positive (P1.A2).
  3. Add 1: 1+e−z1 + e^{-z}. Now the number is always greater than 1.
  4. Take the reciprocal: 1/(1+e−z)1/(1 + e^{-z}). A number greater than 1 has a reciprocal between 0 and 1.

Try it on a few values:

zze−ze^{-z}1+e−z1 + e^{-z}σ(z)=1/(1+e−z)\sigma(z) = 1/(1+e^{-z})
−6403.43404.430.0025
−454.6055.600.0180
−27.3898.3890.1192
0120.5
20.13531.13530.8808
40.01831.01830.9820
60.00251.00250.9975

Look down the last column: it climbs from near 0 to near 1, passing 0.5 at z=0z = 0. That is the S-curve.

What the table tells you

  • Range (0,1)(0, 1). Step 2 gives a positive number, so the denominator is strictly greater than 1, so σ(z)\sigma(z) is strictly between 0 and 1. It never reaches 0 or 1, but gets as close as you like. The lines y=0y = 0 and y=1y = 1 are horizontal asymptotes: lines the curve approaches but never touches.
  • σ(0)=0.5\sigma(0) = 0.5, because e0=1e^{0} = 1 and 1/(1+1)=1/21/(1+1) = 1/2. Zero score means “undecided”.
  • Always increasing. Bigger zz makes e−ze^{-z} smaller, so σ(z)\sigma(z) is bigger.
  • Symmetry: σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z). Check with z=3z = 3: σ(3)=0.9526\sigma(3) = 0.9526 and σ(−3)=0.0474\sigma(-3) = 0.0474, and 0.9526+0.0474=10.9526 + 0.0474 = 1. Proof: σ(−z)=11+ez\sigma(-z) = \dfrac{1}{1+e^{z}}. Multiply top and bottom by e−ze^{-z} to get e−ze−z+1\dfrac{e^{-z}}{e^{-z}+1}, and that is 1−11+e−z=1−σ(z)1 - \dfrac{1}{1+e^{-z}} = 1 - \sigma(z). In words: the curve is balanced around the point (0,0.5)(0, 0.5). Flip it left-to-right and the result is the same as flipping it upside-down about the height 0.5.

The sigmoid as a probability, and the logit

Because the output is in (0,1)(0, 1), we can read it as a probability: σ(z)=0.88\sigma(z) = 0.88 means “88% likely”. The input zz is called the logit (or log-odds) when used this way. The reason is below.

If an event has probability pp, its odds are p/(1−p)p/(1-p). For p=0.8p = 0.8 the odds are 0.8/0.2=40.8/0.2 = 4 (“4 to 1”). The logit is the natural log of the odds:

logit⁡(p)=ln⁡p1−p.\operatorname{logit}(p) = \ln\frac{p}{1-p}.

The logit undoes the sigmoid. Start from p=1/(1+e−z)p = 1/(1+e^{-z}) and solve for zz: 1/p=1+e−z1/p = 1 + e^{-z}, so e−z=(1−p)/pe^{-z} = (1-p)/p, so −z=ln⁡1−pp-z = \ln\frac{1-p}{p}, so z=ln⁡p1−pz = \ln\frac{p}{1-p}. Numerically: p=0.8p = 0.8 gives ln⁡4=1.386\ln 4 = 1.386, and σ(1.386)=0.8\sigma(1.386) = 0.8. So σ\sigma turns a score into a probability, and the logit turns the probability back into the score. Equal odds (p=0.5p = 0.5) give logit ln⁡1=0\ln 1 = 0, which matches σ(0)=0.5\sigma(0) = 0.5.

tanh: the zero-centred cousin

The hyperbolic tangent, written tanh⁡\tanh, is another S-curve:

tanh⁡(z)=ez−e−zez+e−z.\tanh(z) = \frac{e^{z} - e^{-z}}{e^{z} + e^{-z}}.
  • Range (−1,1)(-1, 1): when zz is large, e−ze^{-z} is tiny and the fraction is about ez/ez=1e^{z}/e^{z} = 1. When zz is very negative, it is about −1-1.
  • tanh⁡(0)=0\tanh(0) = 0: the top is 1−1=01 - 1 = 0. It is zero-centred: its outputs are centred on 0, not on 0.5. One reason often given for preferring it over the sigmoid in hidden layers is that outputs centred on 0 are easier for the next layer to work with (more in P4.4).
  • Odd function: tanh⁡(−z)=−tanh⁡(z)\tanh(-z) = -\tanh(z). The graph is unchanged if you rotate it half a turn about the origin. (Swap z→−zz \to -z in the formula: the top changes sign, the bottom stays the same.)

tanh is a transformed sigmoid

From P1.A1: you can build new curves by shifting and stretching old ones. It turns out

tanh⁡(z)=2 σ(2z)−1.\tanh(z) = 2\,\sigma(2z) - 1.

Derivation: 2σ(2z)−1=21+e−2z−1=2−1−e−2z1+e−2z=1−e−2z1+e−2z2\sigma(2z) - 1 = \dfrac{2}{1+e^{-2z}} - 1 = \dfrac{2 - 1 - e^{-2z}}{1+e^{-2z}} = \dfrac{1 - e^{-2z}}{1+e^{-2z}}. Multiply top and bottom by eze^{z} and you get ez−e−zez+e−z\dfrac{e^{z} - e^{-z}}{e^{z}+e^{-z}}. That is tanh⁡(z)\tanh(z).

Read it with the rules of P1.A1: the 2z2z inside squeezes the curve sideways by a factor 2; the 2⋅2\cdot outside stretches it twice as tall (range (0,2)(0,2)); the −1-1 outside moves it down (range (−1,1)(-1,1)). Number check: σ(2)=0.8808\sigma(2) = 0.8808, so 2(0.8808)−1=0.7616=tanh⁡(1)2(0.8808) - 1 = 0.7616 = \tanh(1). So tanh and sigmoid are not unrelated: they are the same S-curve in different clothes.

Play with the curves

Drag the input zz along the curves. Watch the outputs, and watch the orange slope line. It shows how steeply the chosen curve is rising at zz; we will explain the number next. Turn on the other curves to compare.

Squashing functions: drag the input z

−6−4−2246−1−0.50.51zy = 1y = −1y = 0
curveoutput f(z)slope at z
sigmoid σ 0.731 0.196
tanh 0.762 0.417
Orange slope line on:

sigmoid σ: slope 0.196. Still responsive: the output reacts to changes in z.

Drag the ring under the plot, use the slider, or focus the ring and press the arrow keys (Page Up and Page Down move by 1, Home returns to 0). Slope is measured with numbers: (f(z + 0.01) − f(z)) / 0.01. The orange line shows it as a tilt. Dashed grey lines mark 1, 0 and −1, the edges of the ranges: sigmoid and tanh approach them without touching, while the hard sigmoid and step reach them.

Squashing functions with a draggable input z. Blue is the sigmoid, pink is tanh; you can also show softsign, the hard sigmoid and the step function. The ring under the plot sets z (drag it, use the slider, or focus it and use the arrow keys). Each curve's output at z is shown as a dot and in the table. The orange line is the tangent-like slope line, and the orange column gives the slope, measured as (f(z + 0.01) − f(z)) / 0.01. Dashed grey lines mark the values 1, 0 and −1: the edges of the ranges.

Things to try: put zz at 0, then at 2, then at 5, and watch the orange slope shrink. Switch the slope line from sigmoid to tanh. Then drag zz to −5-5.

Saturation: where the curve goes flat

Look at the table again. Going from z=2z = 2 to z=4z = 4 changes σ\sigma from 0.8808 to 0.9820, a gain of 0.10. Going from z=4z = 4 to z=6z = 6 changes it from 0.9820 to 0.9975, a gain of only 0.016. The curve is flattening out. This flattening is called saturation: for large ∣z∣|z| the output is “full” and hardly reacts to a change in the input. It is the same at the left end (z=−6z = -6 gives 0.0025, nearly 0).

Measuring steepness with numbers (no calculus yet)

How do we say “how steep” with a number? Use the idea of slope you know from straight lines: rise over run. For a curve we use a tiny run. The local slope of ff at zz is about

slope≈f(z+0.01)−f(z)0.01\text{slope} \approx \frac{f(z + 0.01) - f(z)}{0.01}

(“how much does the output change, per unit of input, if I nudge the input by 0.01?”). The widget shows this in orange. Some values:

zzσ(z)\sigma(z) slopetanh⁡(z)\tanh(z) slope
00.2501.000
10.1960.417
20.1050.070
40.0180.001
60.00250.000

(Rounded. The exact slopes at −z-z and at +z+z are the same, because both curves are symmetric: the left half is the right half turned around. The measured slopes differ slightly in the third or fourth decimal, because the measuring step +0.01+0.01 only looks to the right.) Two facts to remember:

  • The slope is largest at z=0z = 0: 0.250.25 for the sigmoid and 11 for tanh. (tanh is steeper because of the stretch in 2σ(2z)−12\sigma(2z) - 1.)
  • The slope is nearly 0 far from 0, for both curves, and for tanh it falls faster.

This is the heart of saturation: where the slope is near 0, the output barely responds to the input, and (as you will see in calculus) learning signals that flow through such a spot are cut down too.

A first look at why this matters for training

In a neural network, training adjusts a weight by looking at how much the final output changes when that weight changes. If the signal passes through a saturated sigmoid, it is multiplied by a slope of about 0.00250.0025 (at z=±6z = \pm 6). Even in the best case (the slope 0.250.25 at z=0z = 0), each sigmoid layer multiplies the signal by at most 0.250.25. Stack 5 layers and the factor is 0.255≈0.0010.25^5 \approx 0.001; stack 10 and it is 0.2510≈0.0000010.25^{10} \approx 0.000001. (This is a simplified picture that ignores the weights; the full story, with the chain rule, is in P1.C3, and the effect itself is the vanishing gradient problem of P4.16.) Part of why modern networks use other activations, such as ReLU, is to avoid this (P4.4).

The exact slope of a curve is the derivative, and measuring it with numbers is the first step to the idea of P1.C1: shrink the 0.01 towards 0.

Other squashing and S-shaped functions

Sigmoid and tanh are the stars, but you will meet several relatives. All of these map every real number to a bounded range:

NameFormulaRangeNotes
step11 if z>0z > 0, else 00{0,1}\{0, 1\}A hard yes/no. The value at exactly z=0z=0 is a convention (0, 0.5 or 1). Slope is 0 everywhere except a jump.
hard sigmoidmin⁡(1,max⁡(0,z/6+0.5))\min(1, \max(0, z/6 + 0.5))[0,1][0, 1]A cheap straight-line version of σ\sigma that reaches 0 and 1 exactly. Definitions vary between libraries (the 1/61/6 slope here is the one in PyTorch; some older libraries use 0.2).
softsignz/(1+∣z∣)z / (1 + \lvert z \rvert)(−1,1)(-1, 1)Like tanh, but it approaches ±1\pm 1 slowly: z=10z = 10 gives 0.909, while tanh⁡(10)≈1.0000\tanh(10) \approx 1.0000.
scaled arctan2πarctan⁡(z)\tfrac{2}{\pi}\arctan(z)(−1,1)(-1, 1)Another slow-approaching S-curve; z=10z = 10 gives 0.937.

(Check at z=5z = 5: softsign 0.8330.833, scaled arctan 0.8740.874, tanh⁡\tanh 0.99990.9999.) Turn them on in the widget above to compare. The step is the odd one out: it has no “in-between”, and its slope is 0 almost everywhere, which gives a learning method based on slopes nothing to work with. That is why smooth curves like σ\sigma replaced it in neural networks.

The step as a limit of the sigmoid

Multiply the input by a big number kk: σ(kz)\sigma(kz). Take z=0.5z = 0.5:

kk1520100
σ(0.5k)\sigma(0.5k)0.6220.9240.999951.000 (to 3 decimals)

As kk grows the S gets steeper and steeper, and in the limit it becomes the step for every z≠0z \ne 0. (At z=0z = 0 it stays exactly 0.5 for every kk.) So the step is the “infinitely steep” sigmoid. You can try this with the aa slider below.

Softmax: the sigmoid for many classes

A sigmoid turns one score into a probability of “yes” versus “no”. For more than two classes, softmax (P3.A8) turns a list of scores into probabilities that sum to 1: softmax⁡(z)k=ezk/∑jezj\operatorname{softmax}(\mathbf z)_k = e^{z_k}/\sum_j e^{z_j}. With two classes it is just a sigmoid of the score difference:

ez1ez1+ez2=11+e−(z1−z2)=σ(z1−z2).\begin{aligned} \frac{e^{z_1}}{e^{z_1} + e^{z_2}} &= \frac{1}{1 + e^{-(z_1 - z_2)}} \\ &= \sigma(z_1 - z_2). \end{aligned}

(Divide top and bottom by ez1e^{z_1}.) Check with z=(2,0.5)\mathbf z = (2, 0.5): e2/(e2+e0.5)=0.8176e^{2}/(e^{2} + e^{0.5}) = 0.8176 and σ(1.5)=0.8176\sigma(1.5) = 0.8176.

Stretch and shift: σ(a·z + b)

In models the sigmoid is almost never applied to a bare zz. It is applied to a linear score az+ba z + b: a multiplier aa and an offset bb (these are a weight and a bias in P3.A7). By the rules of P1.A1 these are transformations inside the function:

  • aa changes the steepness: a large ∣a∣|a| squeezes the S sideways; a negative aa mirrors it (recall σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z)).
  • bb moves the curve left or right (inside changes act backwards): σ(az+b)=σ(a(z−c))\sigma(az + b) = \sigma\bigl(a(z - c)\bigr) with c=−b/ac = -b/a.
  • The curve crosses the height 0.5 exactly where the input of σ\sigma is 0: az+b=0az + b = 0, i.e. z=−b/az = -b/a.

Worked example: σ(2z−3)\sigma(2z - 3). The crossing is at z=3/2=1.5z = 3/2 = 1.5. Check: σ(2⋅1.5−3)=σ(0)=0.5\sigma(2\cdot 1.5 - 3) = \sigma(0) = 0.5. Also σ(2⋅0−3)=σ(−3)=0.0474\sigma(2\cdot 0 - 3) = \sigma(-3) = 0.0474 and σ(2⋅2−3)=σ(1)=0.7311\sigma(2\cdot 2 - 3) = \sigma(1) = 0.7311. Make the curve steeper with a larger aa and the crossing stays at 1.51.5 if bb is scaled along with it.

Stretch and shift the sigmoid

σ(1·z + 0)

−6−4−2024600.51z

The curve passes through 0.5 at z = −b/a = 0 / 1 = 0. Slope there (measured numerically): 0.25, about a/4 = 0.25.

Dashed grey is the plain σ(z). The pink dot marks where the new curve crosses 0.5.

Stretch and shift the sigmoid: σ(a·z + b). Dashed grey is the plain σ(z). Drag the a slider to change steepness (negative mirrors the curve, a = 10 is almost a step) and the b slider to shift. The pink dot marks where the curve crosses 0.5, at z = −b/a. The readout also gives the slope there, measured numerically, which is about a/4.

Notice: the slope at the crossing is about a/4a/4 (the plain sigmoid has 0.250.25, and stretching sideways by aa multiplies the steepness by aa). With a=4a = 4 the slope is 1. And z=−b/az = -b/a is a decision point: for logistic regression it is where the model switches from “more likely no” to “more likely yes” (P3.A7).

Numbers in code: overflow and the stable form

Here is the sigmoid as code, straight from the formula:

def sigmoid_naive(z):
    return 1 / (1 + np.exp(-z))

Problem: e−ze^{-z} is enormous when zz is very negative, and a computer number has a limit. In standard double precision, math.exp(709) works (about 8.2×103078.2\times10^{307}) but math.exp(1000) raises OverflowError. NumPy does not raise: np.exp(1000) gives inf with a RuntimeWarning. For the naive sigmoid at z=−1000z = -1000, that gives 1/(1+∞)=01/(1+\infty) = 0, which happens to be the right answer, but with a warning (and with Python’s math module it would crash). Warnings and infinities cause real trouble when they flow into the next calculation (this is previewed in P1.G2).

The fix is to never compute the exponential of a large positive number. Use two equal forms:

σ(z)={11+e−zz≥0ez1+ezz<0\sigma(z) = \begin{cases} \dfrac{1}{1 + e^{-z}} & z \ge 0 \\[2mm] \dfrac{e^{z}}{1 + e^{z}} & z < 0 \end{cases}

They are equal: multiply the top and bottom of the first by eze^{z}. In both branches the exponent is ≤0\le 0, so ethat≤1e^{\text{that}} \le 1 and nothing overflows. This is the stable version, and it is how libraries do it.

One more fact that surprises people: in floating point, σ(z)\sigma(z) is exactly 1.0 for whole numbers z≥37z \ge 37 (the rounding to 1.0 starts between z=36.7z = 36.7 and 36.836.8), because e−37≈8.5×10−17e^{-37} \approx 8.5\times 10^{-17} is too small to change 1+e−371 + e^{-37}. So the “never reaches 1” rule is a statement about real numbers, not about computers. This matters when you take log⁡(1−p)\log(1 - p) later (P1.G2, P3.A7).

Try it in Python

Run the cell: it builds sigmoid and tanh, checks the identities numerically, measures slopes, and shows the overflow and the stable version. Then change the numbers.

Try it: sigmoid, tanh, identities, slope, overflow

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Challenge: change at to −3-3 and check that the slope matches +3+3 to two decimals (symmetry; the measured slopes differ a little after that, because the step only looks to the right). Then find the smallest whole number zz for which sigmoid(z) == 1.0.

Intuition

A squashing function is a soft switch. Far to the left it says “off” (about 0), far to the right it says “on” (about 1), and near 0 it blends between the two. The steepness controls how sudden the switch is; the shift controls where it flips. The flat ends are what make the output bounded, and also what makes the output insensitive there (saturation). Where the picture breaks: a real switch is exactly on or off; a sigmoid never quite is.

Math

σ(z)=11+e−z=ez1+ez,σ(−z)=1−σ(z),logit⁡(σ(z))=z,\begin{aligned} \sigma(z) &= \frac{1}{1+e^{-z}} = \frac{e^{z}}{1+e^{z}}, \\ \sigma(-z) &= 1 - \sigma(z), \\ \operatorname{logit}(\sigma(z)) &= z, \end{aligned}tanh⁡(z)=ez−e−zez+e−z=2σ(2z)−1,tanh⁡(−z)=−tanh⁡(z).\begin{aligned} \tanh(z) &= \frac{e^{z}-e^{-z}}{e^{z}+e^{-z}} \\ &= 2\sigma(2z) - 1, \\ \tanh(-z) &= -\tanh(z). \end{aligned}

Limits: σ(z)→1\sigma(z) \to 1 as z→∞z \to \infty and →0\to 0 as z→−∞z \to -\infty; tanh⁡(z)→±1\tanh(z) \to \pm 1. For a score az+ba z + b with a≠0a \ne 0, the crossing of 0.5 is at z=−b/az = -b/a. Two classes of softmax: softmax⁡(z1,z2)1=σ(z1−z2)\operatorname{softmax}(z_1, z_2)_1 = \sigma(z_1 - z_2).

Code

import numpy as np
def sigmoid(z):                    # stable form
    z = np.asarray(z, dtype=float)
    e = np.exp(-np.abs(z))         # exponent is never positive
    return np.where(z >= 0, 1 / (1 + e), e / (1 + e))

sigmoid(0.0)                       # 0.5
np.tanh(1.0)                       # 0.7616 = 2*sigmoid(2) - 1
sigmoid(-1000.0)                   # 0.0, no warning
Go deeper Preview: the exact slope of σ and tanh

In calculus (P1.C1 to P1.C3) you will show that the slope of the sigmoid at zz has a neat closed form, σ′(z)=σ(z) (1−σ(z))\sigma'(z) = \sigma(z)\,(1 - \sigma(z)), and for tanh it is tanh⁡′(z)=1−tanh⁡2(z)\tanh'(z) = 1 - \tanh^2(z). You do not need to prove them now: just check them against the numbers above.

  • At z=0z = 0: σ(0)(1−σ(0))=0.5×0.5=0.25\sigma(0)(1 - \sigma(0)) = 0.5 \times 0.5 = 0.25 and 1−tanh⁡2(0)=11 - \tanh^2(0) = 1. These match the measured slopes 0.250.25 and 11.
  • At z=2z = 2: 0.8808×0.1192=0.10500.8808 \times 0.1192 = 0.1050 (measured 0.10460.1046), and 1−0.96402=0.07071 - 0.9640^2 = 0.0707 (measured 0.07000.0700).

The measured numbers are very slightly lower because a step of 0.01 is not infinitely small and the curve bends. This also explains why the largest slope of σ\sigma is 0.250.25: the product p(1−p)p(1-p) is largest when p=0.5p = 0.5, where it equals 0.250.25.

Go deeper Why the logistic curve, and where tanh shows up

The sigmoid is exactly the curve that turns log-odds into a probability. If a model assumes the log-odds of “yes” is a linear score z=w⋅x+bz = \mathbf{w}\cdot\mathbf{x} + b, then the probability is σ(z)\sigma(z): that is logistic regression (P3.A7). The LSTM (P6.7) uses sigmoids for gates, numbers between 0 and 1 that multiply a signal to let a fraction of it through, and tanh to produce candidate values between −1-1 and 11.

Exercises

  1. Compute σ(1)\sigma(1) and σ(−1)\sigma(-1) and check that they add up to 1.
    Show answerσ(1)=1/(1+e−1)=1/1.3679=0.7311\sigma(1) = 1/(1 + e^{-1}) = 1/1.3679 = 0.7311 and σ(−1)=0.2689\sigma(-1) = 0.2689. Sum =1= 1 (by σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z)).
  2. A model says the probability of rain is p=0.9p = 0.9. What logit (score) does that correspond to?
    Show answerln⁡(0.9/0.1)=ln⁡9=2.197\ln(0.9/0.1) = \ln 9 = 2.197. Check: σ(2.197)=0.9\sigma(2.197) = 0.9.
  3. Use σ(4)=0.9820\sigma(4) = 0.9820 and the identity to compute tanh⁡(2)\tanh(2).
    Show answertanh⁡(2)=2σ(4)−1=2(0.9820)−1=0.9640\tanh(2) = 2\sigma(4) - 1 = 2(0.9820) - 1 = 0.9640 (more exactly 0.96400.9640 with σ(4)=0.98201\sigma(4) = 0.98201).
  4. Where does σ(3z−6)\sigma(3z - 6) cross 0.5? What are its values at z=0z = 0 and z=3z = 3?
    Show answerz=−b/a=6/3=2z = -b/a = 6/3 = 2. At z=0z = 0: σ(−6)=0.0025\sigma(-6) = 0.0025. At z=3z = 3: σ(3)=0.9526\sigma(3) = 0.9526.
  5. A stack of 10 sigmoid layers, each with its best-case slope 0.250.25. By what factor (at most) does the signal shrink in this simplified picture?
    Show answer0.2510≈9.5×10−70.25^{10} \approx 9.5\times 10^{-7}, less than one millionth. (Simplified: ignores the weights.)
  6. A two-class softmax has scores (1,3)(1, 3). What is the probability of class 2?
    Show answerσ(3−1)=σ(2)=0.8808\sigma(3 - 1) = \sigma(2) = 0.8808. Directly: e3/(e1+e3)=20.09/22.80=0.8808e^3/(e^1 + e^3) = 20.09/22.80 = 0.8808. Class 1 gets 0.11920.1192.
  7. In floating point, is σ(50)\sigma(50) equal to 1? Is it mathematically? And is 1−σ(50)1 - \sigma(50) equal to 0 in floating point?
    Show answerIn float64, σ(50)\sigma(50) rounds to exactly 1.0, and 1−σ(50)=0.01 - \sigma(50) = 0.0. Mathematically σ(50)<1\sigma(50) < 1 always: 1−σ(50)=σ(−50)≈1.9×10−221 - \sigma(50) = \sigma(-50) \approx 1.9\times 10^{-22}. This is why stable code computes σ(−z)\sigma(-z) rather than 1−σ(z)1 - \sigma(z) when the small number matters.
  8. The hard sigmoid min⁡(1,max⁡(0,z/6+0.5))\min(1, \max(0, z/6 + 0.5)) at z=1z = 1 gives what, and how close is it to σ(1)\sigma(1)?
    Show answer1/6+0.5=0.66671/6 + 0.5 = 0.6667, versus σ(1)=0.7311\sigma(1) = 0.7311. About 0.0640.064 apart.
  9. Which of these is not a squashing function: softsign, 2πarctan⁡z\tfrac{2}{\pi}\arctan z, ReLU, step?
    Show answerReLU: its outputs are unbounded above. The other three are bounded (softsign and arctan in (−1,1)(-1,1), step in {0,1}\{0,1\}).
  10. Why does a naive 1/(1+e−z)1/(1 + e^{-z}) risk trouble for z=−1000z = -1000 in Python’s math module?
    Show answere1000e^{1000} is bigger than the largest double (≈1.8×10308\approx 1.8\times10^{308}), so math.exp(1000) raises OverflowError. NumPy returns inf with a warning instead. The stable form uses ez/(1+ez)e^{z}/(1+e^{z}) for negative zz, where eze^{z} is tiny.

Quiz

Check your understanding

  1. What values can the sigmoid σ(z)=1/(1+e−z)\sigma(z) = 1/(1+e^{-z}) output (mathematically, as real numbers)?

  2. If σ(2)=0.88\sigma(2) = 0.88, what is σ(−2)\sigma(-2)?

  3. Using tanh⁡(z)=2σ(2z)−1\tanh(z) = 2\sigma(2z) - 1 and σ(2)=0.8808\sigma(2) = 0.8808, what is tanh⁡(1)\tanh(1)?

  4. Why can deep stacks of sigmoid or tanh layers learn slowly?

  5. Which of these is a squashing function (it maps every real number into a bounded range)?

Putting it together

A squashing function turns any real number into a bounded output. The sigmoid 1/(1+e−z)1/(1+e^{-z}) lives in (0,1)(0,1), equals 0.5 at 0, satisfies σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z), and is read as a probability; its inverse is the logit ln⁡(p/(1−p))\ln(p/(1-p)). tanh lives in (−1,1)(-1,1) and is 2σ(2z)−12\sigma(2z) - 1. Both saturate: far from 0 the curve is flat, and the slope, which is largest at 0 (0.25 and 1), is nearly 0. σ(az+b)\sigma(az+b) crosses 0.5 at z=−b/az = -b/a, and a huge aa gives a step. Softmax with two classes is a sigmoid. In code, use the stable form to avoid overflow.

Where next. You have measured steepness with a tiny step of 0.01. Calculus (P1.C1) turns this into an exact tool: the derivative is what you get as the step shrinks to zero, and the chain rule (P1.C3) explains how slopes multiply through layers. Then the sigmoid appears as the output of logistic regression (P3.A7) and as an activation in neural networks (P4.4).

Key takeaways

  • A squashing function takes any real number and returns a number in a fixed range. The sigmoid σ(z)=1/(1+e−z)\sigma(z) = 1/(1 + e^{-z}) squashes into (0,1)(0, 1), so its output can be read as a probability. It equals 0.5 at z=0z = 0, and σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z).
  • tanh⁡(z)\tanh(z) squashes into (−1,1)(-1, 1), is zero at z=0z = 0, is an odd function, and is a stretched, shifted sigmoid: tanh⁡(z)=2σ(2z)−1\tanh(z) = 2\sigma(2z) - 1.
  • Saturation: for large ∣z∣|z| both curves are nearly flat, so the output barely changes when the input changes. The slope is largest at z=0z = 0 (exactly 0.25 for sigmoid and 1 for tanh; the measured value (f(z+0.01)−f(z))/0.01(f(z+0.01) - f(z))/0.01 comes out a hair below that) and close to 0 far out. This is why deep stacks of these functions can train slowly (P4.16).
  • The logit ln⁡(p/(1−p))\ln(p/(1-p)) is the inverse of the sigmoid: it turns a probability back into a score. σ(az+b)\sigma(a z + b) is a stretched and shifted sigmoid that crosses 0.5 at z=−b/az = -b/a; a very large aa approaches the step function. Softmax with two classes is a sigmoid of the score difference. ReLU is not a squashing function.
  • In code, e−ze^{-z} overflows for very negative zz in the naive formula. Compute ez/(1+ez)e^{z}/(1+e^{z}) for negative zz instead (the stable form). In floating point, σ(z)\sigma(z) rounds to exactly 1.0 for z≥37z \ge 37, even though mathematically it never reaches 1.
Found a mistake? Tell us