WEEK 9
CHAPTER 9

Perceptrons & Neural Networks

One artificial neuron draws one straight line. Stack them, and they can draw almost anything.

Manga page: Konaev (Kapchagay). / How does a machine learn to SEE faces and READ words? / With neural networks. It starts with one tiny neuron: the perceptron. / Weights... sum... fire!

From brain cells to a formula

A biological neuron receives signals through dendrites, sums them, and "fires" along its axon if the total is strong enough. McCulloch & Pitts (1943) turned this into a logic unit. An artificial neuron computes a weighted sum of its inputs plus a bias, then applies an activation function:

z = w₁x₁ + w₂x₂ + … + wₙxₙ + b output ŷ = g(z)
x₁x₂x₃Σg(z)ŷ w₁w₂w₃+ b

The weights say how important each input is; the bias shifts the firing threshold. Learning means finding good weights.

Step1 if z > 0, else 0 (the original perceptron)
Sigmoid1/(1+e^(−z)), between 0 and 1 (Chapter 8)
tanhbetween −1 and 1, centred at 0
ReLUmax(0, z), the default in deep nets

The perceptron learns a line

With two inputs, the perceptron outputs 1 on one side of the line w₁x₁ + w₂x₂ + b = 0 and 0 on the other side. It is a linear classifier. Rosenblatt's learning rule nudges the weights after every mistake:

wᵢ ← wᵢ + η · (y − ŷ) · xᵢ b ← b + η · (y − ŷ) y = true label, ŷ = prediction, η = learning rate correct prediction: (y − ŷ) = 0, so nothing changes

Worked example: learning AND (η = 1, start w = (0, 0), b = 0)

EpochMistakesWeights after epoch (w₁, w₂, b)
11 (on input 1,1)(1, 1, 1)
23(2, 1, 0)
33(2, 1, −1)
42(2, 2, −1)
51(2, 1, −2)
60 ✓(2, 1, −2): 2x₁ + x₂ − 2 > 0 only for (1, 1)

Epoch 1 in detail: inputs (0,0), (0,1), (1,0) give z = 0, so ŷ = 0, which is correct. Input (1,1) gives ŷ = 0 but y = 1, error +1, so w ← (0+1, 0+1) and b ← 0+1. The perceptron convergence theorem guarantees: if a separating line exists, the rule finds one in a finite number of steps. You can reproduce this exact table in the demo below.

TRY IT! Perceptron sandbox: AND and OR work, XOR doesn't


Fix XOR with a hidden layer: train a small 2-3-1 sigmoid network with backpropagation.

The background colour will show the network's output: red ≈ 1, white ≈ 0.

The XOR problem, and the first neural winter

XOR outputs 1 when exactly one input is 1: (0,1) and (1,0) are positive, (0,0) and (1,1) are negative. No single straight line separates them, so a single perceptron can never learn XOR. Minsky & Papert proved such limits in Perceptrons (1969), and enthusiasm and funding for neural networks dropped for over a decade.

The fix: hidden layers

Combine simple lines: XOR = (x₁ OR x₂) AND NOT(x₁ AND x₂). Hand-set step-neuron weights:

h₁ = step(x₁ + x₂ − 0.5) → OR h₂ = step(−x₁ − x₂ + 1.5) → NAND y = step(h₁ + h₂ − 1.5) → AND of the two
x₁, x₂h₁ (OR)h₂ (NAND)y = XOR
0, 0010
0, 1111
1, 0111
1, 1100

A multilayer perceptron (MLP) stacks layers: input → one or more hidden layers → output. With nonlinear activations, a network with enough hidden units can approximate any continuous function to any accuracy (the universal approximation theorem). The hard part is learning the weights.

How networks learn: loss, gradients, backpropagation

  1. Forward pass: compute the prediction ŷ layer by layer.
  2. Loss: measure the error, e.g. MSE = (y − ŷ)² or cross-entropy for classification.
  3. Backward pass (backpropagation): use the chain rule to compute ∂Loss/∂w for every weight, from the output back to the input.
  4. Gradient descent: w ← w − η · ∂Loss/∂w, a small step downhill (the same idea as Chapter 3's continuous local search).
  5. Repeat over many epochs (passes over the data), usually on mini-batches.

Backpropagation was popularised by Rumelhart, Hinton & Williams (1986). The step function has zero gradient almost everywhere, which is why trained networks use activations with useful derivatives, like sigmoid or ReLU (ReLU is differentiable everywhere except at 0, where a convention is used).

IN PLAIN WORDS

The network guesses, measures how wrong it was, figures out which weights were most to blame, and nudges each one a little in the direction that reduces the error. It does this millions of times.

Deep learning: many layers, learned features

Deep learning uses networks with many hidden layers. Early layers learn simple features (edges), later ones learn combinations (eyes, wheels, words). Nobody hand-designs the features. The big 2012 breakthrough (AlexNet) came from three things together: large datasets, GPUs and better training tricks.

ArchitectureKey ideaTypical use
MLP (fully connected)Every neuron connects to every neuron in the next layerTabular data, small problems
CNN (convolutional)Small filters slide across an image and share weightsImages, defect detection, medical scans
RNN / LSTMA hidden state carries memory along a sequenceOlder speech and text models, time series
TransformerAttention: each token weighs the other tokens (in LLMs, only the earlier ones)LLMs, translation, also vision (Chapter 10)

Neural networks also power deep reinforcement learning (DQN from Chapter 5): the network replaces the giant Q-table.

Training pitfalls

  • Overfitting: millions of weights can memorise the training set. Remedies: more data, validation-based early stopping, dropout, weight decay (regularisation).
  • Learning rate: too high and the loss explodes; too low and training takes forever.
  • Local minima and plateaus: like hill climbing, gradient descent can stall. Random initialisation and momentum-based optimisers (e.g. Adam) help.
  • Black box: it's hard to explain why a deep net decided something. That matters for high-risk uses (Chapter 10).
REAL WORLD

Face unlock on phones (CNNs), voice assistants (speech networks), machine translation and chatbots (Transformers), quality inspection on factory lines, crop monitoring from satellite images, and AlphaFold's protein-structure prediction (2020) are all deep neural networks.

DID YOU KNOW?

Rosenblatt's Mark I Perceptron (1958) was a physical machine: a 20×20 grid of light sensors wired to motor-driven potentiometers that physically turned to change the weights. The press predicted it would soon "walk, talk, see, write". It took about 60 more years and a few billion times more computing power.

MYTH vs FACT

Myth: "Artificial neural networks work just like the brain." Fact: they are loosely inspired by neurons, but real neurons use spikes, chemistry and feedback in ways ANNs don't model. Backpropagation is not how the brain is known to learn.

MINI-QUIZ

1. Why can't a single perceptron learn XOR?

A perceptron draws only one line. The positives (0,1) and (1,0) sit on one diagonal and the negatives on the other, so no single line splits them.

2. With w = (2, 1), b = −2 and a step activation, what is the output for input (1, 0)?

z = 2·1 + 1·0 − 2 = 0, and the step fires only for z > 0, so the output is 0. That's why this perceptron computes AND.

3. What does backpropagation compute?

It applies the chain rule backwards through the layers. Gradient descent then uses those gradients to update the weights.
Manga page: ALMATY! / I see the mountains!
9