A smooth logic gate
A neuron does almost the same thing as the function from the last tutorial: it multiplies inputs by weights, adds a bias, and passes the result through an activation function. The classic choice is the sigmoid, which squishes any number into a value between 0 and 1.
import math def sigmoid(z): return 1 / (1 + math.exp(-z)) def neuron(x1, x2, w1, w2, b): z = w1*x1 + w2*x2 + b return sigmoid(z) # a smooth 0-to-1 answer print(neuron(1, 0, 2.0, -1.0, -0.5)) # 0.82
The sigmoid is what makes a neuron “smooth.” A hard logic gate would say “exactly 1 or exactly 0.” A sigmoid says “probably 1” or “mostly 0.” That smoothness is the secret to training: if you nudge a weight a tiny bit, the output changes a tiny bit, so you can measure which direction is better.
The sigmoid squishes any input into the range 0 to 1.
Stacking makes curves
One neuron draws a straight line through the input space. Everything on one side of the line outputs “yes,” the other side outputs “no.” That is fine for simple problems, but most real problems need curved boundaries.
When you stack neurons in layers, the second layer combines the lines drawn by the first layer. Two neurons can form a corner. Three can make a triangle. With enough neurons, a network can approximate any shape. This is why neural networks are called universal approximators.
Core idea: a single neuron draws a line. Hidden layers combine those lines into curves, corners, and complex decision boundaries.
The XOR puzzle
The XOR problem is famous: output 1 when the two inputs are different, and 0 when they are the same. No single straight line can separate the two answers. You need at least one hidden layer.
Below is a tiny 2-2-1 network: two inputs, two hidden neurons, one output. The grid shows how the network classifies every point in the input space. Press Train to run backpropagation and watch the boundary bend.
Training every weight
A neural network might have billions of knobs. How do you know which way to turn each one? The answer is backpropagation.
Backpropagation is the chain rule from calculus applied across the whole network. The loss at the output is propagated backward, one layer at a time, telling every weight how much it contributed to the error. Each weight then takes a tiny step in the direction that reduces the loss.
In the widget above, each press of Train runs backpropagation on the four XOR examples. The output layer asks the hidden layer, “How much of this error is your fault?” The hidden layer asks the inputs the same question. That is how every weight gets updated.
Why this matters: backpropagation makes deep networks possible. Without it, training more than a few weights would be impossibly slow.
Why does a single neuron fail XOR?
Look at the four XOR points: the two “same” points are in one corner, and the two “different” points are in the opposite corners. Can a single straight line put all “same” points on one side and all “different” points on the other?
No. A single neuron draws one straight line, and XOR is not linearly separable. You need a hidden layer to combine two lines into a corner, so the network can separate the two groups.
Three questions
1. What does an activation function like sigmoid do?
2. Why do we stack neurons in layers?
3. What is backpropagation?
Keep learning
A neural network turns inputs into predictions. But those inputs are usually long lists of numbers called vectors. The next tutorial shows how vectors capture meaning and similarity.
Part of the AI Explained tutorial series.