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.
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.
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.
| stored | stable states | what happens |
|---|---|---|
| T and X | 4: T, X, −T, −X (all at E = −32) | every cue ends in a stored picture or its reverse |
| T, X and O | 14: 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 U | 6: O is no longer one of them | O 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.
How it relates to other pages
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.