Search

Search papers and lessons

Associative Memory

Level 3 · 13 min read

An associative memory stores pairs of key and value vectors and returns the stored value whose key best matches a query, which is how attention and many modern memory layers look things up.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Linear algebra
  • Architectures
  • Neural network basics
  • Probability
  • ReadReady to readNot yet

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 k1=(1,0)k_1 = (1, 0) and k2=(0,1)k_2 = (0, 1), and the values are v1=(2,5)v_1 = (2, 5) and v2=(−1,3)v_2 = (-1, 3). The memory is a 2×22 \times 2 matrix WW, and recall is a matrix-vector product.

  1. Store the first pair. Build the matrix v1k1⊤v_1 k_1^\top by multiplying each entry of v1v_1 by each entry of k1k_1. Input: one key and one value. Output: a 2×22 \times 2 matrix. Check: v1k1⊤=(2050)v_1 k_1^\top = \begin{pmatrix} 2 & 0 \\ 5 & 0 \end{pmatrix}, since k1k_1 has a 00 in its second place.
  2. Store the second pair and add. Build v2k2⊤=(0−103)v_2 k_2^\top = \begin{pmatrix} 0 & -1 \\ 0 & 3 \end{pmatrix} and add it to the first matrix. The memory is W=(2−153)W = \begin{pmatrix} 2 & -1 \\ 5 & 3 \end{pmatrix}. Check: each stored pair adds one outer product, and the two outer products do not overlap here.
  3. Recall with the first key. Compute Wk1=(2⋅1+(−1)⋅0,  5⋅1+3⋅0)=(2,5)W k_1 = (2 \cdot 1 + (-1) \cdot 0,\; 5 \cdot 1 + 3 \cdot 0) = (2, 5). Check: this is exactly v1v_1.
  4. Recall with the second key. Compute Wk2=(2⋅0+(−1)⋅1,  5⋅0+3⋅1)=(−1,3)W k_2 = (2 \cdot 0 + (-1) \cdot 1,\; 5 \cdot 0 + 3 \cdot 1) = (-1, 3), which is exactly v2v_2. Check: both keys retrieve their own values.
  5. Recall from a noisy cue. Use the query q=(0.9,0.1)q = (0.9, 0.1), which is close to k1k_1. Compute Wq=(1.8−0.1,  4.5+0.3)=(1.7,4.8)Wq = (1.8 - 0.1,\; 4.5 + 0.3) = (1.7, 4.8). Check: (1.7,4.8)(1.7, 4.8) is close to v1=(2,5)v_1 = (2, 5). 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

Definition: Linear associative memory

Let k1,…,knk_1, \dots, k_n be key vectors in Rd\mathbb{R}^d and v1,…,vnv_1, \dots, v_n be value vectors in Rm\mathbb{R}^m. The memory is the m×dm \times d matrix

W=∑i=1nvi ki⊤.W = \sum_{i=1}^{n} v_i\, k_i^\top.

Recall with a query q∈Rdq \in \mathbb{R}^d returns Wq=∑i=1n(ki⋅q) viW q = \sum_{i=1}^{n} (k_i \cdot q)\, v_i, where ki⋅qk_i \cdot q is the dot product of key and query.

The notation k⊤k^\top means the row version of the column vector kk, so vk⊤v k^\top is an m×dm \times d matrix. The dot product ki⋅qk_i \cdot q measures how much the query resembles key ii, 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 viki⊤v_i k_i^\top, whose entry in row aa and column bb is (vi)a(ki)b(v_i)_a (k_i)_b. In the example, v1k1⊤v_1 k_1^\top has entries 2⋅12 \cdot 1, 2⋅02 \cdot 0, 5⋅15 \cdot 1 and 5⋅05 \cdot 0.

Step 2 in symbols: recall of a stored key

Compute Wkj=∑ivi ki⊤kj=∑ivi(ki⋅kj)W k_j = \sum_i v_i\, k_i^\top k_j = \sum_i v_i (k_i \cdot k_j), using ki⊤kj=ki⋅kjk_i^\top k_j = k_i \cdot k_j, the dot product written as a row times a column. Split off the term i=ji = j:

