ai/courses

AI Fundamentals/Lesson 2 of 8

Neural Networks Explained: How They Learn, No Math

Neurons, layers and backpropagation, and why one neuron can’t learn XOR.

Lesson
2 of 8
Time
30 min
Updated

TL;DR

The short answer

A neural network is a stack of simple units called neurons. Each one multiplies its inputs by weights, adds them up and passes the result through a small bend called an activation function. One neuron can only split data with a straight line. Layers of neurons feeding into each other can learn almost any pattern, from handwritten digits to language.

Networks learn with the loop from lesson 1: guess, measure the loss, nudge the weights. The part that makes it work at scale is backpropagation, an algorithm that works out how much each of millions of weights contributed to the error, all in one backward pass.

One neuron: a tiny voting machine

Forget biology for a moment. An artificial neuron is a very small calculator that makes one decision.

Say it decides whether you should go to the beach. It gets three inputs, each a number: is it sunny (1 or 0), how warm is it, is it the weekend. Each input has a weight, a number that says how much that input matters. Sun might matter a lot (weight 3), the weekend a little (weight 1). The neuron multiplies each input by its weight, adds everything up, and adds one more number called the bias, which sets how easy the neuron is to convince. If the total is big enough, the neuron "fires": go to the beach.

That's the entire unit. Inputs times weights, plus a bias, then a decision. The weights and the bias are the knobs training will turn.

Fig. 1An artificial neuron: each input is multiplied by its weight, everything is summed with a bias, and an activation function turns the sum into the output.

The straight-line problem

Here is the limit of a single neuron, in a form you can test. Take two inputs that are each 0 or 1, so there are four possible cases. Put them on the corners of a square. A neuron's decision ("fire" or "don't") always splits that square with one straight line: everything on one side fires, everything on the other side doesn't. Changing the weights only moves and tilts the line.

AND (fire only if both inputs are 1) is easy: put the line near the top-right corner. OR (fire if at least one is 1) is easy too. Now try XOR, "exclusive or": fire if exactly one input is 1. The two "yes" corners sit on one diagonal and the two "no" corners on the other.

DemoMove the weights and the bias to solve AND and OR. Then try XOR: no single line can do it. Add a hidden layer and see how two neurons plus a third one solve it.

No straight line can separate the diagonals. That's not a failure of effort: it's geometry. In 1969 Marvin Minsky and Seymour Papert proved this kind of limit formally in their book Perceptrons, and it helped push neural network research into a long freeze.

The fix is visible in the demo. Use two neurons in a middle layer: one that learns OR, one that learns "not both" (NAND). Feed both into a third neuron that fires only when both of them fire (AND). Each neuron still draws a single straight line, but the third one combines the first two decisions into a shape no single line could make. That's the whole idea of a neural network: simple decisions, combined in layers, make complicated ones.

Layers: neurons feeding neurons

A neural network is many neurons arranged in layers. The input layer is just the data: for a 28 by 28 pixel image of a handwritten digit, that's 784 numbers, one per pixel's brightness. Then come one or more hidden layers, where every neuron takes all the outputs of the previous layer as its inputs. Finally the output layer gives the answer: for digits, ten neurons, one per digit from 0 to 9, each giving a score.

Count the knobs in a small digit-reading network with one hidden layer of 128 neurons. Each hidden neuron has 784 weights and a bias: 128 × 785 = 100,480 parameters. Each output neuron has 128 weights and a bias: 10 × 129 = 1,290 more. That's 101,770 numbers to set, for a network that is tiny by today's standards. Nobody could set them by hand. Training does.

Fig. 2A deep network learns a hierarchy on its own: early neurons respond to edges, later ones to shapes, the last ones to whole objects. Nobody programs these features; they come out of training.

The fascinating part is what the hidden layers end up doing. When researchers looked inside trained image networks, they found that neurons in the first layers respond to simple things like edges and color blobs, middle layers to textures and shapes like circles and stripes, and deep layers to whole concepts: faces, wheels, dog snouts. Nobody told the network to look for edges. Edges turned out to be useful for lowering the loss, so the weights drifted there.

This is why the field is called deep learning: deep networks, with many layers, learn their own features in layers of increasing abstraction. Before 2012, people spent careers hand-designing those features. After 2012, networks learned better ones from data.

The bend that makes it work: activation functions

One detail is easy to miss and absolutely essential. If each neuron only multiplied and added, then stacking layers would be pointless: multiplying and adding, then multiplying and adding again, is still just multiplying and adding. A hundred such layers collapse mathematically into one. You'd be back to a single straight line.

So after adding up its inputs, every neuron passes the sum through a small nonlinear function, a bend, called the activation function. The most popular one today is almost comically simple. It's called ReLU: if the sum is negative, output zero; otherwise, output the sum unchanged. That one kink is enough. With it, stacked layers can bend and fold the data into any shape the task needs.

Backpropagation: passing the blame backwards

Training a network uses the loop from lesson 1. Show it an example (a picture of a 7), let it guess (maybe it says "1"), measure the loss, and nudge every weight downhill. The hard question is: which way is downhill for each of the 101,770 weights? Turning one weight changes one neuron, which changes every neuron after it, which changes the output. How do you know who to blame?

