⏱️ Reading time: 13 min

In 1982, physicist John Hopfield published a neural network model that, four decades later, turned out to be the conceptual foundation of the attention mechanism underlying GPT, Claude, and Gemini. The core idea is associative memory: a network that stores complete patterns and retrieves them from an incomplete or noisy version, without using exact addresses the way a computer’s memory does.

📑 En este artículo
  1. TL;DR
  2. What is associative memory?
  3. Why this model matters
  4. How a Hopfield network works
  5. Practical examples: associative retrieval in four lines of Python
  6. How to start experimenting with associative memory
  7. Real-world use cases for associative memory
  8. Common mistakes and best practices
  9. Comparison: classic Hopfield, modern Hopfield, and transformer attention
  10. Going deeper: from discrete energy to continuous softmax
  11. Frequently Asked Questions
    1. What’s the difference between associative memory and a computer’s RAM?
    2. Are Hopfield networks still used today?
    3. Why is transformer attention described as a form of associative memory?
    4. How many patterns can a Hopfield network store?
    5. What is a modern or continuous Hopfield network?
    6. Do I need to understand calculus to follow this model?
  12. References

Researcher Alex Wang retraced that connection in a recent essay: he showed, step by step, how starting from nothing more than the idea of storing and retrieving patterns is enough to derive the attention formula, without memorizing it from a paper.

TL;DR

  • A Hopfield network retrieves a complete pattern from an incomplete or noisy version of that pattern.
  • The network’s energy decreases with each update until it settles into the stored pattern most similar to the input.
  • A transformer’s attention mechanism computes that same retrieval through similarity between vectors.
  • The Query, Key, and Value matrices turn that associative search into parallel matrix multiplications on a GPU.
  • Thirty-five years later, the 2017 attention paper formalized that mathematical equivalence.

What is associative memory?

Associative memory is a storage model in which a complete pattern is retrieved from part of itself or from a noisy version of it, without using an exact address the way a traditional computer’s memory does; Hopfield networks are its classic implementation.

In RAM, you request the data at address 0x4F2A and the machine hands it over regardless of how similar it is to what you were looking for. Content-addressable memory flips that order: you go in with a fragment of the data, a corrupted or incomplete pattern, and the system returns the stored pattern that most closely matches it. It’s the same logic your brain uses when you recognize a song from three notes.

Why this model matters

The Hopfield network was born in biophysics, not software engineering: Hopfield was trying to explain how the brain retrieves complete memories from partial cues. For years it remained a curiosity in neural network courses, outpaced in practice by more flexible architectures.

The turning point came when several researchers noticed that the update rule of a continuous Hopfield network matches, term for term, the attention formula popularized by the paper Attention Is All You Need in 2017. Understanding associative retrieval stops being academic archaeology: it explains why attention sometimes converges on the wrong token, just as a Hopfield network can settle into the wrong energy minimum, and why expanding the context window changes a model’s effective memory capacity.

For anyone tuning prompts or designing agents, this framing explains a familiar phenomenon: a piece of data repeated several times in the context gets retrieved more strongly than one mentioned just once. In energy terms, a pattern reinforced multiple times carves out a deeper well that’s easier for the network to find.

Hopfield formulated his model in 1982, before modern GPUs existed. Foto de Brett Jordan en Unsplash

How a Hopfield network works

A classic Hopfield network has N binary neurons, each with a value of +1 or -1. To memorize a pattern, the network doesn’t store it in a cell: it encodes it into a weight matrix W using Hebb’s rule, where each weight W_ij is computed as the product of the pattern’s i and j values, with the diagonal set to zero so no neuron reinforces itself.

Hebb’s rule works because the product p_i × p_j is positive when two neurons agree in the memorized pattern and negative when they differ. By summing those products into the matrix W, each pair of neurons learns to reinforce or inhibit each other based on how often they agreed across the stored patterns: it’s the mathematical version of Hebbian plasticity, summed up since the 1950s as “neurons that fire together, wire together.”

To retrieve a pattern from a corrupted input, the network updates each neuron one at a time: every neuron sums the signals it receives from the others, weighted by the connection strengths, and takes the sign of that sum as its new value. Repeating this update pushes the system down an energy function E = -0.5 × xᵗWx. Because the weights are symmetric, each asynchronous update guarantees the energy never rises, so the system converges to a stable minimum: the stored pattern closest to the input.

There are two ways to apply the update: asynchronous (one neuron at a time, in random order) or synchronous (all at once). The guarantee that energy never rises only holds for asynchronous updates with symmetric weights; updating every neuron at the same time can make the system oscillate between two states without ever converging, a detail that’s almost always left out of simplified explanations of the model.