Wkj=vj (kj⋅kj)+∑i≠j(ki⋅kj) vi.W k_j = v_j \,(k_j \cdot k_j) + \sum_{i \neq j} (k_i \cdot k_j)\, v_i.

If the keys are orthonormal, meaning ki⋅kj=1k_i \cdot k_j = 1 when i=ji = j and 00 otherwise, the sum vanishes and Wkj=vjW k_j = v_j exactly. This is what happened in steps 3 and 4 of the walkthrough, where k1k_1 and k2k_2 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 dd. Exact recall of arbitrary values needs linearly independent keys, so a linear memory in dd dimensions cannot recall more than dd arbitrary pairs exactly.

Step 4 in symbols: softmax recall

Attention changes the weights. Replace ki⋅qk_i \cdot q by β (ki⋅q)\beta\, (k_i \cdot q) and pass the scores through softmax:

v^(q)=∑i=1nexp⁡(β ki⋅q)∑jexp⁡(β kj⋅q) vi.\hat{v}(q) = \sum_{i=1}^{n} \frac{\exp\bigl(\beta\, k_i \cdot q\bigr)}{\sum_{j} \exp\bigl(\beta\, k_j \cdot q\bigr)}\, v_i.

The weights are positive and sum to one, so the output is a convex mix of stored values. When β\beta 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 β=1\beta = 1, shows the soft version.

Worked examples

Example
1. Two orthonormal keys, exact recall

Keys k1=(1,0)k_1 = (1, 0) and k2=(0,1)k_2 = (0, 1) are orthonormal. Values v1=(2,5)v_1 = (2, 5) and v2=(−1,3)v_2 = (-1, 3) are stored. Recall with the mixed query q=(1,1)q = (1, 1).

  1. The memory is W=v1k1⊤+v2k2⊤=(2−153)W = v_1 k_1^\top + v_2 k_2^\top = \begin{pmatrix} 2 & -1 \\ 5 & 3 \end{pmatrix}.
  2. Dot products of the query with the keys are k1⋅q=1k_1 \cdot q = 1 and k2⋅q=1k_2 \cdot q = 1.
  3. Apply the recall formula: Wq=1⋅v1+1⋅v2=(2−1,5+3)=(1,8)Wq = 1 \cdot v_1 + 1 \cdot v_2 = (2 - 1, 5 + 3) = (1, 8).
  4. Check by multiplying out: Wq=(2⋅1+(−1)⋅1,  5⋅1+3⋅1)=(1,8)Wq = (2 \cdot 1 + (-1) \cdot 1,\; 5 \cdot 1 + 3 \cdot 1) = (1, 8), which agrees.
  5. Since both keys contribute equally, the answer is the sum of the two values, not either one.
  6. 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.
  7. 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.
Example
2. Non-orthogonal keys, with crosstalk

Keys k1=(1,0)k_1 = (1, 0) and k2=(1,1)k_2 = (1, 1) are not orthogonal. Values v1=(1,2)v_1 = (1, 2) and v2=(3,−1)v_2 = (3, -1) are stored.

  1. The memory is W=v1k1⊤+v2k2⊤W = v_1 k_1^\top + v_2 k_2^\top. The first term is (1020)\begin{pmatrix} 1 & 0 \\ 2 & 0 \end{pmatrix} and the second is (33−1−1)\begin{pmatrix} 3 & 3 \\ -1 & -1 \end{pmatrix}. So W=(431−1)W = \begin{pmatrix} 4 & 3 \\ 1 & -1 \end{pmatrix}.
  2. Recall with k1k_1: compute Wk1=(4⋅1+3⋅0,  1⋅1+(−1)⋅0)=(4,1)W k_1 = (4 \cdot 1 + 3 \cdot 0,\; 1 \cdot 1 + (-1) \cdot 0) = (4, 1).
  3. Compare with v1=(1,2)v_1 = (1, 2). The answer is wrong, and the error is (3,−1)(3, -1), which is exactly v2v_2.
  4. Explain it with Step 3: the crosstalk term is (k2⋅k1) v2=1⋅(3,−1)=(3,−1)(k_2 \cdot k_1)\, v_2 = 1 \cdot (3, -1) = (3, -1), and adding it to v1v_1 gives (4,1)(4, 1).
  5. Recall with k2k_2: compute Wk2=(4+3,  1−1)=(7,0)W k_2 = (4 + 3,\; 1 - 1) = (7, 0). The exact answer v2=(3,−1)v_2 = (3, -1) is not returned.
  6. Check: the crosstalk formula gives v1(k1⋅k2)+v2(k2⋅k2)=(1,2)+2(3,−1)=(7,0)v_1 (k_1 \cdot k_2) + v_2 (k_2 \cdot k_2) = (1, 2) + 2(3, -1) = (7, 0), which agrees.
  7. 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.
