What a multilayer perceptron is
A multilayer perceptron (MLP) is a stack of fully connected layers: every unit takes a weighted sum of all the units of the layer below, adds a bias and passes the result through a fixed non-linear function. With at least one hidden layer between input and output it can learn functions that a single layer of weights cannot, and it is a part of every later tab: the feed-forward half of each transformer block is a two-layer MLP.
The network on this page reads a 28 × 28 MNIST digit as 784 numbers between 0 and 1 and maps it through hidden layers of 64 and 32 units to ten digit scores: 784 → 64 → 32 → 10, 52,650 weights and biases in all. It was trained here on the CPU with plain SGD for 3 epochs (2,811 steps of 64 images, learning rate 0.1, seed 0), starting from PyTorch's default random weights, and its test accuracy rose from 8.9 % to 95.4 %.
The forward pass
Each layer is a matrix product, a bias and a ReLU, , applied to every entry. The last layer has no ReLU; a softmax turns its ten scores, the logits, into probabilities:
is 64 × 784, 32 × 64 and 10 × 32; with the 106 biases they make the 52,650 parameters, of which the first layer holds 50,240. The Forward pass panel draws one column per layer for the selected test digit, with the weights after the selected step (0, 1, 10, 100, 500, 1000 or 2811), and counts the hidden units above zero. Clicking a cell spells out its sum: one row of dotted with the layer below, plus its bias. Without the ReLUs the three matrices would multiply into one, and the network could only separate the digits by flat boundaries; the kinks the ReLUs add let it bend them.
The loss
Training needs one number that says how wrong the network is. For a digit with label it is the cross-entropy : zero when all the probability is on the right class, when it is spread evenly, and large when the network is confidently wrong. Before training the test loss is 2.311, close to that even spread. The gradient of with respect to the logits is simply the probabilities minus the one-hot label,
so the backward pass starts from how much probability went to each wrong class.
Backpropagation
Backpropagation applies the chain rule layer by layer, from the loss back to the input, reusing each layer's result for the next. Through a linear layer the gradient goes back through the transposed matrix; through a ReLU it passes where the unit was active and stops where it was not:
The weight gradient is an outer product: row of is times every pixel, so it looks like the digit itself, scaled and signed by how unit should change. The Backpropagation panel shows these columns for the same digit, from the loss back to the pixels. The backward pass costs about as much as the forward pass, which is what makes it practical to train millions of weights. The page's hand-written backward pass matches PyTorch's autograd to .
One SGD step
Stochastic gradient descent estimates the gradient of the loss over all 60,000 training digits from a small random batch and takes a step against it:
The One SGD step panel shows two recorded steps, the first one and step 1000, with their 64 training digits. For one first-layer unit it draws the 784 weights before the step, their batch gradient, the change and the weights after. At the first step the batch loss only moves from 2.308 to 2.301; at step 1000 the same kind of step takes it from 0.282 to 0.157. Each step is noisy, since 64 digits are a rough sample of the training set, but on average it points downhill, and it is far cheaper than the exact gradient over every image. The charts below the page follow the loss, the accuracy and the size of each layer's gradient through all 2,811 steps.
What the first layer learns
Each row of has one weight per pixel, so it can be drawn as a 28 × 28 picture: red pixels raise the unit's input , blue ones lower it. The What first-layer units respond to panel shows all 64 rows at the selected checkpoint. At step 0 they are random numbers in ; training shapes each into a template whose dot product with a digit is large when the ink falls on its red pixels and avoids its blue ones. For the selected unit the page also shows its mean activation for each digit class over the 10,000 test images and the five test images that drive it hardest. No unit is told what to detect: its weights are whatever made the loss fall.
Where it came from
Rosenblatt's perceptron (1958) was a single layer of adjustable weights trained by an error-correction rule. Minsky and Papert's book Perceptrons (1969) showed what one layer cannot compute, such as XOR, and nobody yet knew how to train the hidden layers that could. Reverse-mode differentiation had been described by Linnainmaa (1970) and proposed for networks by Werbos (1974), but backpropagation took hold after Rumelhart, Hinton and Williams (1986) showed hidden units learning useful internal representations. In 1989 Cybenko, and Hornik, Stinchcombe and White, proved that one hidden layer of squashing units can approximate any continuous function on a bounded region, given enough units; it says nothing about how many, or how much data they need. The MLP's weakness on images, a separate weight for every pixel, is what the CNN tab fixes.
Papers
- Learning representations by back-propagating errors Backpropagation through hidden layers, and the internal representations those layers learn.
- The perceptron: A probabilistic model for information storage and organization in the brain The perceptron: a layer of weighted sums with a threshold, adjusted from its errors.
- A Stochastic Approximation Method Following noisy estimates of a gradient with shrinking steps still converges: the root of SGD.
- Approximation by superpositions of a sigmoidal function One hidden layer of sigmoid units can approximate any continuous function on the unit cube.
- Multilayer feedforward networks are universal approximators The same universal approximation result for any squashing activation.
- Efficient BackProp Practical advice for training MLPs with SGD: minibatches, input scaling, initial weights and learning rates.
- Deep learning A short survey of deep networks, from backpropagation to convolutional and recurrent networks.