Foundations to Frontier
Tests
E. Deep Learning › E6 Early and Energy-Based Networks

Modern Hopfield Networks

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.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

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. 1. Modern Hopfield retrieval weights stored patterns by…
  2. 2. The modern update is the same computation as…
  3. 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. 1. Update of ξ = (1, 1) with X = I₂, β = 1:
  2. 2. Transformer β for d_k = 64:
  3. 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).
def retrieve(X, xi, beta): return X.T @ torch.softmax(beta * X @ xi, dim=0)
🐍 Exercise 1: the update is attention
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. 1. Larger β made one-step retrieval…
  2. 2. HopfieldPooling resembles…
  3. 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. 1. Using F = exp gives capacity…
  2. 2. The update follows from the energy by…
  3. 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

  1. D. Krotov, J. J. Hopfield (2016). “Dense associative memory for pattern recognition.” Advances in NeurIPS.
  2. 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.
  3. H. Ramsauer et al. (2021). “Hopfield networks is all you need.” Proc. ICLR.