Example
3. Softmax attention over the same memory

Use the keys and values from the walkthrough, k1=(1,0)k_1 = (1, 0), k2=(0,1)k_2 = (0, 1), v1=(2,5)v_1 = (2, 5), v2=(−1,3)v_2 = (-1, 3), with β=1\beta = 1. Recall with the noisy query q=(0.9,0.1)q = (0.9, 0.1).

  1. Scores: k1⋅q=0.9k_1 \cdot q = 0.9 and k2⋅q=0.1k_2 \cdot q = 0.1.
  2. Exponentials: e0.9≈2.4596e^{0.9} \approx 2.4596 and e0.1≈1.1052e^{0.1} \approx 1.1052.
  3. Sum: 2.4596+1.1052=3.56482.4596 + 1.1052 = 3.5648.
  4. Weights: 2.4596/3.5648≈0.6902.4596 / 3.5648 \approx 0.690 for v1v_1, and 1.1052/3.5648≈0.3101.1052 / 3.5648 \approx 0.310 for v2v_2. They sum to one.
  5. Weighted sum, first coordinate: 0.690⋅2+0.310⋅(−1)=1.380−0.310=1.0700.690 \cdot 2 + 0.310 \cdot (-1) = 1.380 - 0.310 = 1.070.
  6. Weighted sum, second coordinate: 0.690⋅5+0.310⋅3=3.450+0.930=4.3800.690 \cdot 5 + 0.310 \cdot 3 = 3.450 + 0.930 = 4.380.
  7. Check: the result (1.070,4.380)(1.070, 4.380) lies between v1v_1 and v2v_2, closer to v1v_1, as the weights predicted. Interpretation: the query mostly matches the first key, so the output is mostly v1v_1 with a little of v2v_2.

Try it: with the same memory and β=1\beta = 1, recall with q=(0,1)q = (0, 1). Answer: the scores are 00 and 11, the weights are 1/(1+e)≈0.2691/(1 + e) \approx 0.269 and e/(1+e)≈0.731e/(1 + e) \approx 0.731, and the output is about (−0.193,3.538)(-0.193, 3.538). The second key matches best, so the output leans toward v2=(−1,3)v_2 = (-1, 3).

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 dd pairs in dd 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 β\beta approaches a single value but never reaches it exactly for finite β\beta.

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 d×md \times m 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 (k⋅k) v(k \cdot k)\, v. 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 k1=(1,0)k_1 = (1, 0) and k2=(0,1)k_2 = (0, 1) store values v1=(3,1)v_1 = (3, 1) and v2=(0,−2)v_2 = (0, -2). What does the memory return for the query q=(2,1)q = (2, 1)?Show answer

The memory is W=v1k1⊤+v2k2⊤W = v_1 k_1^\top + v_2 k_2^\top. Recall gives (k1⋅q)v1+(k2⋅q)v2=2(3,1)+1(0,−2)=(6,2)+(0,−2)=(6,0)(k_1 \cdot q) v_1 + (k_2 \cdot q) v_2 = 2 (3, 1) + 1 (0, -2) = (6, 2) + (0, -2) = (6, 0). Check: the dot products are 22 and 11, 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 k1k_1 gives v1(k1⋅k1)+v2(k2⋅k1)v_1 (k_1 \cdot k_1) + v_2 (k_2 \cdot k_1). The second term is nonzero when k2⋅k1≠0k_2 \cdot k_1 \neq 0, so the value v2v_2 leaks into the answer for k1k_1. 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.

Sources