Modern (dense) Hopfield networks replace the classical quadratic energy by one built from a log-sum-exp over stored patterns, so retrieval sharpens exponentially and the network can store a number of patterns that grows exponentially with the dimension. With continuous states the update ξ ← Xᵀsoftmax(βXξ) retrieves a memory in one step, and it is exactly the attention mechanism of transformers. This page covers the dense associative memory, the continuous update and its energy, the role of the inverse temperature β, and the link to attention. In the exercises you verify the update equals attention and lowers the energy, then compare capacities in 64 dimensions: the classical network failed by 16 patterns, while the modern one recalled every pattern among 16 384 when β was large enough, and blended patterns when β was too small.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Sharper memories, and the link to attention
A classical Hopfield network lets every stored memory pull on the state a little, so with many memories the pulls blur together. A modern Hopfield network first scores how similar the state is to each memory, then lets the best match dominate through a softmax, so even with a huge number of memories the right one wins. The retrieval step, weighting stored items by a softmax of their similarity to a query, is exactly what attention does in a transformer.
Associative memory: recall a stored pattern from a corrupted cue
cue (pattern 1, corrupted)
→
after 3 softmax steps
overlap with pattern 1: 1.000 · largest overlap with any other stored pattern: 0.125 · load P/N = 0.056
A classical Hopfield network with N = 144 units stores about 0.138 N ≈ 20 random patterns; push P beyond that, or corrupt the cue heavily, and recall fails or lands in a spurious mixture. Modern Hopfield networks replace the sum of outer products by a softmax over stored patterns, which separates memories far more sharply: with β large enough they recall P well beyond N. The softmax update is the same computation as attention.
Why engineers care
Seeing attention as associative memory explains what transformers do with their keys and values, why the softmax temperature matters, and why attention can fail by averaging many items. Hopfield layers are also used directly for retrieval tasks such as immune-repertoire classification and set-based learning.
📝 Quick check (Level 0)
0/3 answered
1. Modern Hopfield retrieval weights stored patterns by…
2. The modern update is the same computation as…
3. Modern Hopfield capacity grows…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
By hand
Stored patterns as rows of X; state ξ.
Update: ξ ← Xᵀsoftmax(βXξ).
Example: X = I₂, ξ = (10, 0), β = 1: weights about (1, 0), so ξ becomes (1, 0).
For ξ = (1, 1) the weights are (½, ½) and ξ becomes the blend (0.5, 0.5).
Energy: E(ξ) = −(1/β)log Σᵢ exp(βxᵢ·ξ) + ½‖ξ‖² (plus constants). The first term is a smooth maximum: lse of (0, 0) at β = 1 is log 2; of (5, −100) at β = 10, almost exactly 5.
Attention: with query q, keys K and values V, attention returns Vᵀsoftmax(Kq/√d_k). Setting q = ξ, K = V = X and β = 1/√d_k gives the Hopfield update; for d_k = 64, β = 0.125.
β controls sharpness: small β averages many patterns (metastable states); large β picks one.
📝 Quick check (Level 1)
0/3 answered
1. Update of ξ = (1, 1) with X = I₂, β = 1:
2. Transformer β for d_k = 64:
3. Small β tends to retrieve…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.
Modern Hopfield networks in code
Exercise 1.
The update equalled attention with query ξ and keys and values X.
Starting 0.77 from a stored pattern among 50 Gaussian patterns in 16 dimensions, one update landed 0.059 away at β = 1, 0.00045 at β = 2 and below 10⁻⁶ at β = 4.
The energy never increased over five updates.
Practice.
The hopfield-layers package provides Hopfield, HopfieldPooling and HopfieldLayer modules, which act as attention over stored or learned patterns.
Normalise patterns, or scale β with the expected similarity, so that one memory dominates when it should.
For retrieval over very many items, approximate the softmax with nearest-neighbour search (as in retrieval-augmented models).
Implement the modern Hopfield update, check it equals attention, measure one-step retrieval for several β, and check the energy decreases.
📝 Quick check (Level 2)
0/3 answered
1. Larger β made one-step retrieval…
2. HopfieldPooling resembles…
3. For very many patterns, the softmax can be approximated by…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.
Theory
Dense associative memory. Krotov and Hopfield (2016) replaced the quadratic energy −Σ(xᵢ·ξ)² by −ΣF(xᵢ·ξ) with sharply rising F (higher powers). Capacity grows like N^(n−1) for F(z) = zⁿ. Demircigil et al. (2017) used F = exp and obtained capacity exponential in N.
Continuous version. Ramsauer et al. (2021) defined E(ξ) = −lse(β, Xξ) + ½ξᵀξ + constants. The concave–convex procedure applied to this energy gives exactly ξ ← Xᵀsoftmax(βXξ), which never increases E and typically converges in one step when patterns are well separated.
Three kinds of fixed points.
Global average (small β): all patterns blend.
Metastable states: a subset of similar patterns blend.
Single-pattern fixed points (large β): one pattern is retrieved.
Transformer attention heads show all three, which is one way to interpret what different heads do.
Capacity versus separation. Retrieval succeeds when the true pattern’s score exceeds the others’ by enough that the softmax concentrates on it. With random patterns the largest spurious score grows only like √(2N log P), so P can grow exponentially in N before retrieval fails.
📝 Quick check (Level 3)
0/3 answered
1. Using F = exp gives capacity…
2. The update follows from the energy by…
3. Small-β fixed points are…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.
Classical versus modern capacity
History.
D. Krotov and J. Hopfield (2016) introduced dense associative memories.
M. Demircigil et al. (2017) proved exponential capacity with an exponential energy.
H. Ramsauer et al. (2021) showed “Hopfield networks is all you need”: the continuous update is attention.
Later work linked transformer blocks and energy-based models (for example the Energy Transformer, 2023).
Exercise 2. N = 64-dimensional ±1 patterns; each of up to 40 stored patterns is cued with 20% of its bits flipped. Classical: Hebbian weights with asynchronous updates. Modern: three softmax updates, then the sign. Fraction recalled exactly, two seeds:
stored patterns
classical
modern, β = 0.05
modern, β = 0.5
4
1.000
1.000
1.000
8
1.000
0.000
1.000
16
0.156
0.000
1.000
256
0.000
0.000
1.000
4096
0.000
0.000
1.000
16 384
0.000
0.000
1.000
The classical network failed by 16 patterns, a load of 0.25, beyond its 0.138N limit.
The modern network with β = 0.5 recalled every pattern with 16 384 stored, 256 times the dimension.
With β = 0.05 the softmax spread its weight over many patterns and returned a blend, failing from 8 patterns: capacity depends on β as much as on the architecture.
🐍 Exercise 2 (research): classical versus modern capacity
Store growing numbers of random patterns and measure exact recall from corrupted cues for a classical network and for a modern one at two values of β.
Modern Hopfield networks in AI, robotics and aerospace
Attention in transformers for language, vision and robot policies is this retrieval rule.
Memory-augmented agents store past observations and retrieve them by softmax similarity.
Few-shot recognition of objects or faults from a stored library uses the same associative lookup.
References
D. Krotov, J. J. Hopfield (2016). “Dense associative memory for pattern recognition.” Advances in NeurIPS.
M. Demircigil, J. Heusel, M. Löwe, S. Upgang, F. Vermet (2017). “On a model of associative memory with huge storage capacity.” J. Statistical Physics 168.
H. Ramsauer et al. (2021). “Hopfield networks is all you need.” Proc. ICLR.
Too small a β blends patterns; large enough β retrieved one of 16 384.
Practice
Show:
Level 0
Problem 1Level 0
First entry of softmax(0, 0)?
0.5.
Problem 2Level 0
Transformer β for d_k = 64?
0.125.
Problem 3Level 0
First entry after updating ξ = (10, 0) with X = I₂, β = 1 (to 3 decimals)?
≈ 1.
Level 1
Problem 4Level 1
First entry after updating ξ = (1, 1) with X = I₂, β = 1?
0.5.
Problem 5Level 1
lse of (0, 0) at β = 1?
log 2 ≈ 0.693.
Problem 6Level 1
lse of (5, −100) at β = 10 (to 3 decimals)?
≈ 5.
Problem 7Level 1
Attention weight on key (1, 0) for query (1, 0), keys (1, 0) and (−1, 0), scale 1?
e/(e + e⁻¹) ≈ 0.881.
Level 2
Problem 8Level 2
Distance after one update at β = 1 in exercise 1?
0.059.
Problem 9Level 2
Classical exact recall with 16 patterns in exercise 2?
0.156.
Problem 10Level 2
Ratio of stored patterns to dimension at 16 384 patterns in 64 dimensions?
256.
Problem 11Level 2
Modern recall at β = 0.05 with 8 patterns?
0.
Problem 12Level 2
Classical capacity 0.138N for N = 64?
8.8.
Problem 13Level 2
Starting distance in exercise 1?
0.773.
Level 3
Problem 14Level 3
Show that attention with q = ξ, K = V = X and scale β is the modern Hopfield update.
Attention computes weights softmax(βKq) = softmax(βXξ) over stored rows and returns Vᵀ(weights) = Xᵀsoftmax(βXξ), which is the Hopfield update. ∎
Problem 15Level 3
Explain why β = 0.05 failed with 8 patterns while β = 0.5 succeeded with 16 384.
The true pattern’s overlap with a cue with 20% flipped bits is about 0.6 × 64 ≈ 38, while random patterns overlap by about ±8. At β = 0.05 the score gap is only about 0.05 × 30 = 1.5, so several patterns get comparable softmax weight and the retrieved state is a blend whose sign differs from the true pattern in some units. At β = 0.5 the gap becomes about 15, the true pattern takes nearly all the weight, and even the largest of 16 384 random overlaps (about 35) stays below 38. ∎
Problem 16Level 3
Explain what a “metastable state” means for an attention head.
It is a fixed point of the update where the softmax spreads its weight over a group of similar keys, so the output is their average rather than any single value. For an attention head this means it summarises a cluster of related tokens rather than selecting one; whether that is useful depends on the task, and the temperature (β) controls which regime the head is in. ∎
Problem 17Level 3
Advise how to build a fault-signature lookup that matches a noisy sensor snapshot against 50 000 stored signatures.
Normalise signatures and snapshots, compute similarities, and retrieve with a softmax at a β chosen on held-out noisy examples so that the right signature dominates; report the top match and its weight as a confidence. At 50 000 entries, use an approximate nearest-neighbour index to find the top candidates and apply the softmax only to them. Check that genuinely ambiguous cases produce spread-out weights rather than a confident wrong match. ∎
Level 4
Problem 18Level 4
Modern recall at β = 0.5 with 4096 patterns?
1.
Problem 19Level 4
Classical recall with 256 patterns?
0.
Problem 20Level 4
Distance after one update at β = 2 in exercise 1?
0.00045.
Coding practice
🐍 Code 1 (Level 0): softmax
Stable softmax.
🐍 Code 2 (Level 1): retrieve
Xᵀsoftmax(βXξ).
🐍 Code 3 (Level 1): log-sum-exp
(1/β)log Σexp(βz), stably.
🐍 Code 4 (Level 1): attention weights
softmax(scale·Kq).
🐍 Code 5 (Level 0): transformer scale
1/√d_k.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Modern Hopfield Networks: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.