Find a course

Module 1: The Artificial NeuronLesson 3 of 1115 min

Activation Functions

Learn why networks need nonlinear activation functions, compare sigmoid, tanh, ReLU and softmax, and see how a hidden layer solves XOR.

Step 1 of 13Learn

Why stacking plain layers gets you nowhere

Suppose you drop the activation function and let each neuron output z directly. A layer then computes Wx + b, a linear function. Stack a second layer on top and you get W2(W1x + b1) + b2. Multiply it out and it becomes (W2W1)x + (W2b1 + b2), which is again just one matrix times x plus one vector. Any number of linear layers collapses into a single linear layer. A 50-layer network without activations is no more capable than one with zero hidden layers: it can only draw straight lines (or flat planes) through the data.

The fix is to pass each z through a nonlinear activation function f, so a neuron outputs a = f(z). Now layers can bend, fold and combine boundaries, and stacking them adds real power. This is why the activation function, a seemingly small detail, is the reason deep networks can recognize faces or translate languages.

The step function from the last lesson is nonlinear, but it has a fatal flaw for training: it is flat everywhere except at a jump. Training relies on slopes, and a flat function has slope zero, so it gives no hint about which way to change a weight. Modern networks use smooth or piecewise-linear functions instead.

Step 2 of 13Practice · True or false

A network with ten hidden layers but no activation functions can model curved decision boundaries that a single linear layer cannot.

Step 3 of 13Learn

The sigmoid: a smooth switch

The sigmoid (or logistic) function squeezes any number into the range 0 to 1:

σ(z) = 1 / (1 + e^−z)

Key values to remember: σ(0) = 0.5, σ(2) ≈ 0.88, σ(−2) ≈ 0.12, and for z beyond about ±5 the output is practically 1 or 0. It looks like a smoothed-out step, which made it the default choice for decades. Its output can be read as a probability, which is why it is still used in the output neuron of yes/no classifiers ("Is this email spam?").

Inside hidden layers, though, the sigmoid has a problem called saturation. When z is large in either direction, the curve is almost flat, so its slope is nearly zero. Even at its steepest point (z = 0) the slope is only 0.25. During training, these small slopes get multiplied together layer after layer, and the learning signal fades away before it reaches the early layers. This is the vanishing gradient problem, and you'll see its math in the backpropagation lesson.

In the explorer below, the weight w controls how steep the curve is and the bias b slides it left or right. The point where the output crosses 0.5 is at x = −b/w.

Step 4 of 13Explore

Shape a sigmoid neuron

The curve shows σ(w·x + b) for a single input x. Make w large to approach a step function; make it negative to flip the curve.

00.20.40.60.81 -505
Output at x = 0
0.5
x where output = 0.5
0
Steepest slope (w/4)
0.25

Step 5 of 13Practice · Work it out

What is σ(2), the sigmoid of 2? Give your answer to two decimal places.

Step 6 of 13Learn

ReLU, tanh and softmax

ReLU (rectified linear unit) is the workhorse of modern hidden layers: ReLU(z) = max(0, z). Negative inputs become 0; positive inputs pass through unchanged. It looks almost too simple, but it has two big advantages. It is extremely cheap to compute, and for positive z its slope is exactly 1, so gradients don't shrink as they pass through. Its weakness is the dying ReLU: a neuron whose z is negative for every input outputs 0 forever and stops learning. Leaky ReLU fixes this by letting a small slope through for negatives, such as 0.01·z.

Tanh (hyperbolic tangent) is a stretched sigmoid with range −1 to 1 and tanh(0) = 0. Being centered on zero helps training compared with sigmoid, but it still saturates. You'll find it inside older recurrent networks.

Softmax is used only in the output layer of classifiers with several classes. It takes a vector of scores and turns them into probabilities that are all positive and add up to 1: each output is e^zi divided by the sum of e^zj over all classes. For scores (1, 2, 3), softmax gives about (0.09, 0.24, 0.67). The largest score always gets the largest probability, but the exponent exaggerates gaps between scores.

Step 7 of 13Chart

Sigmoid, tanh and ReLU side by side

The same inputs z pushed through three activation functions. Notice the ranges: sigmoid stays in 0 to 1, tanh in −1 to 1, ReLU is 0 then a straight line.

