The idea: a memory you fall into

A Hopfield network (John Hopfield, 1982) is a set of neurons that are all joined to each other. Each neuron is either +1 or −1. Here there are 9 of them, drawn as a 3 × 3 picture: dark is +1, white is −1. There are no inputs and no outputs. The state of the neurons is the thing being remembered. You store a few pictures by setting the weights between the neurons. Later you start the network from a damaged picture, the cue, and let the neurons change one at a time. The state slides into the stored picture that is closest. This is associative memory: you get the whole picture back from a part of it.

Left: the 3 by 3 pictures T and X added into a 9 by 9 weight matrix W with the rule w i j = sum over patterns of x i times x j, symmetric, zero diagonal. Right: a cue that is T with neurons 3 and 8 wrong, updated one neuron at a time by s i becomes the sign of the sum of w i j times s j, while the energy goes from −4 to −20 to −32, ending at T, marked with a green check
Storing is one sum over the patterns; recall updates one neuron at a time until the cue has become the stored T.

Storing: the Hebbian rule

Every pair of neurons i and j shares one weight wij, the same in both directions (wij = wji). A neuron has no weight to itself (wii = 0). To store patterns x (each a list of nine ±1 values), add up, over all patterns:

w_ij = Σ over patterns of x_i · x_j      (i ≠ j)

This is the Hebbian rule, "neurons that fire together wire together". In one pattern, two pixels that are the same add +1 to their weight, two pixels that differ add −1. There is no training loop and no error: storing is one sum. With T and X stored, pixels 1 and 3 are dark in both, so w13 = +2. Pixels 1 and 4 differ in both, so w14 = −2. Pixels 1 and 2 agree in T but differ in X, so w12 = 0. Most books also divide by the number of neurons N; that scales every field and the energy but never changes a sign, so the page leaves it out. (Demo 1: store T and X)

Recall: one neuron at a time

Recall visits one neuron at a time (asynchronous update). Neuron i adds up the other states, each times its weight: the field hi = Σj wij·sj. Then it takes the sign of its field: si becomes +1 if hi > 0 and −1 if hi < 0 (with hi = 0 it keeps its state). One pass over all 9 neurons is a sweep. When a whole sweep changes nothing, every neuron agrees with its field and the state is stable.

The page starts from T with neurons 3 and 8 flipped. Neurons 1 and 2 already agree with their fields. Neuron 3 has h3 = +8 but is −1, so it flips to +1. Later neuron 8 (h8 = +6) flips too. The second sweep changes nothing: the state is T again after 18 visits. Even 3 wrong pixels (cue T, flips 3) still come back to T. (Demo 2: one neuron at a time, Demo 3: recall T from a noisy cue)

Energy only goes down

Hopfield gave each state a number, the energy:

E = −½ · Σ over i ≠ j of w_ij · s_i · s_j

A pair that the weights "like" (positive weight and equal states, or negative weight and different states) lowers E. When neuron i flips, only its own terms change, and E changes by exactly −2·|hi|. A neuron flips only when its state disagrees with the sign of hi, so every flip makes E smaller and nothing ever makes it bigger. E cannot fall forever (there are only 29 = 512 states), so the run must stop, in a local minimum of E. In Demo 3 the energy goes −4 → −20 (neuron 3, 2·8 = 16) → −32 (neuron 8, 2·6 = 12), the lowest value any state has with T and X stored. The weights need to be symmetric for this to work. With wij ≠ wji the network can cycle forever.

Two energy landscapes drawn as curves with valleys. Top, with T and X stored: valleys for minus T, T and X, all at energy −32; a cue at energy −20 on the wall of the T valley rolls down into T, marked with a green check. Bottom, with T, X and O stored: valleys for O, T and X at −28 and a red spurious valley at −24 right next to T; the same cue rolls into the spurious valley, marked with a red cross
Stored pictures are valleys of the energy. A third pattern adds a spurious valley next to T, and the same cue now stops there (a picture, not to scale).

The reversed picture is stored too

E stays the same when every sign flips (each term has two s values), and so do all the fields, up to sign. So if T is a minimum, the reversed picture −T (dark and white swapped) is a minimum with the same energy. Storing T also stores −T for free. A cue that looks more like −T falls into −T. (Demo 4: the reversed picture is stored too: −T with two flipped pixels goes from E = −4 to −32.)

Spurious states and capacity

