How a neural network learns: a playground

The figure below is a complete neural network: two inputs, a handful of hidden units you choose, one output, trained by gradient descent on whatever you point it at, running in your browser. It is the smallest network that shows the things that matter about large ones, and the point of this page is to let you see them happen rather than read about them. Each experiment is a couple of clicks. If the words neural network, hidden unit or training are new, read the beginner’s guide first; if you have done lesson 3, you have already built the version of this with no hidden units.

Loading the interactive figure…
The playground. Pick a dataset, choose how many hidden units, press train. Filled dots are training examples, hollow dots are the held-out test set, and the shading is the network’s current answer for every point in the plane.

What you are looking at

Each dot is an example with two measurements (its position) and a label (its colour). The network's job is to learn a rule that gives the right colour from the position. The big square shows the rule it currently has: for every point on the plane, blue or orange, stronger where it is more sure. The small diagram on the right is the network itself, with each weight drawn as a line whose thickness is its size and whose colour is its sign. The curves below it are the loss, a measure of how wrong the network is, on the training examples (solid) and on the examples it never sees during training (dashed).

Training is the same loop as lesson 3: show the network the training examples, measure how wrong it is, work out for every weight which direction would make it less wrong, and move each weight a little in that direction. The learning rate is how little. One press of "one step" does that once; "train" does it a few times per frame until you stop it.

Five experiments

  1. A straight line is enough. Choose two blobs and set hidden units to none. Press train. This is logistic regression, exactly lesson 3's decoder, and it can only draw a straight boundary. For two blobs a straight line is all you need, and both accuracies go to about 95 percent within a hundred epochs. Now watch the weights in the diagram: two lines, one from each input, and their thickness is the whole model.
  2. Rings need a bend. Choose rings, still with no hidden units, and train. It cannot do it: there is no straight line that separates an inner disc from an outer ring, and the accuracy stays near 50 percent however long you wait. Set hidden units to 1 and reset: a little better, since one unit can bend the boundary once. Set it to 3: the boundary closes into a loop and the accuracy climbs past 95 percent. Each hidden unit draws one straight line of its own; the output combines them, and three lines can enclose a region.
  3. The problem that stalled the field. Choose four corners: blue where both inputs have the same sign, orange where they differ. With no hidden units this is impossible, and in 1969 Minsky and Papert used exactly this example to argue that single-layer networks were a dead end, which helped stop research for a decade. Two hidden units solve it. Watch how: each learns one diagonal, and the output learns to say "one but not both".
  4. Overfitting, live. Choose spiral, set 8 hidden units and a learning rate of about 1.5, and let it train for a while. The training loss keeps falling. The dashed test loss falls, flattens, and then, often, begins to rise, while the shading grows strange tongues that fit single training dots. The network is memorising the examples rather than learning the rule, and the only thing that reveals it is the data you held out. This is why lesson 3 insisted on a test set, and why every serious model is judged on data it has not seen.
  5. Real neurons. Choose real: two neurons. The dots are the 340 trials from lesson 3, positioned by how many spikes neurons 141 and 190 fired, coloured by whether a stimulus was on the screen. With no hidden units the network reaches about 97 percent, which is lesson 3's result for these two cells. Now add hidden units. Nothing improves, because the two classes overlap where they overlap and no boundary, however bendy, can separate what the neurons themselves did not. More model does not fix less information. Lesson 3 got to 99.7 percent not with a cleverer network but with 64 more neurons.

Things to notice along the way

  • The learning rate is a dial between slow and unstable. Set it very low and training crawls. Set it to 3 on the spiral and the loss jumps around instead of falling: each step overshoots. Real training uses schedules that start high and shrink, for this reason.
  • Reset changes the answer. "Reset weights" starts from different random values. On the spiral, train twice from different starts and you will get two different boundaries with similar accuracy. There is no single right network, only a family of acceptable ones, which is one reason the weights of a trained model are hard to interpret.
  • Width buys shape, not understanding. Hidden units are straight lines; more of them means a boundary with more bends. That is the entire source of a network's flexibility, in two dimensions and, with more layers, in billions.
  • Nothing here knows what a ring is. The network has no concept of circles, corners or spirals. It has weights and a gradient. Everything that looks like understanding in the shading is the gradient having found a boundary that lowers the loss on these dots. Keep that in mind when a much larger version of the same machine produces a paragraph.

How it relates to the rest of the site

The network with no hidden units is the logistic regression of lesson 3, and the GLM primer explains why that is one member of a family. The hidden layer is new, and it is what turns a linear decoder into a universal one: a theorem from 1989 says that one hidden layer with enough units can approximate any reasonable boundary, which the rings and spiral experiments demonstrate in miniature. The train/test split is lesson 3's central rule, and the overfitting experiment is the reason for it. The real-data experiment is a reminder that in neuroscience the data, not the model, are usually the limit, which is the recurring lesson of lesson 4 and of the whole Foundations track.

The network is written in about a hundred lines of plain JavaScript inside this site's plugin: a forward pass, a backward pass computed by hand, and a loop. There is nothing in it that lesson 3 did not already contain except the hidden layer and the chain rule. The real dataset is neurons 141 and 190 from the Steinmetz recordings (Nature 2019, CC-BY, via Neuromatch).