Sigmoid, tanh and ReLU side by sideLine chart with 7 categories: −3: Sigmoid 0.05, tanh -1, ReLU 0; −2: Sigmoid 0.12, tanh -0.96, ReLU 0; −1: Sigmoid 0.27, tanh -0.76, ReLU 0; 0: Sigmoid 0.5, tanh 0, ReLU 0; 1: Sigmoid 0.73, tanh 0.76, ReLU 1; 2: Sigmoid 0.88, tanh 0.96, ReLU 2; 3: Sigmoid 0.95, tanh 1, ReLU 3.-10123output−3−2−10123z
Values computed from the formulas and rounded to two decimals.
Sigmoid, tanh and ReLU side by side
zSigmoidtanhReLU
−30.05-10
−20.12-0.960
−10.27-0.760
00.500
10.730.761
20.880.962
30.9513

Step 8 of 13Compare

Activation functions at a glance

AspectSigmoidTanhReLUSoftmax
Formula1 / (1 + e^−z)(ez − e^−z) / (ez + e^−z)max(0, z)e^zi / Σ e^zj
Output range0 to 1−1 to 10 to infinity0 to 1, outputs sum to 1
Typical useOutput of yes/no classifiersOlder recurrent networksHidden layers of most modern networksOutput of multi-class classifiers
Main drawbackSaturates; slope at most 0.25Saturates for large |z|Neurons can die (always 0)Only meaningful across a group of outputs

Step 9 of 13Practice · Match the pairs

Match each situation to the activation function that fits it best.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Hidden layers of a deep image classifier
  2. Single output: probability that a tumor is malignant
  3. Output layer choosing one of 10 digits
  4. Hidden state of a classic recurrent network, centered on zero

Step 10 of 13Learn

Solving XOR with a hidden layer

Remember that XOR (output 1 when exactly one input is 1) can't be computed by one neuron, because no single straight line separates (0,1) and (1,0) from (0,0) and (1,1). Add a hidden layer with two step neurons and the problem dissolves:

  • Hidden neuron h1 is an OR gate: weights (1, 1), bias −0.5.
  • Hidden neuron h2 is a NAND gate: weights (−1, −1), bias 1.5.
  • The output neuron is an AND of h1 and h2: weights (1, 1), bias −1.5.

Check (1, 1): OR gives 1, NAND gives 0, AND of 1 and 0 is 0. Correct. Check (1, 0): OR gives 1, NAND gives 1, AND gives 1. Correct.

What happened geometrically? Each hidden neuron draws its own line, and the output neuron combines the two half-planes into a band. The hidden layer has transformed the data into a new representation in which the problem becomes linearly separable. That is the real job of hidden layers in every network: learn features that make the final decision easy. In practice nobody sets these weights by hand; training finds them, and with ReLU or sigmoid units instead of steps.

Step 11 of 13Diagram

XOR built from three neurons

Read it from the top: the output neuron combines two hidden neurons, and each hidden neuron looks at both inputs.

XOR built from three neuronsHierarchy diagram: Output: AND, Hidden h1: OR, Hidden h2: NAND, x1, x2, x1, x2.Output: ANDHidden h1: ORHidden h2: NANDx1x2x1x2

Tap a shape with to see more.

The inputs are drawn twice because every hidden neuron sees both of them.
Output: AND

Fires only when both hidden neurons fire.

Hidden h1: OR

Fires if at least one input is 1. Rules out (0,0).

Hidden h2: NAND

Fires unless both inputs are 1. Rules out (1,1).

x1
x2
x1
x2

Step 12 of 13Practice · Code challenge · Python

Write relu(z) that returns max(0, z), and softmax(scores) that returns a list of probabilities: each math.exp(s) divided by the sum of the exponentials of all scores.

Real Python, running in a sandbox in your browser.
  • relu(-3)
  • relu(2.5)
  • softmax([0, 0])
  • [round(p, 4) for p in softmax([1, 2, 3])]
  • round(sum(softmax([5, -1, 0.3, 2])), 6)

Step 13 of 13Practice · Choose one

A network classifies photos of handwritten digits into 10 classes. Which activation belongs in its output layer?