Week 6 — Dense Memory, and Attention Derived from an Energy
Notes for the neural-networks course, Unit 1. Written by an AI assistant under the researcher’s supervision; see ai-authorship.
A continuous memory can retrieve a weighted average of stored vectors. Starting from an energy, we will derive those weights and prove descent for the fixed-memory update. Allowing the vectors used for comparison and retrieval to differ gives the attention operation used in a transformer.
Learning goals
- Explain what resource a capacity statement counts, and why exponential capacity does not mean exponential information in a coupling matrix.
- Derive the continuous recall update and prove that a full step decreases its energy, for every .
- Identify the attention operation in that update, and state the exact condition under which a more general attention field has a potential.
- Say what changes when the keys themselves move.
Reading
Primary text. Chapter 2, §2.8 through §2.11.
Primary literature, through the bibliography: Krotov and Hopfield (2016); Demircigil and collaborators (2017); Ramsauer and collaborators (2021); Vaswani and collaborators (2017) for the scaled dot-product convention; Hoover and collaborators (2023) for an architecture with its own explicitly defined energy. Krotov (2023) and Krotov and collaborators (2025) are the reviews to read after the meeting.
Prerequisites. Week 5. The log-sum-exp function and its gradient, from Week 1.
The question to keep in view
How can a state retrieve information from several stored vectors without choosing one in advance? We will compute a similarity score for each memory, normalize those scores into weights and average the memories. This operation becomes attention when the scoring vectors and the retrieved vectors are allowed to differ. The derivation below also identifies the special case in which repeated retrieval decreases a known energy.
1. Dense memory, and what a capacity counts
The Hopfield energy is quadratic in the pattern overlaps. Replacing the square by a more rapidly growing function rewards a large overlap more sharply, and expanding the result produces interactions among more than two spins at a time. Krotov and Hopfield developed this route to dense associative memory.
For suitable independent-pattern ensembles and retrieval definitions, a degree- interaction supports capacity scales whose leading power is , and exponential interactions support for suitable positive . These are model-dependent asymptotic statements, and the distinction between retrieving a typical pattern and retrieving all of them simultaneously is essential to them.
The book’s reading note is the part to dwell on. The Hebbian model summarizes its memories in an coupling matrix. A dense-memory implementation may instead retain the patterns explicitly, which is stored numbers and comparable work for a direct overlap evaluation. Exponential capacity as a function of state dimension therefore does not imply exponential information held in fixed-precision couplings.
Physical picture. Ask, every time, what is being counted. “Capacity” in Week 5 counted attractors of a dynamics whose couplings were an object built once and then the only thing stored. “Capacity” here counts patterns the energy can distinguish, in a construction that may be holding all of them in memory. Both are legitimate and they are not the same quantity, and the difference is not a subtlety — it is the whole content of the comparison. The same question recurs in Week 13 about scaling laws, where the resource is compute.
2. A continuous memory, and a descent proof
Let the evolving state be a vector and collect fixed memories as the rows of . The score vector holds the dot product of the current state with each memory. The energy
rewards alignment with high-scoring memories through the second term while the first prevents escape to infinite norm. Using the gradient of established in Week 1, the stationarity condition gives the update
which scores the memories, converts the scores into positive normalized weights, and returns a weighted mean of the memories. Every new state lies in their convex hull.
To prove descent, write . Convexity gives , so
The right side is a quadratic with minimum at . Completing its square gives . Combining the two relations proves
This holds for every with fixed. Bounded iterates plus the inequality force the step differences to vanish, and any accumulation point is stationary.
Physical picture. Read the update as a thermal average and it stops being mysterious. The softmax weights are Boltzmann factors over the memory index, with playing the role of the energy of memory and the inverse temperature of that average. The new state is then , the ensemble mean of the memories. At small every memory contributes and the average sits near the centroid; at large the memory with the largest dot product dominates, if that maximum is unique. It is also the nearest in Euclidean distance when the memories have equal norms. Without that norm condition, a large dot product is not the same as a small distance. Nothing here is a bath and nothing is extensive — is a sharpness parameter of a finite weighted mean — but the intuition for how the mean moves with is exactly the familiar one.
3. Mixture and retrieval: a bifurcation, not a phase transition
Take two antipodal unit memories. After one update the state is parallel to them, so a single scalar suffices, and the energy and update reduce to
For example, start at . At one update gives , moving toward the symmetric mixture. At it gives , moving toward the positive memory. The slope determines whether small differences in state grow or shrink locally.
The mixture has curvature and loses linear stability at , where two nonzero stable fixed points appear, with near threshold. At finite the retrieved values satisfy : retrieval is never exact at finite sharpness.
Physical picture. is the mean-field ferromagnet’s self-consistency equation, the bifurcation sits at exactly where sits, and the square-root growth of the order parameter is the familiar exponent. A physicist will recognize all of it in a second, which is precisely why the book stops to say what it is not. There is no extensive system here and no : this is the bifurcation of a one-dimensional map, and its partition function — a sum over two memories — is analytic in throughout. The resemblance is a resemblance of equations. Calling it a phase transition would import a thermodynamic limit that nothing has supplied.
4. What the identification with attention establishes
Attention uses a query to weight a collection of available items. Keys are the vectors used to compute the scores, values the vectors combined with the resulting weights, and a head is one such set of projections and weighted averaging. Scaled dot-product attention uses , which is the initialization argument of Week 8 and not a measured temperature.
If the values are the keys, the attention output is exactly the continuous memory update of §2, with the memories fixed. Independent query and key projections do not obstruct that reading. So the identification is real, and it is exact in the state space where it was proved.
Now push it. For general values, the update field is , whose Jacobian is with . A continuously differentiable field on is a Euclidean gradient field precisely when its Jacobian is symmetric everywhere, so the condition is
Taking satisfies it. So does , since . For an explicit counterexample take ,
At the weights are and
The off-diagonal derivatives disagree, so no scalar Euclidean potential has as its gradient on . The test assumes values and keys have the same dimension as the state; a map into another dimension is not a vector field on that state space.
This is the familiar curl-free test for a potential. If , then is a candidate energy gradient. That identifies a potential; it does not yet show that the full iteration decreases it. The majorization argument above supplies that extra result for the fixed-memory choice .
Two further restrictions close the section. In self-attention the queries and the available items come from the same sequence, so if the sequence evolves the keys themselves change, and a candidate energy for the whole sequence must be differentiated accounting for each vector appearing as both query and key — derivatives the fixed-memory proof does not contain. And a transformer applies successive layers, generally with different parameters: a sequence of maps that each decrease different energies does not decrease one common energy, and parameter sharing is neither necessary nor sufficient for one to exist.
5. When new inputs keep arriving
Recall so far has meant evolving a state against fixed memories. §2.11 asks what happens when new information arrives before recall has finished, and reduces the question to a scalar driven map, . Holding fixed does not make the input constant. For the derivative with respect to the previous state is bounded by , so two states exposed to the same inputs become less distinguishable at every step.
That is the bridge into Unit 2, and Week 9 develops it: a route through the computation is not the same thing as retained information.
Checkpoints with answers
1. Where does the new state lie? In the convex hull of the fixed memories, because all weights are nonnegative and sum to one. It need not equal any single memory.
2. Are keys and values doing the same job? Keys determine the weights; values are what those weights average. They coincide in the energy-derived model. Attention allows them to differ, as the explicit matrix example shows.
3. Why is finding a potential insufficient for a full-step descent claim? A gradient direction gives a local decrease for a sufficiently small step. A finite iteration needs its own step-size or majorization argument.
Subtleties and fine print
Descent is not retrieval. The inequality of §2 guarantees the energy falls and the steps shrink. It does not guarantee convergence to a single memory, and a unique limit requires further conditions.
The energy lives in query space with the memories fixed. Quoting the descent result without that qualification is the most common misuse of it, and §4 is the demonstration of what goes wrong.
A potential does not imply a finite step descends it. The family has an explicit potential, and that alone says nothing about whether the iteration decreases it. The majorization inequality is what supplies that for , and it has to be redone otherwise.
The scaling is an initialization argument. It moderates the growth of score fluctuations with dimension under an assumption of independent unit-variance coordinates. Learned queries and keys need not satisfy that assumption.
Architectures with their own energy must be read on their own terms. The Energy Transformer defines an energy and a dynamics explicitly; conclusions about it follow from its definitions, not from the fixed-memory proof here.
Key claims and status
| Claim | Status |
|---|---|
| Degree- interactions give capacity scales with leading power | [Model-specific]; ensemble and retrieval criterion dependent |
| Exponential interactions support for suitable | [Model-specific]; not a universal maximum |
| [Exact.] | |
| [Exact] for every , fixed | |
| Every update lies in the convex hull of the memories | [Exact.] |
| Mixture loses stability at ; | [Exact] stability boundary; [Asymptotic] amplitude near the bifurcation |
| Key-equals-value attention is the memory update | [Exact] in the stated state space |
| Gradient field iff everywhere | [Exact]; sufficient, not necessary |
| A general attention field need not have a potential | [Exact], by the counterexample |
| Contraction of the driven scalar map for | [Exact.] |
What the week establishes
- A capacity statement counts a resource, and the resource differs between the Hebbian and dense constructions even when the word does not.
- The continuous memory update is the gradient-based fixed-point iteration of an explicit energy, and a full step decreases that energy by at least half the squared step, for every sharpness and every fixed memory set.
- With values equal to keys, that update is the attention operation exactly — the derivation the course was built towards.
- For general values, having a potential is equivalent to a symmetry condition on the Jacobian at every attainable state, which some attention fields satisfy and others demonstrably violate.
What to carry forward. The identification is real and it is narrow, and both halves matter. A student who leaves with only the first half will read every transformer as an energy-descent machine; one who leaves with only the second will miss that the course’s central derivation went through. The honest summary is the one the book gives: an update with an energy in memory space does not by itself supply an energy for an entire transformer.
The practical session — Laboratory 3
Varied: the sharpness , in the antipodal two-memory system; then the value matrix , with the keys held fixed. Held fixed: the memory set, the norm convention, and the state space — declared explicitly, since the whole point is that the results are statements about a state space. Measured: the fixed points against the bifurcation at ; the energy at every update, checked against the descent inequality term by term; and the antisymmetric part of the Jacobian as is moved away from .
Product: the fixed-point diagram with the bifurcation located; a table of against showing the inequality holding; and the value matrix at which the antisymmetric part first becomes nonzero, with the potential lost.
A larger-memory experiment must state its pattern ensemble, norm convention, noise level and retrieval criterion. A finite sweep establishes neither exponential capacity nor its absence.
Connections to other parts of the wiki
- Week 5 supplies the discrete construction this week makes continuous, and the capacity whose resource §1 contrasts.
- Week 1 supplies the log-sum-exp gradient used in §2, and the warning about the Gibbs reading that §3 applies again.
- Week 8 builds the full attention layer whose special case §4 identifies, and supplies the argument.
- Week 9 develops the driven map of §5 into the course’s treatment of recurrence and memory.
- Week 11 studies a particle dynamics on the sphere whose monotone function is proved separately, precisely because the fixed-memory proof does not extend to it.
- nn-llm-unit-1 — the block skeleton this note implements.
Start here · All weeks · Book and experiments · Previous week · Next week