That stable minimum is, by definition, the retrieved memory. Associative memory works because the network doesn’t look up an address: it falls into an energy well that matches the original pattern.

flowchart TD
    A["Noisy input pattern"] --> B["Compute network energy"]
    B --> C["Update a neuron based on the sign of its input"]
    C --> D{"Did energy drop?"}
    D -- "Yes" --> C
    D -- "No, it stabilized" --> E["Retrieved stored pattern"]

A classic Hopfield network’s capacity is limited: the classical 1985 theory by Amit, Gutfreund, and Sompolinsky puts the practical limit at around 14% of the number of neurons before stored patterns start bleeding into each other, a phenomenon known as crosstalk. With 100 neurons, that’s just 14 distinct patterns before retrieval starts failing.

Practical examples: associative retrieval in four lines of Python

The simplest example fits in a 4-neuron network. We store the pattern [1, -1, 1, -1] and then show it a version with one bit flipped, to see if the network corrects it:

import numpy as np

pattern = np.array([1, -1, 1, -1])
W = np.outer(pattern, pattern)
np.fill_diagonal(W, 0)

corrupted = np.array([1, -1, -1, -1])  # one bit flipped relative to the original
recovered = np.sign(W @ corrupted)
print(recovered)

The output is exactly the original pattern, even though the input had one different bit:

[ 1 -1  1 -1]

The corrupted neuron, the third one, which came in as -1 instead of 1, receives a net signal of +3 from the other three neurons, so its sign gets corrected in a single update. With longer patterns the correction takes several rounds, but the mechanism is identical.

The second example stores two patterns at once, [1, -1, 1, -1] and [1, 1, -1, -1], which are orthogonal to each other:

patterns = [np.array([1, -1, 1, -1]), np.array([1, 1, -1, -1])]
W = sum(np.outer(p, p) for p in patterns)
np.fill_diagonal(W, 0)

corrupted = np.array([1, -1, -1, -1])
recovered = np.sign(W @ corrupted)
print(recovered)

The network still retrieves [ 1 -1 1 -1] even though it’s now storing two patterns, because being orthogonal they don’t interfere with each other. If you add a third pattern correlated with either of the other two, the risk of crosstalk shows up well before the 14% limit mentioned above.

How to start experimenting with associative memory

To run the examples above on your machine you only need Python 3 and NumPy. On Linux and macOS:

python3 -m pip install numpy
python3 hopfield_demo.py

On Windows, run the same command using python instead of python3 from PowerShell or CMD.

Save the code from the two examples into a file called hopfield_demo.py and run it: no API keys, no external dependencies, and no GPU needed at this network scale.

Real-world use cases for associative memory

  • Teaching error correction: neural network courses use Hopfield networks to show how a distributed system corrects noise without an explicit correction algorithm, for example recovering a full binary image from half of its pixels.
  • Energy-based models: Boltzmann machines and diffusion models share the same logic of minimizing an energy function until it settles into a valid pattern, generating new data by letting the system fall toward a minimum on that surface.
  • Alternative attention layers: the paper Hopfield Networks is All You Need (2020) proposes continuous Hopfield layers as a direct replacement for attention layers in a transformer, with exponential rather than linear storage capacity.
  • Language model interpretability: framing attention as associative retrieval lets you treat a model’s memory failure as convergence to the wrong energy minimum, instead of an unexplained black-box mystery.
The paper “Hopfield Networks is All You Need” was published in 2020, thirty-eight years after the original model. Foto de Markus Winkler en Unsplash

Common mistakes and best practices

The most common mistake is overloading the network: storing more patterns than its capacity allows produces mixed-up retrievals, where the network converges to a spurious pattern that isn’t any of the stored ones. The classic fix is to reduce the number of patterns or use patterns with low correlation to each other, as in the orthogonal example above.

Another mistake is breaking the symmetry of the weight matrix W. The convergence guarantee depends on W being symmetric (W_ij = W_ji); if you train the network with a rule other than Hebb’s without checking for symmetry, you lose the mathematical guarantee that energy only decreases, and the system can oscillate without ever stabilizing.

Finally, it’s common to confuse the classic Hopfield network, binary and with iterative updates, with its modern continuous version, which retrieves a pattern in a single step using softmax. They’re mathematical relatives, but they have different capacity and convergence properties; treating them as interchangeable leads to wrong conclusions about how closely real attention actually resembles the original 1982 network.

Comparison: classic Hopfield, modern Hopfield, and transformer attention

The table summarizes the three versions of the same principle, from the oldest to the one most used in production today:

ModelUpdate ruleCapacityRelationship to attention
Classic Hopfield (1982)Sign of the weighted sum, iterative~0.14 × N patternsConceptual ancestor, discrete update
Modern continuous Hopfield (2020)Softmax over similarities, single stepExponential in NMathematically equivalent to an attention layer
Transformer attentionSoftmax(QKᵗ/√d)·VLimited by the context windowSpecial case of continuous associative retrieval

Going deeper: from discrete energy to continuous softmax

The modern version of the Hopfield network, described in Hopfield Networks is All You Need, replaces binary neurons with continuous vectors and swaps the update rule for an energy function based on log-sum-exp. The result of minimizing that energy in a single step is, term for term, the attention formula: softmax of the similarity between a query and a set of keys, multiplied by the associated values.

In transformer terms, the query is the corrupted or partial pattern entering the network; the keys are the stored patterns it’s compared against by similarity; and the values are what actually gets retrieved when a key matches. The difference from classic Hopfield is that retrieval doesn’t iterate: softmax computes, in one shot, a weighted combination of all the values, weighted by how similar each key is to the query.

sequenceDiagram
    participant Q as Query
    participant K as Stored keys (Key)
    participant V as Associated values (Value)
    Q->>K: compares similarity against each key
    K-->>Q: returns a similarity weight per key
    Q->>V: combines the values according to those weights
    V-->>Q: pattern retrieved by association
flowchart LR
    A["Input embeddings"] --> B["Query matrix"]
    A --> C["Key matrix"]
    A --> D["Value matrix"]
    B --> E["Query-Key similarity"]
    C --> E
    E --> F["Softmax over the similarities"]
    F --> G["Weighted sum of Values"]
    D --> G
    G --> H["Attention layer output"]

The continuous version adds an extra parameter, called beta (equivalent to the 1/√d factor in the attention formula), that controls how sharp the retrieval is. With low beta, the network returns a blurry average of all patterns similar to the query, like a fuzzy memory; with high beta, it converges to a single stored pattern, just like the classic 1982 network. Modern transformers operate somewhere in between: they neither average the whole context equally nor retrieve a single token, but produce a weighted combination where a few tokens dominate the output.

This framing doesn’t claim that the engineers who designed the 2017 attention paper started from Hopfield; it shows that two separate research paths converge on the same formula. What it does let you do is derive attention from a physical intuition, retrieving a complete memory, instead of memorizing it as a linear algebra formula with no apparent motivation.

💭 Key point: the exponential capacity of modern Hopfield is the mathematical reason why expanding a language model’s context doesn’t linearly multiply its ability to “remember”: the relationship is far more favorable than in the classic 1982 network.

Your next step: open a terminal, paste the code from the two examples into hopfield_demo.py, and try adding a third pattern correlated with the other two, until retrieval starts to fail.

📬 Get new articles by email

We only email about big articles (1-2 a month).

Frequently Asked Questions

What’s the difference between associative memory and a computer’s RAM?

RAM retrieves data by its exact address, regardless of its content. Content-addressable memory, by contrast, retrieves a complete pattern from part of it or from a noisy version, comparing similarity instead of addresses.

Are Hopfield networks still used today?

The classic 1982 version is used almost exclusively for teaching purposes, because of its low storage capacity. Its modern continuous version, however, is the theoretical foundation behind active research proposals to redesign attention layers.

Why is transformer attention described as a form of associative memory?

Because the attention formula, softmax of the similarity between query and keys multiplied by values, is mathematically equivalent to one update step of a continuous Hopfield network. Both solve the same problem: retrieving the stored pattern most similar to a partial query.

How many patterns can a Hopfield network store?

The classic binary network reliably stores around 14% of its number of neurons before patterns start bleeding into each other. The modern continuous version raises that figure to exponential growth relative to the number of neurons.

What is a modern or continuous Hopfield network?

It’s the version described in 2020 that replaces binary neurons with continuous vectors and the iterative update rule with a single-step softmax, achieving greater capacity and a direct mathematical connection to the attention mechanism.

Do I need to understand calculus to follow this model?

Not for the intuition: vector algebra and the idea of a system falling toward an energy minimum, like a ball rolling down a surface to its lowest point, are enough. Calculus only comes in when you want to formally derive the 2020 continuous version.

References

📱 Do you like this content? Follow @programacion on Telegram for daily tech content in Spanish: quick summaries, fresh content every day.

Featured image: Foto de Logan Voss en Unsplash

Did it work for you? Got a different error? Say so below: questions get answered and help the next reader.

Leave a comment

Andrés Morales

Developer and AI researcher. Writes about language models, frameworks, developer tooling, and open source releases. Covers ML papers, the tech startup ecosystem, and programming trends.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

You can include code inside <code>…</code> or, for several lines, <pre><code>…</code></pre>.

This site uses Akismet to reduce spam. Learn how your comment data is processed.