The weights are a sum of the patterns, so the patterns get in each other's way. With more patterns, new minima appear that were never stored: spurious states. The most common are mixtures of three patterns, such as sign(T + X − O), which takes at each pixel the value most of the three agree on.

storedstable stateswhat happens
T and X4: T, X, −T, −X (all at E = −32)every cue ends in a stored picture or its reverse
T, X and O14: the 6 stored and reversed (E = −28) and 8 spurious (E = −24 or −16)T with neuron 8 flipped ends in the spurious state sign(T + X − O), E = −24, not in T (Demo 5)
T, X, O and U6: O is no longer one of themO itself is not stable: neuron 2 has h = −2, flips, and the state becomes U (E −48 → −52) (Demo 7)

U is very close to O (they differ in one pixel), so the two valleys merge into one. For random patterns, the classic result (Amit, Gutfreund and Sompolinsky, 1985) is that recall works only while the number of patterns is below about 0.14·N. With N = 9 that is about one pattern, so even 2 nearly different pictures are a lot for this tiny network.

The update order matters

Asynchronous recall needs an order. The page offers 1 → 9 in every sweep, or a new shuffled order each sweep from a fixed seed. The energy falls in both, but the run can take a different path and end in a different valley. With T, X and O stored, O with neurons 1 and 9 flipped (E = −12) comes back to O in order 1 → 9, but the shuffled order first visits neuron 3, flips it, and ends in the spurious state −sign(T + X − O) at E = −24. (Demo 6: the update order changes the end) Updating all neurons at once (synchronous) is not shown: it can bounce between two states forever.

Each neuron is the step-output neuron of the Perceptron, but it is never trained on errors and has no separate inputs. A feed-forward Neural Network maps an input to an output in one pass; a Hopfield network runs on its own state until it stops. Getting stuck in a spurious valley is the same problem that Simulated Annealing solves by sometimes accepting an uphill move; a Boltzmann machine is a Hopfield network with exactly that kind of random update.

See also Self-Organizing Map, another network trained with no labels, where a grid of neurons unfolds over the data.

What the page leaves out

Only 9 neurons and integer weights without the 1/N factor; no thresholds (bias) on the neurons. Synchronous updates, the storage rules that hold more patterns (the pseudo-inverse rule, Storkey's rule), continuous-valued Hopfield networks used for optimization (Hopfield and Tank), Boltzmann machines, and the modern Hopfield networks with exponential capacity whose update rule is the attention of transformers are not shown.

On the mind map

Open the Machine Learning mind mapMachine LearningLearning setup: what feedback, how to scoreLearning setupLinear models: weighted sum w·x + bLinear modelsOther classic models: trees, margins, neighboursOther classic modelsUnsupervised: find structure without labelsUnsupervisedTraining: loss, gradient, small stepTrainingNeural networks: layers of σ(W·a + b)Neural networksArchitectures: how layers share informationArchitecturesLanguage models: predict the next tokenLanguage modelsLLM speed and tuning: cheaper to run, safer to useLLM speed and tuningRetrieval and agents: give the model data and toolsRetrieval and agentsLinear regressionLinear regressionminimize squared residualsLogistic regressionLogistic regressionsigmoid of w·x + b, log lossGenerative vs DiscriminativeGenerative vs DiscriminativeModel P(x|y) and use Bayes, or fit P(y|x)PerceptronPerceptronfix weights only on mistakesHopfield networkEnergy only falls; recall = nearest valleySupport vector machineSupport vector machinewidest margin, set by support vectorsL1 vs L2 regularizationL1 vs L2 regularizationL1 gives exact zeros, L2 shrinks allOpen the Machine Learning mind mapMachine Learningweighted sum w·x + bLinear modelsLinear regressionLinear regressionminimize squared residualsLogistic regressionLogistic regressionsigmoid of w·x + b, log lossGenerative vs DiscriminativeGenerative vs DiscriminativeModel P(x|y) and use Bayes, or fit P(y|x)PerceptronPerceptronfix weights only on mistakesHopfield networkEnergy only falls; recall = nearest valleySupport vector machineSupport vector machinewidest margin, set by support vectorsL1 vs L2 regularizationL1 vs L2 regularizationL1 gives exact zeros, L2 shrinks all
This page on the Machine Learning mind map · 88 pages in 10 branches · open it to see the whole map and test yourself in recall mode.