Understanding AI · lesson 30 of 60 · about 7 minutes
Layers of Neurons
Stack neurons in layers, and the impossible becomes easy. Follow numbers through a tiny two-layer network.
What you'll learn
- Describe a network as layers of neurons
- Pass inputs through a hidden layer to an output
- Explain how layers solve XOR
Neurons feeding neurons
In a neural network, neurons are arranged in layers. The first layer gets the inputs. Its answers become the inputs for the next layer, and so on, until the last layer gives the final answer.
Layers in the middle are called hidden layers, because you never see their answers directly.
Solving XOR
Here's a trick: one hidden neuron checks or (either on), another checks and (both on). The output neuron says yes when or is true but and isn't. That's exactly one input on: XOR. Two simple neurons, combined, do what one couldn't.
A neuron as a function, then a two-layer network solving XOR. Weights were chosen to make each neuron work.
def neuron(inputs, weights, bias):
total = bias
for x, w in zip(inputs, weights):
total = total + x * w
return 1 if total > 0 else 0
for x1 in [0, 1]:
for x2 in [0, 1]:
either = neuron([x1, x2], [1, 1], 0)
both = neuron([x1, x2], [1, 1], -1)
answer = neuron([either, both], [1, -1], 0)
print(x1, x2, answer)Output
0 0 0
0 1 1
1 0 1
1 1 0Tip
Hidden layers let simple neurons team up on harder patterns.
Now try it yourself
This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.