Neurons and activations
About this lesson
A neural network is built from one small part repeated many times: the neuron. It multiplies each input by a weight, adds a bias, and bends the result with an activation function such as ReLU. Follow numbers through a two-neuron network that computes XOR, then remove the activation and watch it fail.
Numbers in: the XOR task
A neural network works only with numbers. Ours gets two inputs, x1 and x2, each 0 or 1, and must output XOR (exclusive or): 1 when exactly one input is 1, else 0. First input: x = (1, 0).
XOR ("exclusive or")
x1 x2 XOR
0 0 0
0 1 1
1 0 1
1 1 0
first input: x1 = 1, x2 = 0Read the lesson step by step
Numbers in: the XOR task
A neural network works only with numbers. Ours gets two inputs, x1 and x2, each 0 or 1, and must output XOR (exclusive or): 1 when exactly one input is 1, else 0. First input: x = (1, 0).
XOR ("exclusive or") x1 x2 XOR 0 0 0 0 1 1 1 0 1 1 1 0 first input: x1 = 1, x2 = 0A neuron: a weighted sum
A neuron multiplies each input by a weight, adds the results and adds a bias b: z = w1·x1 + w2·x2 + b. Neuron h1 has weights 1 and 1 and bias 0, so z = 1·1 + 1·0 + 0 = 1.
z = w1*x1 + w2*x2 + b h1: w1 = 1, w2 = 1, b = 0 z = 1*1 + 1*0 + 0 = 1 a weight says how much an input counts (and its sign); the bias shifts the sum up or downActivation: ReLU bends it
Then an activation function bends the sum. The usual one is ReLU, the rectified linear unit: a = max(0, z). It passes positive numbers through and turns negative ones into 0. Here z = 1, so h1 outputs a = 1.
a = max(0, z) ReLU z = -3 -> a = 0 z = 0 -> a = 0 z = 2 -> a = 2 h1: z = 1 -> a = 1A bias, and a layer
Neuron h2 reads the same inputs with the same weights, but its bias is −1: z = 1 + 0 − 1 = 0, and ReLU gives 0. Neurons side by side, reading the same inputs, form a layer: here h = (1, 0).
h2: w1 = 1, w2 = 1, b = -1 z = 1*1 + 1*0 - 1 = 0 a = max(0, 0) = 0 hidden layer: h1 and h2 read the same x h = (1, 0)The output neuron
The output neuron weighs the layer's numbers: y = 1·h1 − 2·h2 + 0 = 1 − 0 = 1. It has no ReLU, so it may pass on any number. Exactly one input was 1, so XOR says 1: correct.
y = 1*h1 - 2*h2 + 0 (no activation) = 1*1 - 2*0 + 0 = 1 XOR(1, 0) = 1 correctBoth inputs on
Now x = (1, 1). h1: z = 1 + 1 + 0 = 2. h2: z = 1 + 1 − 1 = 1. ReLU passes both unchanged, so y = 2 − 2·1 = 0. XOR of 1 and 1 is 0: correct.
x = (1, 1) h1: z = 1 + 1 + 0 = 2 a = 2 h2: z = 1 + 1 - 1 = 1 a = 1 y = 1*2 - 2*1 = 0 XOR(1, 1) = 0 correctReLU clips a negative
Now x = (0, 0). h2 gets z = 0 + 0 − 1 = −1, and ReLU clips it to 0, so h = (0, 0) and y = 0: correct. (0, 1) works like (1, 0): all four XOR cases are right.
x = (0, 0) h1: z = 0 + 0 + 0 = 0 a = 0 h2: z = 0 + 0 - 1 = -1 a = max(0, -1) = 0 y = 1*0 - 2*0 = 0 correct all four: 00->0 01->1 10->1 11->0Without the bend: wrong
Take the bend away and h2 passes −1 straight on: y = 0 − 2·(−1) = 2, wrong. Without activations, a sum of weighted sums is just one weighted sum, a straight line, and no straight line separates XOR's 1s from its 0s.
no activation: a = z h2: a = -1 y = 1*0 - 2*(-1) = 2 wrong (XOR says 0) without activations the whole network is y = -x1 - x2 + 2 one weighted sum: a straight lineOther activations
Sigmoid squashes any z into 0 to 1; tanh into −1 to 1. Hidden layers today mostly use ReLU or relatives such as GELU. ReLU is cheap, and its slope is 1 for every positive z, so learning signals do not fade.
ReLU max(0, z) 0 to infinity sigmoid 1 / (1 + e^-z) 0 to 1 (sigmoid(0) = 0.5) tanh tanh(z) -1 to 1 (tanh(0) = 0) slope: ReLU 1 for z > 0; sigmoid at most 0.25Where the weights come from
We copied these weights from the XOR example in the Deep Learning textbook. A real network starts from random weights and learns them: gradient descent nudges every weight and bias to shrink the loss, and backpropagation works out each one's slope.
this network: 9 numbers hidden w = 1, 1 and 1, 1 b = 0, -1 output w = 1, -2 b = 0 training: start random, then repeat backpropagation -> a slope for every weight gradient descent -> w = w - lr * slope
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A neuron has weights 2 and −1 and bias 1, and uses ReLU. What does it output for inputs x1 = 1, x2 = 4?
z = 2·1 + (−1)·4 + 1 = −1. ReLU turns a negative sum into 0, so the output is 0.
02What does the bias do in a neuron?
The bias is added after the weighted sum. In the figure, h2 has the same weights as h1 but bias −1, so it only fires when both inputs are 1.
03In the figure's network, what is the output for x = (0, 1)?
h1: z = 0 + 1 + 0 = 1, a = 1. h2: z = 0 + 1 − 1 = 0, a = 0. y = 1·1 − 2·0 = 1, and XOR of 0 and 1 is 1.
04Why does a network need activation functions between its layers?
A weighted sum of weighted sums is still one weighted sum. The bend an activation adds is what lets extra layers represent more than a straight line.
05Why is ReLU the usual default in hidden layers, instead of sigmoid?
ReLU is just max(0, z): cheap, with slope 1 for any positive z. Sigmoid squashes z into 0 to 1 and its slope is at most 0.25, so signals shrink as they pass back through many layers.
Glossary
- Neuron (artificial)
- A unit that multiplies each input by a weight, adds them and a bias, and passes the sum through an activation function.
- Weight
- A number that says how much one input counts toward a neuron's sum, and in which direction (positive or negative).
- Bias
- A number added to a neuron's weighted sum. It shifts the point where the neuron starts to output more than 0.
- Activation function
- The function a neuron applies to its sum, such as ReLU, sigmoid or tanh. It adds the bend that lets layers do more than a straight line.
- ReLU
- Rectified linear unit: max(0, z). Keeps positive numbers and turns negative ones into 0. The usual default in hidden layers.
- Sigmoid and tanh
- S-shaped activations. Sigmoid squashes any number into 0 to 1; tanh squashes it into −1 to 1.
- Layer
- A group of neurons side by side that all read the same inputs. A hidden layer sits between the inputs and the output.
- Forward pass
- Running inputs through the network, layer by layer, to get its output.
Notes and sources
- The weights are the XOR solution printed in Goodfellow, Bengio and Courville, Deep Learning (2016), section 6.1. Training from random weights usually finds a different set that also works.
- An artificial neuron is loosely inspired by a brain cell, but it is only arithmetic: a weighted sum, a bias and an activation function.
- Without activations, any stack of layers equals a single weighted sum. For XOR that is y = −x1 − x2 + 2 here, which gives 2 for (0, 0) instead of 0.
- The Python program in the lesson is real: run it and it prints the XOR table, 0, 1, 1, 0.
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), chapter 6, section 6.1: Example: Learning XOR
- Stanford CS231n notes, Neural Networks Part 1: the neuron model and common activation functions
- Stanford CS229 lecture notes (A. Ng, T. Ma): deep learning, neural networks and backpropagation
- Nair & Hinton, Rectified Linear Units Improve Restricted Boltzmann Machines (ICML 2010)
- Glorot, Bordes & Bengio, Deep Sparse Rectifier Neural Networks (AISTATS 2011)
- McCulloch & Pitts, A logical calculus of the ideas immanent in nervous activity (1943)
- Rumelhart, Hinton & Williams, Learning representations by back-propagating errors, Nature 323 (1986)
- Google Machine Learning Crash Course: Neural networks, activation functions
- Python documentation: built-in function max()
Quiz Unlocks after the last step.