An interactive visualization of Hopfield networks — one of the most elegant ideas in computational neuroscience. This document walks through the theory, the math, and the intuition behind how a network of simple binary units can store and recall patterns from memory.
Try the live demo — draw patterns, train the network, corrupt them with noise, and watch it reconstruct them.
- Hopfield Networks: From Neurons to Memory
- Table of Contents
- The Big Idea
- The Neuron Model
- Connecting Neurons: The Weight Matrix
- Learning: The Hebbian Rule
- Recall: How the Network Remembers
- The Energy Function
- Why It Works: Energy Minimization
- Capacity: How Much Can It Remember?
- Failure Modes: Spurious States
- Connection to Physics
- References
Imagine you see half of a friend's face. Instantly, your brain fills in the rest. You don't compute anything consciously — the complete image just appears. Hopfield networks are a mathematical model of this process: content-addressable memory.
Unlike a hard drive where you look up data by address (give me byte 0x4F2A), a Hopfield network retrieves data by content (give me the pattern that looks most like this). You feed in a corrupted, partial, or noisy version of something it has seen before, and it reconstructs the original.
The key insight, due to John Hopfield (1982), is that this can be understood as an energy minimization problem. The stored patterns are valleys in an energy landscape. Recall is the process of rolling downhill into the nearest valley.
The network consists of
That's it. No continuous activations, no layers, no backpropagation. Each neuron is either ON (
In this visualization,
$N = 400$ neurons are arranged on a 20 x 20 grid. Cyan cells are ON ($-1$ ), dark cells are OFF ($+1$ ). The choice of$-1$ for ON is a convention from physics (spin down = active).
The full state of the network at any moment is a vector:
This vector lives in a space with
Every neuron is connected to every other neuron (but not to itself) through a symmetric weight
The weight
The full weight matrix
In the visualization, the "Per Neuron" weight map shows one row of this matrix reshaped back onto the 20 x 20 grid. Cyan = excitatory connection, red = inhibitory. The "Full Matrix" view shows all
$N \times N$ entries as a heatmap.
How do we choose the weights so the network remembers a set of patterns? We use Hebb's rule, inspired by the neuroscience principle: neurons that fire together, wire together.
Suppose we want to store
The Hebbian learning rule sets each weight to:
Let's unpack what this does:
- If neurons
$i$ and$j$ are both ON or both OFF in a stored pattern ($\xi_i^\mu = \xi_j^\mu$ ), their product is$+1$ , contributing a positive (excitatory) weight. - If they disagree (
$\xi_i^\mu \neq \xi_j^\mu$ ), the product is$-1$ , contributing a negative (inhibitory) weight. - The sum over all patterns means the weight reflects the net agreement across all stored memories.
- The
$1/N$ factor is a normalization to keep the weights from growing with network size.
This can be written compactly in matrix form:
The
Consider two neurons that are both ON in patterns 1 and 3, but disagree in pattern 2:
| Pattern | Neuron |
Neuron |
Product |
|---|---|---|---|
| 1 |
|
|
|
| 2 |
|
|
|
| 3 |
|
|
The net weight is positive — these neurons mostly agree, so they have an excitatory connection.
Recall is an iterative process. We start from some initial state (possibly a corrupted pattern) and repeatedly update neurons until the network settles.
The update rule for neuron
In detail:
-
Pick a random neuron
$i$ -
Compute its local field (the weighted sum of all other neurons' states):
$$h_i = \sum_{j=1}^{N} W_{ij} , s_j$$ - Update the neuron: $$s_i = \begin{cases} +1 & \text{if } h_i \geq 0 \ -1 & \text{if } h_i < 0 \end{cases}$$
- Repeat until no neurons change (convergence)
The local field
This is asynchronous updating — one neuron at a time, chosen randomly. This is crucial. Synchronous updating (all at once) can lead to oscillations and doesn't guarantee convergence.
In the visualization, the inference panel shows this computation for a selected neuron: the excitatory sum (positive votes), inhibitory sum (negative votes), the local field
$h_i$ (net vote), and whether the neuron would flip. The bar chart shows every individual contribution$W_{ij} s_j$ .
Here is where Hopfield's insight connects neural networks to physics. Define an energy function over the network's state:
This is a scalar value that depends on the current configuration of all neurons. Lower energy = more "comfortable" configuration.
Key theorem: When a single neuron updates according to the rule above, the energy never increases:
Proof sketch: When neuron
where
If the neuron flips from
If it flips from
If it doesn't flip,
In all cases,
The visualization shows the energy trace during recall — watch it monotonically decrease until convergence.
The stored patterns sit at (or near) local minima of the energy function. To see why, let's compute the energy when the network is in the state of pattern
Substituting the Hebbian weights:
For
For
So:
The first term is large and negative — the stored pattern has very low energy. Nearby states (a few neurons flipped) have slightly higher energy. The pattern sits at the bottom of an energy basin, and the update rule rolls downhill into it.
The energy landscape in the visualization shows exactly this. The x and y axes represent overlap with two stored patterns. Dark blue regions are low energy (basins). The cyan dot shows the current state rolling downhill during recall.
The picture to have in mind:
Energy
^
| ___ ___
| / \ / \
| / \_____/ \
|/ P1 P2 \
+-------------------------> State space
Each stored pattern
A natural question: how many patterns can we store before the network starts making mistakes?
For random patterns of length
This result, due to Amit, Gutfreund, and Sompolinsky (1985), means:
- Below
$0.138N$ patterns: recall is nearly perfect - Above
$0.138N$ patterns: the basins of attraction shrink, overlap, and eventually disappear - The network starts confusing patterns or converging to spurious mixtures
For our 400-neuron network:
The capacity is linear in
To measure how well recall works, we use the overlap between the network state and a stored pattern:
This ranges from
The visualization shows overlap bars after convergence — 100% means the recalled pattern matches the stored pattern perfectly.
Hopfield networks aren't perfect. The energy landscape has local minima that don't correspond to any stored pattern. These are called spurious states, and there are several kinds:
If
With three stored patterns
These are "chimera" patterns — blends of stored memories. They appear when patterns share structure and their basins overlap.
At high load (
The Hopfield network is mathematically identical to an Ising model from statistical mechanics. Each neuron is a spin, the weights are coupling constants, and the energy function is the Hamiltonian:
| Neural Network | Statistical Physics |
|---|---|
| Neuron state |
Spin |
| Weight |
Coupling |
| Energy |
Hamiltonian |
| Recall | Relaxation to ground state |
| Stored pattern | Ferromagnetic ground state |
| Spurious state | Metastable state |
| Capacity transition | Phase transition |
This connection is not just an analogy — it is an exact mathematical equivalence. The capacity result
Hopfield's 1982 paper, along with the subsequent statistical mechanics analysis, helped establish the field of neural network theory and contributed to Hopfield receiving the 2024 Nobel Prize in Physics.
- Draw a pattern on the grid (or load a preset)
- Store it — the weight matrix updates, a basin forms in the energy landscape
- Repeat for a second pattern — watch the landscape develop two distinct valleys
- Load one of the stored patterns
- Add noise to corrupt it
- Recall — watch the network converge back to the original pattern
- Use Step mode to see individual neuron updates with the full inference breakdown
- Select neurons to inspect their weights and contributions
| Key | Action |
|---|---|
Space |
Start/stop recall |
. |
Single step |
D |
Draw mode |
V |
Select mode |
C |
Clear grid |
N |
Add noise |
S |
Store pattern |
I |
Invert pattern |
- Hopfield, J. J. (1982). "Neural networks and physical systems with emergent collective computational abilities." Proceedings of the National Academy of Sciences, 79(8), 2554-2558.
- Amit, D. J., Gutfreund, H., & Sompolinsky, H. (1985). "Storing infinite numbers of patterns in a spin-glass model of neural networks." Physical Review Letters, 55(14), 1530.
- Hertz, J., Krogh, A., & Palmer, R. G. (1991). Introduction to the Theory of Neural Computation. Addison-Wesley.
Built with plain HTML, CSS, and JavaScript. No frameworks, no dependencies.