Neural Networks
Layers of simple units that learn their own features.
One artificial neuron cannot compute "one or the other, but not both", and the proof takes two sentences. Add a second layer and the problem disappears. Stack many layers, tune every connection from examples, and the result transcribes speech, sorts photographs and translates text. This lesson builds that machine one neuron at a time and asks where its power comes from, and what it costs.
What you’ll learn
- Compute a neuron's output from its weights, bias and activation, and explain why one neuron can only separate classes with a straight line.
- Prove that no single neuron computes XOR, and build a two-layer network that does.
- Describe how a network learns: a loss, gradient descent, and backpropagation's use of the chain rule.
- Explain what width and depth add, why large networks can memorize their training data, and how regularization fights back.
- Trace the history from McCulloch and Pitts to AlexNet, and explain how architectures build in assumptions about the data.
A neuron weighs the evidence
Suppose you want a rule that flags a loan application as risky from two numbers: , the applicant's debt as a fraction of income, and , the number of payments they missed last year. A natural rule weighs each number by how much it matters, adds a constant, and checks the sign:
That is an artificial neuron. The weights say how strongly, and in which direction, each input pushes; the bias sets how much total evidence is needed before the neuron fires. In general a neuron takes inputs, forms the weighted sum (written compactly as ), and passes it through an activation function to produce its output . With the hard threshold "output 1 if , else 0" it is Rosenblatt's perceptron.
Where is the neuron undecided? Exactly on the set , a straight line in the plane (a flat hyperplane when there are more inputs). Every point on one side fires and every point on the other side does not, so a single neuron's decision boundary is always straight. The weights tilt the line; the bias slides it.
A neuron has weights and bias , and it fires when . Which logic gate does it compute on inputs from ?
- AND
- OR
- NAND
- XOR
Show the answer
C. NAND
at and at and , so it fires on all three; at , and it stays silent. Output 0 only when both inputs are 1: that is NAND. Every logic circuit can be built from NAND gates alone, so networks of such neurons can compute any Boolean function, which was McCulloch and Pitts's point.
XOR: where one line fails
Now take XOR, short for "exclusive or": the output is 1 if one input is on but not both. On the unit square its 1s are the corners and , and its 0s are and , the two ends of the other diagonal.
No line separates them, and the proof takes two sentences. The points on one side of a line form a convex set: the midpoint of two points on that side lies on that side too. A line with and on its firing side would put their midpoint there as well, but is also the midpoint of and , which must both be on the silent side. Contradiction: XOR is not linearly separable.
The way out is to use more than one neuron. XOR means "at least one input is on, but not both": the AND of an OR and a NAND. Give a hidden layer two neurons, computing OR and computing NAND, and let an output neuron compute the AND of and :
| (OR) | (NAND) | output: AND of | ||
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 0 | 0 |
Think first Plot the four inputs at their new coordinates . Why can a single line now finish the job?
lands at and at , while both and land on the same point . The two positive inputs have merged, and the line cuts off from the other two. The hidden layer did not solve the problem by itself: it re-described the inputs with new features in which a straight line suffices. That is what hidden layers do in every network.
Let the network learn its features
Hand-designing hidden neurons does not scale to an image with a million pixels. The fix is to make every weight learnable, those of the hidden layers included. That takes one change. A hard threshold jumps from 0 to 1, so a small change in a weight either does nothing or flips the output, and there is no slope to follow. Learning needs outputs that respond smoothly, so networks use smooth activations such as the sigmoid , , or the ReLU , which is smooth everywhere except at one point.
Layers of such neurons, each feeding the next, form a multilayer perceptron (MLP), the basic neural network. Layers that sit between the input and the output are called hidden layers. Their number is the network's depth (some authors also count the output layer), and the number of neurons in a layer is its width.
Why did the network with the linear activation fail, even though it has a hidden layer of four neurons?
- Four neurons are too few; with forty it would succeed
- A linear function of a linear function is linear, so the whole network computes and its boundary is one straight line
- The learning rate was too small for a linear network
- Linear neurons cannot have negative weights
Show the answer
B. A linear function of a linear function is linear, so the whole network computes and its boundary is one straight line
Each layer computes , and composing two such maps gives , another map of the same kind. However many layers you stack, without a nonlinearity between them the network is one linear model, and XOR defeats every linear model.
Learning: downhill on the loss
How does the network find good weights? Training picks a loss, a single number scoring how badly the network does on the training set, and pushes it down step by step. For classification the usual loss is the cross-entropy: if the network gives the correct class probability , it pays , which is near 0 when is near 1 and grows without bound as .
To lower the loss, move every weight a little against its slope, the partial derivative :
This is gradient descent, and is the learning rate. In practice each step estimates the gradient from a mini-batch of randomly chosen examples instead of the whole training set, which is stochastic gradient descent.
The hard part is getting the derivatives. A modern network has millions or billions of weights, and the loss depends on each one through every layer above it. Backpropagation computes all of them with the chain rule. A forward pass computes and stores every neuron's value. A backward pass then starts at the loss and walks back through the network; at each node it multiplies the node's own local derivative by the gradient already computed above it, so the work done for one layer is reused by every layer below it.
A tiny "network" computes and then the loss . At , what is ? Do it the backpropagation way: first , then multiply by the local derivative .
Show the answer
18
At , , so . The local derivative of is 3, and the chain rule multiplies them: . Directly: has derivative , which is 18 at . Backpropagation does exactly this, node by node, for every weight at once.
Wider, deeper, and the danger of memorizing
Width and depth add capacity in different ways. A wider layer draws more lines at once. A deeper network composes: the second layer builds corners and wedges out of the first layer's lines, the third builds shapes out of those, and so on. Some functions need exponentially many neurons in a single hidden layer but only a modest number when they are spread over several layers; the Descent and Convergence lessons make that precise.
A network for 2-D inputs has two hidden layers of 8 neurons and one output neuron; every neuron has one weight per input and one bias. How many parameters does it have? (You can check in the widget.)
Show the answer
105
Layer by layer: , then , then , for 105 in total. A layer with inputs and neurons has parameters, so counts grow with the product of neighboring widths: two connected layers of 1,000 neurons already hold a million weights.
More parameters cut both ways. A large network can fit almost any training set, even one whose labels were assigned at random, so a perfect training score proves little. What counts is generalization: performance on data held out from training. When a network starts fitting noise instead of the pattern, training error keeps falling while validation error rises: overfitting. The standard defenses are forms of regularization:
- Weight decay pulls every weight a little toward zero at each step, favoring smaller, smoother functions.
- Dropout switches off a random subset of neurons on every training step, so no neuron can lean on a few others.
- Data augmentation trains on flipped, cropped or slightly distorted copies of the examples, teaching invariances the task is known to have.
- Early stopping halts training when the loss on a validation set stops improving.
During training, the training loss keeps falling, while the validation loss reached its minimum at epoch 20 and has risen ever since. What is the best reading?
- The network is underfitting; make it larger
- The learning rate is too small
- The network has started to overfit; keep the weights from around epoch 20, or regularize more
- The validation set is broken: validation loss cannot rise while training loss falls
Show the answer
C. The network has started to overfit; keep the weights from around epoch 20, or regularize more
Training loss measures the fit to the training examples; validation loss estimates performance on new ones. When they move in opposite directions, the network is learning details of its training set that do not carry over. Early stopping keeps the weights from the validation minimum; weight decay, dropout, augmentation or more data postpone the turn.
Why networks took over
The core of this lesson, hidden layers trained by backpropagation, was in place by the late 1980s, and convolutional networks followed soon after; dropout and the widespread use of ReLU came only in the early 2010s. For about two decades neural networks were one method among many, and on many benchmarks kernel methods such as support vector machines did as well or better. Then, in 2012, a deep convolutional network trained on two graphics cards with 1.2 million labeled ImageNet photographs (Krizhevsky, Sutskever & Hinton) won the ImageNet classification challenge, cutting the top-5 error to 15.3% when the runner-up had 26.2%. Within a few years networks led in image recognition, speech recognition and machine translation.
Three things came together: large labeled datasets, fast parallel hardware, and architectures that build in assumptions about the data. A convolutional network assumes that nearby pixels belong together and that a pattern means the same thing wherever it appears, so it slides one small filter over the whole image instead of learning separate weights for every location. A transformer treats text as a set of tokens that exchange information through attention, with each token's position supplied explicitly. Such assumptions are an inductive bias: they make the right functions easy to find from limited data. Where data have no such structure, as in many tables of mixed numeric and categorical columns, gradient-boosted trees still often win (Trees vs Deep Learning on Tabular Data).
| Year | Milestone |
|---|---|
| 1943 | McCulloch & Pitts: networks of threshold neurons compute logical functions |
| 1958 | Rosenblatt's perceptron learns its weights from examples |
| 1969 | Minsky & Papert's Perceptrons maps the limits of a single layer |
| 1986 | Rumelhart, Hinton & Williams popularize backpropagation through hidden layers |
| 2012 | Krizhevsky, Sutskever & Hinton's deep CNN (AlexNet) wins ImageNet, and deep learning takes off |
| Descent lesson | What you will do there |
|---|---|
| From Perceptrons to Deep Networks | Write the forward pass in matrix form, compare activation functions, pair output layers with losses, count parameters, and see what one hidden layer can represent. |
| Backpropagation | Derive the backward pass layer by layer, count its cost, check gradients numerically, and see why gradients vanish or explode in deep networks. |
| Training Networks Well | Assemble the practical recipe: initialization, optimizers and learning-rate schedules, normalization layers, regularization, and how to debug a run that will not learn. |
| Representations, Transfer & Inductive Bias | Reuse what networks learn through embeddings, transfer learning and self-supervised pretraining, and read architectures as built-in assumptions. |
Key takeaways
- A neuron computes ; alone, it separates classes only along a straight line (a hyperplane in higher dimensions).
- XOR is not linearly separable, so no single neuron computes it; a hidden layer that maps the inputs to new features fixes that.
- Without nonlinear activations any stack of layers is one linear model. With them, one wide hidden layer can approximate any continuous function on a closed, bounded region, possibly with enormously many neurons.
- Training takes downhill steps on a loss; backpropagation supplies the slope for every weight at once by running the chain rule backward through the network.
- Width and depth add capacity, and large networks can memorize their training data: judge them on held-out data, and regularize with weight decay, dropout, augmentation or early stopping.
- Networks took over perception and language once big datasets, fast hardware and architectures with the right built-in assumptions came together (AlexNet, 2012).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A neuron has weights , , bias , and fires when . Which of the four inputs in make it fire?
- Only
- and
- Every input except
- None: the bias is negative
Show the answer
A. Only
is at , at and at , and at . Only clears zero, so the neuron computes " and not ".
In the hand-built XOR network (OR and NAND hidden neurons, AND output), what exactly did the hidden layer contribute?
- It memorized the four training points
- It made the output neuron's boundary curved
- It mapped the inputs to new features in which the two classes are linearly separable
- It replaced the need for an output neuron
Show the answer
C. It mapped the inputs to new features in which the two classes are linearly separable
OR and NAND send both XOR-positive inputs to and the negatives to and , where one line separates them. The output neuron is still a single linear threshold unit; the hidden layer changed the representation it sees.
Rosenblatt's perceptron uses a hard threshold. Why do networks trained by gradient descent use smooth activations such as the sigmoid, tanh or ReLU instead?
- Threshold units cannot compute logic functions such as NAND
- Smooth activations make the whole network linear, which is easier to train
- Threshold units need far more memory
- A threshold is flat on both sides of its jump, so a small change in a weight gives gradient descent no slope to follow
Show the answer
D. A threshold is flat on both sides of its jump, so a small change in a weight gives gradient descent no slope to follow
Gradient descent follows derivatives. With a step function, nudging a weight either leaves the loss unchanged or makes it jump, so the derivative carries no information about which way to move. Smooth activations (ReLU is smooth except at one point) make the loss respond gradually, so every weight gets a useful slope.
Why do large networks get their gradients from backpropagation rather than by nudging each weight in turn and re-measuring the loss?
- Backpropagation finds the global minimum of the loss
- One backward pass gives the derivative for every weight at the cost of a few forward passes, while nudging needs about one forward pass per weight
- Nudging weights one at a time gives the wrong derivatives
- Backpropagation does not need a loss function
Show the answer
B. One backward pass gives the derivative for every weight at the cost of a few forward passes, while nudging needs about one forward pass per weight
With a million weights, nudging them one at a time costs about a million forward passes for a single gradient. The chain rule lets the backward pass reuse each layer's gradient for all the weights below it, so the whole gradient costs about as much as a few forward passes.
Why does a convolutional network usually need far fewer training images than a fully connected network of similar size to reach the same accuracy on photographs?
- Its architecture builds in locality and the same-filter-everywhere assumption, so it does not have to learn them from data
- It trains with a larger learning rate
- It has more parameters
- Convolution makes overfitting impossible
Show the answer
A. Its architecture builds in locality and the same-filter-everywhere assumption, so it does not have to learn them from data
A fully connected network must discover from examples that an edge in one corner is the same feature as an edge in another. A convolutional network has that knowledge built in, because one small filter is reused everywhere. This inductive bias shrinks the set of functions it has to search, which saves data, as long as the assumption fits the task.
Go deeper
- From Perceptrons to Deep Networks · Neurons, layers, activations and what networks can represent.
- Backpropagation · The chain rule, organized: every gradient in one backward sweep.
- Training Networks Well · Normalization, regularization and the recipes that make deep learning work.
- Representations, Transfer & Inductive Bias · What networks learn, and how to reuse it.
Further reading
- I. Goodfellow, Y. Bengio & A. Courville, Deep Learning (MIT Press, 2016), chapters 6–8.
- Y. LeCun, Y. Bengio & G. Hinton, "Deep learning," Nature 521, 436–444 (2015).