Backpropagation answers it efficiently. It starts at the output, where the error is known, and works backwards one layer at a time. For each neuron it asks: how much did your output contribute to the error of the layer after you? And for each weight: how much did you contribute to your neuron's output? Calculus (the chain rule, if you remember it from school) lets each layer compute its share of the blame from the layer above. One forward pass to guess, one backward pass to assign blame to every weight, then one small step for all of them at once.

Backpropagation was discovered more than once, but it became famous in 1986 when David Rumelhart, Geoffrey Hinton and Ronald Williams showed in Nature that it trained networks with hidden layers to learn useful internal representations. It's still the engine under every network trained today, including the largest language models. Modern tools compute it automatically: you write the forward pass, and libraries like PyTorch work out every gradient for you.

xor.py
import numpy as np

rng = np.random.default_rng(1)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])                 # XOR

W1 = rng.normal(size=(2, 4)); b1 = np.zeros(4)    # hidden layer: 4 neurons
W2 = rng.normal(size=(4, 1)); b2 = np.zeros(1)    # output neuron
sigmoid = lambda z: 1 / (1 + np.exp(-z))

for step in range(10_000):
    # Forward pass: make a guess
    h = np.tanh(X @ W1 + b1)
    out = sigmoid(h @ W2 + b2)
    # Backward pass: send the error back, layer by layer
    d_out = out - y                       # error at the output
    d_h = (d_out @ W2.T) * (1 - h ** 2)   # each hidden neuron's share of the blame
    # Nudge every weight a little downhill
    W2 -= 0.1 * h.T @ d_out
    b2 -= 0.1 * d_out.sum(axis=0)
    W1 -= 0.1 * X.T @ d_h
    b1 -= 0.1 * d_h.sum(axis=0)

print(out.round(2).ravel())   # close to [0, 1, 1, 0]
CodeA two-layer network learns XOR with backpropagation, in plain numpy. It prints [0. 1. 1. 0.]. Change the random seed to 0 and it gets stuck at [0. 0.5 1. 0.5]: gradient descent can settle in a bad valley, which is why big models are trained with many tricks.

Why deep learning took off in 2012

If the ideas existed in 1986, why did neural networks only take over around 2012? Because they're hungry. They need lots of examples to learn from, and lots of computation to learn quickly. Both arrived at the same time.

The data came from the internet. The ImageNet project collected more than 14 million labeled photos. The computation came from video games. Graphics cards (GPUs) are designed to do millions of small multiply-and-add operations in parallel, which is exactly what a layer of neurons is. In 2012 a network called AlexNet, trained on two gaming graphics cards, crushed the ImageNet image recognition contest, and within a few years deep networks ran speech recognition, translation and photo search.

Different problems got different network shapes, called architectures:

ArchitectureGood atKey idea
Fully connected (MLP)Tables of numbers, small problemsEvery neuron sees every input from the layer before
Convolutional (CNN)Images and videoSmall filters slide across the image, reusing the same weights everywhere
Recurrent (RNN, LSTM)Sequences such as text and audio, before 2017Reads one step at a time and keeps a running memory
TransformerText, code, images, audio: almost everything nowEvery element looks at every other element through attention
TableNetwork architectures you will hear about. Transformers, the subject of lesson 5, now dominate.

You now know what a neural network is: layers of weighted sums with bends in between, trained by gradient descent with backpropagation. Every model in the rest of this course, including ChatGPT, is this. The next question is how text gets into one, since a network only understands numbers.

Exercise · about 5 minutes

Design a neuron by hand

Build an "umbrella neuron" on paper. Inputs: clouds (0 to 1), rain forecast (0 to 1, the probability), and I'm walking, not driving (0 or 1).

  1. Choose a weight for each input. Which should matter most?
  2. Choose a bias, so that the neuron only fires when the total is convincing. A negative bias makes it harder to fire.
  3. Compute the output for three days: a clear morning when you drive (0.1, 0.0, 0), a grey day with a 70% forecast when you walk (0.8, 0.7, 1), and a dry but cloudy day when you walk (0.9, 0.1, 1).
  4. Does it make the decisions you would? Adjust one number and check again.

Congratulations: you just trained a neuron with the most expensive optimizer in the world, a human brain.

Quiz

  1. 1.What does a single artificial neuron compute?

  2. 2.Why can’t one neuron learn XOR?

  3. 3.What happens if you remove the activation functions from a deep network?

  4. 4.What does backpropagation do?

  5. 5.Which two things made deep learning take off around 2012?

0 of 5 answered

Key takeaways

  • A neuron multiplies inputs by weights, adds a bias, and passes the sum through an activation function.

  • One neuron can only draw a straight line through data. That is why it can’t learn XOR.

  • Layers of neurons combine simple decisions into complex ones, and learn their own features: edges, shapes, objects.

  • The activation function is the bend that keeps stacked layers from collapsing into one.

  • Backpropagation works out every weight’s share of the error in one backward pass; gradient descent then adjusts them all.

Go deeper

AI Fundamentals: How Modern AI Actually Works

  1. 01How machines learn25 min
  2. 02Neural networks30 min
  3. 03Tokens20 min
  4. 04Embeddings25 min
  5. 05Attention and transformers30 min
  6. 06How LLMs are trained30 min
  7. 07How LLMs generate text25 min
  8. 08RAG, tools and agents30 min