TL;DR
An associative memory stores pairs of a key vector and a value vector. To recall, you give it a query, and it returns the value whose key the query resembles most. Unlike a computer's memory, you do not look up an address; you look up by content. The same idea, written with softmax weights, is the attention step inside a Transformer, so understanding the simple memory explains much of how modern networks retrieve information.
Builds on: Dot Product, Softmax · Leads to: attention and the memory layers studied in Titans
Where it's used
Attention in language models. Each token in a transformer produces a query, and every earlier token offers a key and a value. The layer scores each key against the query, turns the scores into weights with softmax, and returns the weighted sum of values. That is an associative memory read, performed once per token per layer.
Key-value caches. While a language model generates text, it stores the keys and values of every earlier token so it does not recompute them. The cache is literally an associative memory that grows as the text grows, and serving systems spend much of their memory budget on it.
Editing facts inside a network. Research on how large language models store facts has treated some of their linear layers as associative memories: a fact is a key (the subject in context) paired with a value (the attribute). Editing one fact then means changing one stored association without retraining the whole model. Interpretability and model-editing papers study this directly.
How it works
Take the smallest memory that can hold two pairs. We use two-dimensional vectors. The keys are and , and the values are and . The memory is a matrix , and recall is a matrix-vector product.
- Store the first pair. Build the matrix by multiplying each entry of by each entry of . Input: one key and one value. Output: a matrix. Check: , since has a in its second place.
- Store the second pair and add. Build and add it to the first matrix. The memory is . Check: each stored pair adds one outer product, and the two outer products do not overlap here.
- Recall with the first key. Compute . Check: this is exactly .
- Recall with the second key. Compute , which is exactly . Check: both keys retrieve their own values.
- Recall from a noisy cue. Use the query , which is close to . Compute . Check: is close to . The memory returns something near the stored value, not an exact copy.
The flowchart shows the two halves of the memory: writing adds outer products, and reading takes a matrix-vector product.
flowchart LR A["Pair (k, v)"] --> B["Outer product v k transpose"] B --> C["Add to memory W"] Q["Query q"] --> D["Compute W q"] C --> D D --> E["Approximate value"]
The math
Definition
Let be key vectors in and be value vectors in . The memory is the matrix
Recall with a query returns , where is the dot product of key and query.
The notation means the row version of the column vector , so is an matrix. The dot product measures how much the query resembles key , and the recall is a weighted sum of all stored values, where each weight is that resemblance.
Step 1 in symbols: the outer product
Each stored pair adds the matrix , whose entry in row and column is . In the example, has entries , , and .
Step 2 in symbols: recall of a stored key
Compute , using , the dot product written as a row times a column. Split off the term :
If the keys are orthonormal, meaning when and otherwise, the sum vanishes and exactly. This is what happened in steps 3 and 4 of the walkthrough, where and are the standard basis.
Step 3 in symbols: crosstalk
When the keys are not orthogonal, the second term is the crosstalk: other stored values leak into the answer with weights equal to their dot products with the query. The next example shows the size of this effect. A useful rule is that nonzero orthogonal keys cannot number more than the dimension . Exact recall of arbitrary values needs linearly independent keys, so a linear memory in dimensions cannot recall more than arbitrary pairs exactly.
Step 4 in symbols: softmax recall
Attention changes the weights. Replace by and pass the scores through softmax:
The weights are positive and sum to one, so the output is a convex mix of stored values. When is large, almost all the weight goes to the best-matching key, and the memory behaves like a sharp lookup. Step 5 of the walkthrough, with , shows the soft version.
Worked examples
Keys and are orthonormal. Values and are stored. Recall with the mixed query .
- The memory is .
- Dot products of the query with the keys are and .
- Apply the recall formula: .
- Check by multiplying out: , which agrees.
- Since both keys contribute equally, the answer is the sum of the two values, not either one.
- Check with the orthogonality rule: with orthonormal keys each recall of a single key is exact, but a query that matches two keys returns their sum.
- Interpretation: the memory returns what was stored for each key, and a query that points between keys returns a blend. Nothing has been lost, but the answer is no longer one stored value.
Keys and are not orthogonal. Values and are stored.
- The memory is . The first term is and the second is . So .
- Recall with : compute .
- Compare with . The answer is wrong, and the error is , which is exactly .
- Explain it with Step 3: the crosstalk term is , and adding it to gives .
- Recall with : compute . The exact answer is not returned.
- Check: the crosstalk formula gives , which agrees.
- Interpretation: a key that overlaps others gets its own value plus a share of theirs. The more the keys overlap, the more the memory mixes, which is why memories with many similar keys need orthogonalising or sharper weights.
Use the keys and values from the walkthrough, , , , , with . Recall with the noisy query .
- Scores: and .
- Exponentials: and .
- Sum: .
- Weights: for , and for . They sum to one.
- Weighted sum, first coordinate: .
- Weighted sum, second coordinate: .
- Check: the result lies between and , closer to , as the weights predicted. Interpretation: the query mostly matches the first key, so the output is mostly with a little of .
Try it: with the same memory and , recall with . Answer: the scores are and , the weights are and , and the output is about . The second key matches best, so the output leans toward .
Common mistakes and tradeoffs
Mistake 1: "A memory with a fixed size can store any number of pairs exactly." It is tempting to think that adding more outer products always keeps every value intact. In fact the crosstalk term grows with the number of overlapping keys. Exact recall of arbitrary values needs linearly independent keys, which is impossible beyond pairs in dimensions, so past that point recall becomes approximate.
Mistake 2: "Recall is a database lookup, so the answer is one stored value." It is tempting to think a query returns exactly one value. In fact a linear memory returns a blend weighted by dot products, and softmax memory returns a convex mix. A sharp approaches a single value but never reaches it exactly for finite .
Mistake 3: "A dot product of a query with a key measures meaning." It is tempting to think the score is a semantic similarity. In fact it measures alignment in the space the keys were learned in. Two keys that are semantically related are similar only if the network has learned keys that place them close together, which is why cosine similarity, which removes length, is often used instead of raw dot products.
When to use it. Use an associative memory when the lookup must work by content, when the number of stored items is moderate, or when the memory must be updated by simple additions. When not to. If you need exact retrieval of many distinct records, a conventional indexed store is better. Linear memories cost numbers regardless of how many pairs are stored, but they blur; softmax memories keep every pair and cost time proportional to the number of stored items for each query. Alternatives include orthogonalised keys, error-correcting codes for keys, and sparse or hashed memories.
History
The idea goes back to the neuroscience of learning. In 1949 Donald Hebb proposed that connections between neurons strengthen when they are active together, a rule that makes a weight matrix the sum of outer products of patterns, exactly the form of the linear memory above. Teuvo Kohonen and James Anderson independently studied such matrices as memories in 1972, showing that a correlation matrix could recall stored patterns from partial cues. In 1982 John Hopfield described a network whose stored patterns are attractors, which made associative recall a standard model of memory. Its limited capacity, roughly a fraction of the number of neurons, was a central problem for decades. In 2017 the transformer used softmax-weighted lookup, a sharper form of the same memory, as its core operation, and in 2020 researchers showed that a modern Hopfield network with softmax has much larger capacity. The memory view caught on because it explains attention in terms that were already well understood.
Recap
- An associative memory stores pairs of key and value vectors as the sum of their outer products.
- Recall with a query is a weighted sum of stored values, with the weights given by dot products between the query and the keys.
- Orthogonal keys give exact recall; overlapping keys produce crosstalk that blurs the answer.
- Softmax weighting, as in attention, turns the same memory into a sharp, normalised lookup, with the sharpness set by a scale factor.
What to read next: Attention Mechanism, which uses this lookup in every layer of a transformer, and Titans, which builds a learned memory module on the same idea.
Check yourself
QuestionRecall: what is the value returned by an associative memory when the query equals a stored key whose key is orthogonal to all other stored keys?Show answer
It returns the value stored with that key, multiplied by the squared length of the key, since the recall term is . Every other term in the recall sum has a zero dot product with the query, so it vanishes. With keys of length 1, as in the walkthrough, the answer is exactly the stored value.
QuestionComputation: keys and store values and . What does the memory return for the query ?Show answer
The memory is . Recall gives . Check: the dot products are and , as used.
QuestionWhy: why does adding a second pair with a key that overlaps the first change the answer for the first key?Show answer
Recall with gives . The second term is nonzero when , so the value leaks into the answer for . Only orthogonal keys avoid this crosstalk.
QuestionApplied: a designer builds a memory with 8 stored keys in a 4-dimensional space and expects exact recall of every value. Is this reasonable?Show answer
No. Exact recall with a linear memory needs orthogonal keys, and in four dimensions there can be at most four mutually orthogonal nonzero keys. With eight keys some overlap is forced, so some recalls will mix other values in. The designer should use fewer pairs, larger dimensions, or a sharper softmax lookup that accepts approximate recall.