Manuscript Offprint
arXiv:2610.22191Attention over noisy examples: generalization and memorization in the exponential Hopfield network
Abstract
In Hebbian learning from examples a Hopfield network of neurons is not supplied with the archetypes it has to retrieve but with blurred examples of each, of quality , and whether it can infer them depends on the load (interference among them) and on the dataset entropy (how little the examples say about them). We transfer this protocol to the exponential Hopfield model, whose zero-temperature update is a step of softmax attention with the unlabelled examples as keys. A signal-to-noise analysis gives in closed form the overlap between the output and an unseen archetype, : the majority-rule law of the pairwise model at small load, with replaced by an attention entropy built from the weight the softmax gives to the correct class and the number of examples it spreads it over. Two consequences follow. First, in a window of inverse temperatures that widens as the interference between archetypes is exponentially small in : the load drops out and the number of archetypes that can be learnt grows as . Second, learning fails differently: in the pairwise model through a spin-glass phase, in the exponential model through memorization: the softmax concentrates its weight on the training example closest to the input and returns it in place of the archetype. Seen from the input the softmax weights are those of the random energy model, and memorization is its freezing transition, at ; it is measurable on a head whose keys are examples as the number of examples carrying weight. On MNIST the diagnostic follows the random-energy prediction; on fashion-MNIST it detects sub-clusters in the classes, and the best accuracy lies beyond the memorization threshold, where the softmax reads out a local sub-prototype.
1 Introduction
Forty years after Hopfield’s model [1] and the statistical-mechanical theory of Amit, Gutfreund and Sompolinsky (AGS) [2, 3], Hebbian neural networks have returned to the centre of the machine-learning stage through two independent developments. On the one hand, dense associative memories [4, 5, 6] and their exponential limit [7, 8, 9] have shown that a Hebbian network with sufficiently steep interactions stores an exponential number of patterns, that the steepness of the interaction controls a transition from feature-like to prototype-like memories [4], and that one step of the zero-temperature dynamics of the exponential model is a softmax attention update with the stored patterns as keys and values [7, 11], a connection also read as a nearest-neighbour or kernel-smoothing view of attention [21, 22, 23]. On the other hand, the Hebbian prescription itself has been turned from a storage rule into a learning rule: in [12, 13] the network is supplied, instead of the archetypes , with corrupted examples of each of them, and the Hebbian coupling is built on the examples, without labels (unsupervised, [12]) or with the examples averaged class by class (supervised, [13]); the same protocol has been solved for dense -body networks in [14, 15], where the load becomes (see also [16, 17, 18]). The resulting phase diagram is controlled by the AGS load and by a new parameter, the dataset entropy , with the quality of the examples; archetypes are retrieved although they were never presented, provided is small enough and lies below a -dependent critical load. Two sources of noise limit the retrieval: the finite number and quality of the examples, measured by , and the interference of the other stored patterns on the field acting on each neuron, usually called crosstalk, which grows with the load . When either is too large the network enters a spin-glass phase, in which its states are correlated with none of the archetypes: in the learning setting this phase is the thermodynamic signature of overfitting.
The present work merges the two lines. We ask what happens to the phase diagram when the examples themselves become the stored keys of the exponential Hopfield model, that is, of an attention layer, with no use of the labels. Three questions are addressed: (i) whether a layer of attention, operating on a dataset of noisy examples, generalizes to the unseen archetypes and in which range of the inverse temperature of the softmax; (ii) what is the fate of the load , which in the pairwise model bounds the number of archetypes that can be learnt; (iii) whether the spin-glass phase has a counterpart in the exponential model, i.e., what overfitting looks like when the network is a transformer layer rather than a Hopfield network.
The answers are obtained analytically by a signal-to-noise analysis, which in the present setting can be carried out in closed form, and are checked by Monte Carlo simulations. In a nutshell: in [13] the probability of reconstructing a pixel of the archetype by a majority vote over its examples is , and the one-step magnetization of the network has the same error-function form; here we find that the one-step magnetization of an unseen archetype produced by an attention layer is again , but the dataset entropy is replaced by an attention entropy that depends on how the softmax distributes its weight over the training set. At intermediate the load drops out for any ; at small the crosstalk of the pairwise model is recovered; at large the softmax condenses, i.e. it assigns essentially all of its weight to a single training example, and the layer memorizes. Seen from the query, the logits of the softmax are independent Gaussian variables, so that the attention layer is a random energy model (REM), the system of independent Gaussian energy levels introduced by Derrida [26, 27] as the simplest spin glass: the condensation is the freezing transition of that model, located at , and it is the attention counterpart of the spin-glass phase. The window of generalization between the two edges widens with the system size as . We then test the picture on MNIST and fashion-MNIST, and compare the Hebbian attention with a one-head attention classifier trained by gradient descent on the same datasets.
The present model is the end of the family of dense Hebbian networks learning from examples of [14, 15]: there the crosstalk is still extensive in the polynomial load , here it becomes logarithmic in , which is what allows the closed-form treatment. Related settings of learning from examples have been considered in [19] for dense p-body networks in the teacher-student scenario, in [20] for the random-features Hopfield model, and in [25] for hierarchical prototypes in polynomial dense memories. For the storage, rather than learning, properties of exponential models we refer to [6, 8, 9], and to [10] for the multilayer hetero-associative extension, whose exponential capacity and basins are obtained at by a large-deviation evaluation of the noise.
Definition 1 (Dataset). The archetypes are K binary vectors , , with i.i.d. Rademacher entries, . The dataset is made of M examples per archetype,
The control parameters are the load , the dataset entropy [13] and, for the exponential model, the inverse temperature introduced below. The archetypes are never shown to the network; a query is a further example of quality , with , independent of . The case is the archetype itself, is a fresh test example.
For each archetype we introduce the empirical average of its examples, , which is the best estimate of available from the dataset (the prototype of the class, obtained by the majority rule on each pixel [13]). For any vector the overlap of a state with it is ; the overlap with an archetype, , is the Mattis magnetization, the order parameter of retrieval: means that the state coincides with .
Definition 2 (Pairwise Hebbian networks [12, 13]). The Hopfield networks built on the class averages and on the examples (called supervised and unsupervised in [12, 13]) have cost functions
up to normalization and self-interaction terms, i.e., and respectively, with . The first one, which uses the labels, is the reference against which the attention layer is compared below.
Definition 3 (Exponential Hebbian network). Given a set of keys , the exponential Hopfield model [6, 7, 8] has cost function
Throughout, the keys are the examples themselves, , and the labels are not used; the alternative choice of the class averages as keys, , is discussed in Remark 6.
Definition 4 (Dynamics). All networks evolve by the zero-temperature parallel dynamics sign[h_i( )], where is the post-synaptic field. For (4)
Remark 1. Equation (5) is one head of self-attention with keys and values equal to the stored vectors and query , the logit being ; one step of Definition 4 is one transformer layer acting on the training set [7]. Expanding the softmax to first order in , with the average overlap of the query with the keys and the mean key,
that is, the pairwise field built on the examples, Eq. (3), plus the constant and the self-interaction: is the pairwise regime, is a nearest-key lookup. The parameter is the inverse temperature of the Gibbs measure (5) over the keys, i.e. of the exponential cost function (4), not of the neural dynamics, which is noiseless throughout; it is the “ ” of [7] and the “ ” of [8]. The logit is scaled as , so that is the regime of exponential capacity of [8, 9]; the natural scales of the present problem will turn out to be and .
Definition 5 (Observables). Let the query be an example of archetype . We distinguish the state after a single update, , which is what a transformer layer computes, from the fixed point of the iterated map, which is the Hopfield retrieval, and we write for the Mattis magnetization of the queried archetype. Generalization is measured by ; memorization by the overlap with the closest training example of the same class,
which equals one when the state is a training example. For the exponential network we also record two statistics of the attention weights of Eq. (5), evaluated on the query:
is the total weight that the softmax assigns to the examples of the correct class. is the participation ratio of the weights within that class, i.e. the number of examples that effectively contribute to the weighted sum: when the M examples carry equal weight (the layer pools them all, and the update is the majority rule), when a single example carries all the weight (the layer has condensed on it). We refer to W and as the attention statistics. Condensation is thus a property of the weights ( ), memorization one of the output ( ): with keys equal to examples of a single archetype the two coincide, on structured data they need not (Sec. 5). Figure 1 shows them on one query, at three temperatures, together with the outcome of the update: it anticipates the three regimes derived in the next section.
3 Signal-to-noise analysis
The signal-to-noise technique [2, 33, 13] evaluates the field acting on neuron i when the network is prepared in the query , approximates its distribution over the quenched disorder (archetypes, examples and query noise) by a Gaussian via the central limit theorem, and returns the magnetization after one parallel update as , where and are the mean and the variance of . In all cases the field is split into a signal, the contribution of the keys belonging to the queried archetype, and a crosstalk, the contribution of all the other keys; the magnetization is high when the former dominates the latter. We first record the pairwise result, which generalizes to a query of quality q the formula of [13] (there obtained for ), and then derive the exponential one.
Proposition 1 (pairwise network built on class averages). For the network (2) prepared in a query of quality q of archetype , in the limit at fixed and ,
Proof. With the normalization , , the field splits into the signal and the crosstalk . By the law of large numbers , while with standard Gaussian for , so that has mean and variance . Each crosstalk term is a product of two independent centred variables of variances and , and there are of them, giving variance (we exclude ). Dividing by one gets , whence (9). (The condition is used only for the Gaussianity of ; the crosstalk is Gaussian by the central limit theorem over .)
For , (9) is Eq. (68) of the Supplementary Material of [13]; the archetypes are stable under one update only in the region , and a query of lower quality shrinks that region by the factor in front of the load. We now turn to the exponential
(a) crosstalk,

(b) prototype,

(c) memorization,

Figure 1: The three regimes seen from one query. Attention weights (5) of the same query (a fresh example of archetype ; , , , ) over the same training set at three inverse temperatures, , and , where and are the two edges of the generalization window derived in Sec. 3, Eq. (17). In each panel the left part shows the weights of the keys of the correct class, the right part the total weight of each of the other classes (logarithmic scale, shared; bars below are not drawn). , Eq. (7), is the sum of the blue bars and , Eq. (8), their participation ratio; and are the overlaps of the updated state with the archetype and with the closest example. (a) Crosstalk: the correct class weighs as much as any other. (b) Prototype: the correct class carries all the weight, spread over its examples. (c) Memorization: one example carries it, and is returned.
network with keys = examples; the choice of the class averages as keys is discussed in Remark 6.
Lemma 1 (logit statistics). Let be a query of quality q of . Conditionally on the query and on the archetypes, the overlaps of the keys with it are, for , Gaussian variables
where the 's are independent standard Gaussians and is a shift shared by the keys of class . Within each class the keys thus differ only by independent fluctuations of spread ; the common shifts are those of the query and do not depend on the key.
Proof. Conditionally on , is a sum of independent variables with means and variances ; its mean is , the term being the same for all . For , has conditional mean and variance , whence (11). Averaged also over the query and the archetypes, the unconditional variances are and , but it is the conditional spread that governs the softmax, since the shifts cancel in the normalized weights of a class.
Lemma 1 says that, seen from the query, the KM logits of the attention layer are independent Gaussian variables, i.e. the energy levels of a random energy model [26, 27], with the softmax playing the role of the Gibbs measure. The model is a REM “with a planted block”: the M levels of the correct class have mean , those of a wrong class have a random mean of spread across classes, and within every class the levels have the same spread . The two attention statistics of Definition 5 follow. For W we use the annealed approximation, in which the ratio of two random sums is replaced by the ratio of their averages; it is accurate as long as each sum is dominated by typical, rather than extreme, levels, i.e. below the freezing point of the corresponding REM: within a class and across the wrong classes.
Proposition 2 (weight of the correct class). In the annealed approximation, valid for and ,
Proof. Averaging the Boltzmann sums over the Gaussian variables of Lemma 1,
the ratio of the former to the total gives (12). Beyond the stated range the sums are dominated by their largest terms and the annealed average overestimates them: the exponent in (12) is then no longer the right one, and indeed it would turn non-monotonic at , far outside the window considered below.
Proposition 3 (condensation of the softmax). Let
with i.i.d. standard Gaussian, be the participation ratio (normalized to one) of the Gibbs measure of a REM with M levels at inverse temperature x: it counts the fraction of levels that carry a non-negligible weight. Then
independently of the quality of the query. For small , , so that the pooling starts to degrade at ; for the measure freezes at onto the levels of lowest energy; in the frozen phase the inverse participation ratio has the exact average [26, 27, 29, 30], so that, by Jensen's inequality, : a finite number of examples, of order one, carries all the weight. The inequality is strict, because the weights remain random in the frozen phase (they have the Poisson–Dirichlet statistics of [30]); the gap is 10–20% at (Appendix B). At the finite values of of interest here () the freezing is a crossover and is evaluated numerically.
Proof. Within the correct class, the common factor cancels from the normalized weights, which become by Lemma 1; the participation ratio of such weights is by definition . The small- expansion follows from (Appendix B); the freezing point is where the entropy of the levels equals the energy gain of the extreme level, , the frozen-phase value of is the classical result of [29, 30], and the bound on is Jensen's inequality for the convex function .
The same argument applied to the keys of a wrong class, whose levels have the same spread x by Lemma 1, gives the number of examples pooled there, . One last ingredient is needed before the central result can be stated: the logit of every key contains the term of the very neuron i that is being updated, so that the field receives a contribution proportional to the current state of that neuron. We call it the self-coupling; it is the exponential-model analogue of the diagonal term of the pairwise model, and it biases the update towards the query.
Proposition 4 (one attention step). For the exponential network (4) with keys = examples, prepared in a query of quality q of , in the limit ,
with the attention entropy
with , given by (12) and by (13). The result holds as long as the field is a sum of many comparable terms, i.e. or ; in the condensed regime the Gaussian approximation fails, see Remark 2.
Proof. Write the field (5) as and multiply by . The -th factor of each Boltzmann weight, , is singled out: writing with weights that do not contain site , one finds
the denominator being positive, so that the second identity is exact: the self-coupling enters the update as an additive field , and the weights are described by Lemma 1. (In the pairwise models of Definition 2 the analogous diagonal term is removed by convention; here it is part of the softmax and is kept.) Conditionally on the weights, has mean and variance , so the correct-class term has mean and variance . For a wrong class , and all its examples share the factor : the class contributes , of zero mean and variance with . Summing over the wrong classes with and gives the second term of the numerator; the self-coupling has mean and variance . Applying the central limit theorem to and yields (14) with . (We evaluate at its typical value; replacing by its average changes only where the Gaussian approximation is already inaccurate.)
Remark 2 (range of validity). Equation (14) has the form of the majority-rule bound of [13], , which gives the probability of reconstructing a pixel of the archetype from M examples: the attention layer performs a weighted majority rule, and is the entropy of that weighted vote. The Gaussian step of the derivation requires the field to be a sum of many comparable terms; it fails when the softmax has condensed on a few examples. The exact limit is however elementary: for the field is with , so and , up to the positive bias induced by the selection of the example closest to the query. Equation (14) tends instead to and overestimates by up to 0.1 in that regime, see Sec. 4.
Corollary 1 (three regimes). Let (a fresh test example) and .
We keep the name “entropy” for and as in [13], where it is justified by the monotonic relation between and the conditional entropy of the archetype given the examples; is a noise-to-signal ratio of the weighted vote and should not be confused with the Shannon entropy of the attention weights, also called attention entropy in the machine-learning literature [24].
(i) Crosstalk regime, : , and, neglecting the self-coupling, (15) reduces to
a crosstalk term with the same structure as in (9): the vote of the correct class is drowned by the wrong ones as long as , and the network is pairwise-like.
(ii) Prototype regime, and : the crosstalk term is below and decays exponentially in (W rises from to one), , hence up to the self-coupling correction , and independently of . The retrieval boundary in the plane is a horizontal line.
(iii) Memorization regime, : but ; the pooling degrades gradually for , where (14) with describes the decrease of , and beyond the freezing point the layer returns the training example closest to the query, and (Remark 2).
Figure 1 shows the three regimes on the attention weights of a single query. The generalization window is therefore bounded by a crosstalk edge , defined as the point where the crosstalk term of (i) equals the dataset entropy , i.e. where the noise coming from the other classes equals that coming from the finite number of examples, and a condensation edge , defined by , beyond which the correct class is represented by a finite number of examples. With from (12) (the quadratic term is negligible at these ),
or, in terms of the mean and the conditional standard deviation of the within-class overlaps of Lemma 1 (here , , and ; the form applies to any dataset once and are measured query by query),
At the crosstalk edge and (to leading order in x and neglecting the self-coupling; at the full expression (15) gives , at , where , it gives 3.7), and the correct class carries only the weight
the archetype is retrieved well before the softmax has selected its class, because the weight lost to the K-1 wrong classes is spread over all of them (middle column of Fig. 3: at , and from (14), 0.86 from the Monte Carlo). Above the crosstalk keeps decaying as and W saturates to one, while the pooling degrades as ; the magnetization is maximal where the two effects balance (Remark 3) and then decreases through the window. The width of the window in is
it opens as (at fixed load subtracts a ), and it is non-empty as long as
i.e. the number of archetypes that can be learnt from M examples of quality r grows as , faster than any power of N but not exponentially in N; (19) is the exponential-model counterpart of the condition of [13].
Remark 3 (optimal temperature). Inside the window, neglecting the self-coupling and to leading order in x, exceeds by two terms: the pooling loss (Proposition 3) and the crosstalk (item (i), ). The first grows as , the second decays exponentially, and at the maximum of they stand in the ratio
pooling loss = ,
(20)
as one finds by differentiating their sum (Appendix C, where the position is given in closed form). The optimum therefore does not remove the crosstalk: it stops when the crosstalk is a fraction of the pooling loss, because the former is exponentially cheap to reduce and the latter is not. The correct class then carries a weight (0.75, 0.50 and 0.39 for the three columns of Fig. 3, against 0.74, 0.49, 0.39 from the full expression (15)), and, within the same approximation, decreases with the load as : the layer generalizes best while it still shares its weight with the wrong classes. The maximum lies inside the window, at for the parameters explored here; Fig. 2 draws it in the plane.
Remark 4 (the load disappears). In (15) the number of classes enters only through W, i.e. through : the crosstalk of archetypes, which is extensive in the pairwise model and produces the spin-glass phase at , becomes exponentially small in above : of order at and of order near the condensation edge. This is the learning counterpart of the exponential storage capacity of [6, 8, 9]: the exponential model can learn from examples a number of archetypes that grows as , Eq. (19), the limitation coming not from the crosstalk
(a) regimes, , ,

(b) one-step magnetization, theory
Figure 2: The three regimes in the plane, from the closed-form theory alone (, , ). (a) Crosstalk (crosstalk term of Corollary 1(i) above ), prototype and memorization () regions; solid and dashed lines are the edges (17); the dotted line is the position of the maximum of , obtained by maximizing (14) numerically and drawn where it lies below the condensation edge (above it crosses it, and the closure is no longer reliable). The crosstalk edge moves with as , the condensation edge as , and the window closes at , Eq. (19); at large the pooling loss is already appreciable at , and the crosstalk edge marks the point where the crosstalk drops below , not a fixed value of . (b) One-step magnetization (14), with the contours , 0.7, 0.9: the prototype regime is where the layer pools the correct class, not where it necessarily retrieves the archetype, since there; in the memorization region (14) overestimates , whose actual value tends to (Remark 2).
but from the condensation edge. The dense p-body networks learning from examples of [14, 15] interpolate between the two cases: there the load is and the crosstalk is still extensive in ; the exponential model is the end of that family, where the polynomial load has become the logarithm in (12).
Remark 5 (overfitting as a REM transition). In the pairwise model, overfitting is the spin-glass phase of a Hebbian spin glass with extensively many quenched patterns, a phase with full replica-symmetry breaking [3, 32, 34]. In the exponential model, overfitting is the condensation of Proposition 3: the Gibbs measure of the attention layer freezes on one training example, as in the low-temperature phase of the REM, which is the limit of the p-spin glass and has one step of replica-symmetry breaking [27, 31]: the overfitting phase of the exponential model is therefore 1-RSB, against the full RSB of the pairwise spin glass. Crosstalk and condensation have different scaling variables, and : this is why the two edges in (17) separate as N grows. We use the REM language for the softmax weights seen from a fixed query, which is where Lemma 1 applies, and do not claim a thermodynamic phase transition of the network at finite M.
Remark 6 (class averages as keys). If the labels are disclosed and the K class averages are used as keys, the within-class condensation cannot occur (M = 1 per key). These keys are not binary, so the factorization of the self-coupling used in the proof of Proposition 4 does not apply: the i-th factor of each weight is a bias of relative size which cancels between numerator and denominator of (5) as soon as one key carries the weight (for K = 1, and for every and q). Neglecting it, the signal-to-noise computation gives with
W being the weight of the correct key: the prototype regime extends to , where and the update is the majority rule of [13], and the layer is a nearest-centroid classifier, i.e. it assigns the query to the class whose prototype has the largest overlap with it. The interesting physics, and the transformer-relevant one, is in the keys = examples.
4 Numerical results
Simulations implement Definitions 1–4 literally: a dataset is drawn, the network is prepared in a fresh example of a randomly chosen archetype and updated in parallel at zero temperature; , , W and are measured after the first update and after convergence (at most ten parallel updates). Each point is an average over independent datasets and queries; error bars are standard errors. Theory curves contain no fitted parameter. Code and data are available at https://github.com/francescoalemanno/hebb-attention.
The load disappears. Figure 4(a) compares, at three dataset entropies, the one-step magnetization of the pairwise network built on class averages, Eq. (9), with that of the attention layer at an inverse temperature inside the window (17), as the load is varied over

Figure 3: One attention step over the training set as a function of the inverse temperature , for , and three datasets of increasing entropy (columns: , , , i.e. , 0.49, 0.45). Top: magnetization of the unseen archetype, theory (14) (line) versus Monte Carlo (full circles), and overlap with the closest training example (open squares); the dotted line is the quality of a single example. Bottom: weight of the correct class and pooled fraction , theory (12), (13) (lines) versus Monte Carlo (symbols). The grey band is the analytic window (17). The three regimes of Corollary 1 are visible from left to right: crosstalk ( ), prototype (crosstalk below , ), memorization ( , , ).
almost two decades. The pairwise curves bend down with , as in [13]: at the magnetization drops below 0.9 at and reaches 0.65 at , at it is below 0.9 already at and reaches 0.38, at it reaches 0.24. The attention curves are flat over the whole range, at 1.0, 0.90 and 0.75 respectively, which are their values, i.e. the majority-rule values lowered by the partial pooling at this . The flatness is more robust than the saturation of : at the correct class carries only of the weight at and at , yet does not move, because the weight lost to the wrong classes is spread over all of them and its contribution to the noise, the term in (15), stays well below (0.04 at , ); in the pairwise model the same crosstalk is of order . The dependence on the load returns only when the crosstalk term of Corollary 1(i),
becomes comparable with , which is the crosstalk edge of (17): there . Panel (b) shows the dependence on at the fixed load : the attention layer stays above the pairwise network at every , and both follow their one-step theories. The triangles are the fixed point of the iterated attention map: for the archetype itself, which is not one of the keys, is an attractor of the softmax dynamics and is retrieved exactly; for larger the fixed point is a training example ( , , the dotted line). Iterating a transformer layer memorizes unless the examples are good enough for the pooled majority vote to be self-consistent; a single application generalizes over a wider range. This is a departure from the attractor paradigm of associative memory: when the keys are examples rather than archetypes, the fixed points of the map are the examples, and the archetype is reached by stopping after one step rather than by converging. It is also, we note, how attention is used in practice, where each layer is applied once in a feed-forward pass rather than iterated to a fixed point (iterated variants exist [11]); the present setting offers one reason why the single application is not a loss.
Temperature scan. Figure 1 shows what the three regimes of Corollary 1 look like on the weights of one
(a) one step at fixed ,

(b) fixed load

Figure 4: The load disappears. Retrieval of an unseen archetype from a fresh test example , , , ; lines are theory, symbols Monte Carlo (3 samples 6 queries, standard errors). (a) One-step magnetization against the load at three dataset entropies: the pairwise network, Eq. (9) (dashed lines, open squares), degrades as grows, the attention layer, Eqs. (14)–(15) (solid lines, full circles), does not. (b) Magnetization against at : pairwise (open squares), attention after one step (full circles) and at the fixed point of the iterated map (triangles); the dotted line is the quality of a single example. The iterated map retrieves the archetype only for and otherwise converges to a training example.
query; Fig. 3 follows them quantitatively as is varied. The magnetization is a non-monotonic function of , with its maximum inside the window (17), at the of Remark 3 (1.4 for the middle column), where the crosstalk has become negligible while is still close to M; the maximum is close to the majority-rule value (0.998, 0.975 and 0.84 for the three columns; measured 1.00, 0.92, 0.77). Through the window the pooling degrades as and decreases; beyond the condensation edge rises to one and settles at about : the layer has become a lookup table of its training set. The closed-form theory reproduces and quantitatively at all (within 0.04), and while the pooling is substantial (within 0.03 for ); in the condensed regime it overestimates by up to 0.1, because the Gaussian approximation no longer holds when a few keys carry the weight (Remark 2); the measured plateaus, 0.67, 0.53 and 0.48 for , 0.49, 0.45 (left to right), lie slightly above , which is the selection bias of the closest example.
Scaling of the two edges. The two edges of the window have different scaling variables, and Fig. 5 isolates them: plotted against the rising edges of the curves for to 4000 collapse, plotted against the falling edges collapse. The window opens as , as predicted by (17); at it is a factor of four wide, at a factor of ten.
The condensation is a REM transition. Figure 6 tests Proposition 3 directly. Simulations at different sizes, dataset sizes and qualities, once plotted against , fall on the REM participation ratio with no adjustable parameter, including the series whose spread differs from the others by a factor of two. The departure of from sets in at , as the small- expansion of Proposition 3 says, and the condensation is essentially complete beyond , where is 0.2–0.3 at these values of . The memorization edge of an attention layer is therefore not a property of the particular dataset but a universal feature of a softmax over exchangeable keys: depends on the data only through the conditional spread of the within-class overlaps.
The attention entropy. Figure 7 summarizes the content of Proposition 4: when is evaluated from the softmax weights actually produced by each query, the one-step magnetization of every simulation, whatever , , and , lies on the single curve . Points in the condensed regime (light colours) lie below the curve, by up to 0.1, where the Gaussian approximation fails (Remark 2). The pairwise network does not collapse against , because the load is a second, independent source of noise there.
5 Structured data
The theory of Sec. 3 rests on Lemma 1: seen from the query, the within-class overlaps are independent Gaussian variables, so that the softmax is a REM. On real data the archetype is not available and the examples of a class are not independent blurred copies of one vector; in [13] they were shown to be organized in a quasi-ultrametric way, with an overlap distribution reminiscent of replica-symmetry breaking, more markedly in fashion-MNIST than in MNIST. We therefore ask which parts of the picture survive on MNIST and fashion-MNIST (F-MNIST), binarised as in [13] by thresholding each pixel at its mean over the training set, with
(a) , ,

(b) crosstalk edge:

(c) condensation edge:

Figure 5: Size dependence of the generalization window at , , (lines: theory; symbols: Monte Carlo, 3 samples 8 queries). (a) Against the window widens with . (b) Against the rising (crosstalk) edges collapse, up to the slow drift of Eq. (17). (c) Against the falling (condensation) edges collapse.

Figure 6: Condensation of the within-class softmax. The pooled fraction measured in simulations with different N, M and r (symbols; unless stated) collapses, as a function of the single variable , on the participation ratio of a random energy model with M levels (lines, Proposition 3); dotted verticals mark the REM freezing point .
the examples themselves as keys and the test images as queries. The ingredients of the theory that can be measured on the data are the mean and the standard deviation of the overlaps between a query and the examples of its class, which enter the window in the form (18), with (0.025 on MNIST, 0.20 on F-MNIST at ), and the condensation variable as . Figure 8 collects the results for examples per class; and 64 give the same picture with lower accuracies.
Condensation. Panel (a) repeats the test of Fig. 6 with . On MNIST the pooled fraction follows with no adjustable parameter, within 0.1 around and within 0.02 beyond , as on Rademacher
(attention); bare for the pairwise crosses
Figure 7: All Monte Carlo points of Figs. 3 and 5 and of the grid at described in Appendix A, for the attention layer (every , , , ; colour: pooled fraction ), plotted against the attention entropy evaluated from the measured weights, Eq. (15); the line is . Crosses: the pairwise network on the same grid, against the bare dataset entropy , which does not account for the crosstalk.
data. On F-MNIST it condenses later and keeps a tail: at and 0.023 at , where the REM gives 0.014 and 0.006. Panel (b) shows why: the standardised within-class overlaps of MNIST are Gaussian (skewness -0.16, excess kurtosis 0.0), those of F-MNIST are not (skewness -0.7, excess kurtosis 1.2), and in particular their upper tail is compressed, with against 0.023 for a Gaussian and a gap between the two largest overlaps of against the expected for 256 Gaussian levels. A query of F-MNIST has many nearly equivalent closest examples, and a softmax over nearly degenerate levels cannot freeze on one of them: this is the signature of sub-clusters within the class, i.e. of the hierarchical organization found in [13], and it is where the REM should be replaced by its hierarchical generalization [28]. The
(a) condensation of the softmax

(b) within-class overlaps
(c) test accuracy, M = 256
Figure 8: One attention step with keys = binarised MNIST and fashion-MNIST (F-MNIST) examples, M = 256 per class, K = 10, N = 784, queries from the test sets (3 samples × 400 queries). (a) Pooled fraction against , being the measured standard deviation of the within-class overlaps; line: of Proposition 3; dotted vertical: . (b) Distribution of the standardised within-class overlaps against the Gaussian of Lemma 1. (c) Test accuracy of the one-step readout (class of largest total weight) against ; dashed: the limit, which returns the closest training example; dotted: the readout with the prototypes as keys (Remark 6); shaded: the window (18) with the measured and (and ); stars: the measured best accuracy.
diagnostic of Proposition 3 thus not only measures memorization but also detects structure.
Accuracy. Panel (c) shows the three regimes on the classification accuracy of the one-step readout. At small the accuracy falls to 0.6 on both datasets, below the prototype readout: this is the crosstalk regime. It then rises through the window (18) and reaches its maximum beyond the condensation edge, at on MNIST (0.90, with ) and on F-MNIST (0.80, with ), far above the prototype readout (0.71 and 0.62). On MNIST the accuracy then stays at the value of the closest-example limit; on F-MNIST it is non-monotonic and its maximum exceeds that limit by 0.03. The overlap of the output state with the class prototype behaves instead as on Rademacher data: it is maximal inside the window, at on MNIST (0.84) and on F-MNIST (0.70), and decreases on condensation. The two maxima are separated because on structured data the class prototype is a poor target: it is the replicasymmetric representation of the class, which in [13] gave 75% on MNIST against 95% once a hidden layer of sub-prototypes was added. Here the same gain is obtained without a hidden layer, by the temperature: at a few times the softmax pools a few examples around the query, i.e. it reads out a local sub-prototype rather than the class average, and the memorization regime of the Rademacher theory becomes the useful operating point. In the language of Corollary 1, the optimal is not M but the size of the sub-cluster to which the query belongs; on data with a single archetype per class that size is M, on hierarchical data it is of order one.
6 Gradient descent versus Hebb
The Hebbian attention of the previous sections has no trainable parameter besides . It is natural to ask whether an attention layer whose keys and values are learned by gradient descent on the same data does better, and whether the memorization phenomenology survives. We trained a one-head attention classifier,
with learnable keys (the rows of are the slots of the layer, the analogue of the stored keys of the Hebbian network), values mapping slots to classes, and learnable inverse temperature
(a)

(b)

(c)
Figure 9: One-head attention classifier ( learned keys and values, softmax inverse temperature learned, cross-entropy, Adam) trained on the datasets of Definition 1 with , . (a) Test accuracy on fresh examples after 500 steps (dashed, symbols) against the Hebbian attention with keys = training examples at its best (solid), which coincides with the nearest-centroid readout, as a function of for . (b) Test accuracy along training without weight decay, for four datasets; dotted lines are the corresponding Hebbian values. (c) Usage-weighted maximal cosine of the learned slots with the archetypes (full symbols) and with the training examples (open symbols), as defined in the text.
(the same symbol as above, with the same meaning, now a trained parameter), by minimizing the cross-entropy on the KM labelled examples. To characterize what the slots have learnt we measure, for each slot, the largest cosine between the slot and an archetype and the largest cosine between the slot and a training example, and we average these over the slots with weights given by the mean attention each slot receives on the test queries (usage-weighted cosines): a slot aligned with an archetype is a prototype, a slot aligned with an example is a memory of that example. Figure 9 collects the results. The trained layer always fits the training set, but its test accuracy on fresh examples lies systematically below that of the Hebbian attention at its best , which on these data coincides with the nearest-centroid readout (Remark 6) and serves as the reference (it is not claimed to be optimal). The gap is not a function of alone: at fixed it grows with M, i.e. it is governed by the noise r of the individual examples; we interpret this as the keys fitting the noise directions of the examples they attend to, which are the more numerous the smaller r. The training curves (panel b) show the classical peak-and-decay: the test accuracy reaches the Hebbian value after a few tens of steps, when the keys are proportional to the class sums of the examples (the gradient of the cross-entropy at initialization is Hebbian), and then decays while the keys fit the noise. A weight decay of order on the keys stops the decay, brings the test accuracy back close to the Hebbian reference (within 0.01–0.03 at r = 0.3, within 0.03–0.08 at r = 0.2), and turns the learned keys into prototypes, their cosine with the archetypes rising from 0.4 to 0.7 (panel c). These observations refer to this setup, full-batch Adam with a single learning-rate schedule and two datasets per point, and are not a statement about optimally tuned training. In the language of [13], gradient descent discovers by itself the averaged, “replica-symmetric” representation of each class, and overfits by moving away from it.
7 Conclusions and outlook
We have transferred Hebbian learning from examples [12, 13] from the pairwise Hopfield network to the exponential one, whose zero-temperature update is a layer of softmax attention with the training examples as keys and values. The signal-to-noise analysis closes in this setting, and yields a one-step magnetization that has the same majority-rule form as in the pairwise network but with the dataset entropy replaced by an attention entropy, Eq. (15), built from two analytically computable statistics of the softmax: the weight of the correct class and the number of examples it pools. The implications are of two kinds.
For Hopfield networks. (i) The load is no longer a control parameter of learning: within the window (17) the crosstalk of the other archetypes decays exponentially in instead of being of order , and the exponential network learns from examples a number of archetypes growing as , Eq. (19), limited by the condensation edge rather than by the crosstalk; the dense networks of [14, 15], with load , are the intermediate steps of this family. (ii) Overfitting changes nature. In the pairwise model it is a Hebbian spin glass with full replica-symmetry breaking; in the exponential model it is the condensation of the Gibbs measure over the keys onto one example, a random-energy transition with one step of replica-symmetry breaking at , whose location depends on the data only through the spread of the within-class overlaps. The exponential model is, in this sense, the least glassy member of the Hebbian family, as the REM is of the p-spin family. (iii) The dataset entropy generalizes to an entropy of the weighted vote: pairwise and exponential networks are the two ends of one formula, with and extensive crosstalk on one side, and exponentially small crosstalk on the other. The labels are not needed: inside the window the examples of the correct class receive the weight by themselves ( ), and the attention layer, which never sees a label, performs the same majority vote as the pairwise network built on class averages at zero load, Eq. (9) with , up to the pooling loss (Fig. 4). (iv) Iteration is not free. With keys equal to examples the fixed points of the dynamics are the examples, and the archetype is an attractor only for small ; the retrieval of an unseen archetype is a one-step operation, not a convergence.
For attention layers. (i) An attention head over a set of noisy examples has a computable phase diagram in the parameters , and the softmax temperature is the axis along which it moves from crosstalk to prototype to memorization; the two edges of the generalization window scale as and , so that a larger model generalizes from the same context over a wider range of temperatures. (ii) Memorization is a measurable property of a head whose keys are training examples: the participation ratio of its weights, which drops to order one at the REM threshold, and whose departure from the REM curve measures the structure of the data (Sec. 5). Extending the diagnostic to heads with learned projections, where the logits are no longer the overlaps of Lemma 1, requires identifying the analogue of x and is left open. (iii) In our experiments the first steps of gradient descent on the cross-entropy are Hebbian, the later ones fit the noise of the examples, and weight decay returns the keys close to the Hebbian prototypes; this suggests a reading of early stopping and weight decay in attention layers as a distance from the Hebbian solution, to be tested beyond the present setup. (iv) In the present setting the single application of a layer generalizes over a wider range than its fixed-point iteration, which converges to a training example unless is small; this is consistent with the feed-forward use of attention, although we do not claim it explains it. (v) On structured data the memorization regime is not a failure but the operating point: the classes of MNIST and F-MNIST are made of sub-clusters, the class prototype is a poor target, and the softmax at a few times the condensation edge reads out the local sub-prototype, doing with the temperature what the hidden layer of [13] did by construction; the departure of the condensation curve from the REM measures how far the data are from the single-archetype picture.
These implications are structural rather than numerical: the data are unstructured, the layer is single, the dynamics is noiseless and the signal-to-noise theory is Gaussian, hence quantitative only while the softmax pools many examples (within 0.03) and off by up to 0.1 once it has condensed. Several directions follow. The natural tool to close the latter gap is the REM-like treatment of exponential models of [8], or the large-deviation evaluation of the noise of [10], with examples in place of archetypes; either would also give the selection bias of the closest example. The latter work, at , finds generalization to unseen cues well below memorization: in the present picture that is the memorization regime, and the window of Corollary 1 is where the two capabilities meet. The iterated dynamics, in which the archetype emerges as a fixed point of the softmax map for small (Fig. 4b), deserves an analytic condition. The finite-temperature equilibrium statistics, which in the pairwise model gives the first-order retrieval transition of [13], is the obvious next step, as is the theory of structured datasets, where Sec. 5 indicates the route: a hierarchical random energy model [28] for the within-class overlaps, whose levels of condensation should correspond to the levels of the ultrametric organization uncovered in [13]. Finally, the diagnostic of point (ii) above and the Hebbian reading of regularization in point (iii) are the two statements that could be confronted with trained transformers, once the mapping from learned projections to the variables of Lemma 1 is understood.
Acknowledgments
This work was supported by SolarSud T.R.E. di Miccoli Antonia (https://solarsud.it). The research on this topic was accelerated by the use of Claude Fable 5.1 (Anthropic); the author takes full responsibility for the content. Code, data and the Monte Carlo results reproducing all figures are available at https://github.com/francescoalemanno/hebb-attention.
References
[1] J. J. Hopfield, Proc. Natl. Acad. Sci. USA 79, 2554 (1982).
[2] D. J. Amit, H. Gutfreund, H. Sompolinsky, Phys. Rev. Lett. 55, 1530 (1985).
[3] D. J. Amit, H. Gutfreund, H. Sompolinsky, Ann. Phys. 173, 30 (1987).
[4] D. Krotov, J. J. Hopfield, Adv. Neural Inf. Process. Syst. 29, 1172 (2016).
[5] D. Krotov, J. J. Hopfield, Neural Comput. 30, 3151 (2018).
[6] M. Demircigil, J. Heusel, M. Löwe, S. Upgang, F. Vermet, J. Stat. Phys. 168, 288 (2017).
[7] H. Ramsauer et al., Hopfield networks is all you need, Int. Conf. Learn. Repr. (2021).
[8] C. Lucibello, M. Mézard, Phys. Rev. Lett. 132, 077301 (2024).
[9] L. Albanese, A. Alessandrelli, A. Barra, P. Sollich, Yet another exponential Hopfield model, arXiv:2509.06905 (2025).
[10] E. Agliari, A. Barra, A. Ladiana, A. Lepre, Exponential capacity in multilayer hetero-associative neural networks, arXiv:2607.29554 (2026).
[11] B. Hoover et al., Energy transformers, Adv. Neural Inf. Process. Syst. 36 (2023).
[12] E. Agliari, F. Alemanno, A. Barra, G. De Marzo, Neural Netw. 148, 232 (2022); arXiv:2109.00454.
[13] F. Alemanno, M. Aquaro, I. Kanter, A. Barra, E. Agliari, Europhys. Lett. 141, 11001 (2023); arXiv:2203.01304.
[14] E. Agliari, L. Albanese, F. Alemanno, A. Alessandrelli, A. Barra, F. Giannotti, D. Lotito, D. Pedreschi, Physica A 626, 129076 (2023).
[15] E. Agliari, L. Albanese, F. Alemanno, A. Alessandrelli, A. Barra, F. Giannotti, D. Lotito, D. Pedreschi, Physica A 627, 129143 (2023).
[16] L. Albanese, A. Barra, P. Bianco, F. Durante, D. Pallara, J. Math. Phys. 65, 113302 (2024).
[17] E. Agliari, F. Alemanno, M. Aquaro, A. Barra, F. Durante, I. Kanter, Neural Netw. 173, 106174 (2024).
[18] J. F. Fontanari, J. Phys. (France) 51, 2421 (1990).
[19] R. Thériault, D. Tantari, SciPost Phys. 17, 040 (2024).
[20] M. Negri, C. Lauditi, G. Perugini, C. Lucibello, E. Malatesta, Phys. Rev. Lett. 131, 257301 (2023).
[21] Y.-H. H. Tsai, S. Bai, M. Yamada, L.-P. Morency, R. Salakhutdinov, Transformer dissection: a unified understanding of transformer's attention via the lens of kernel, Proc. EMNLP (2019).
[22] T. Bricken, C. Pehlevan, Attention approximates sparse distributed memory, Adv. Neural Inf. Process. Syst. 34 (2021).
[23] B. Millidge, T. Salvatori, Y. Song, T. Lukasiewicz, R. Bogacz, Universal Hopfield networks: a general framework for single-shot associative memory models, Proc. ICML (2022).
[24] S. Zhai et al., Stabilizing transformer training by preventing attention entropy collapse, Proc. ICML (2023).
[25] A. Cowsik, A. Sriram, Hierarchical prototype emergence in modern Hopfield models, arXiv:2609.12079 (2026).
[26] B. Derrida, Phys. Rev. Lett. 45, 79 (1980).
[27] B. Derrida, Phys. Rev. B 24, 2613 (1981).
[28] B. Derrida, J. Phys. Lett. (Paris) 46, L401 (1985); B. Derrida, E. Gardner, J. Phys. C 19, 2253 (1986).
[29] B. Derrida, G. Toulouse, J. Phys. Lett. (Paris) 46, L223 (1985).
[30] M. Mézard, G. Parisi, M. A. Virasoro, J. Phys. Lett. (Paris) 46, L217 (1985).
[31] D. J. Gross, M. Mézard, Nucl. Phys. B 240, 431 (1984).
[32] M. Mézard, G. Parisi, M. A. Virasoro, Spin Glass Theory and Beyond (World Scientific, Singapore, 1987).
[33] A. C. C. Coolen, R. Kühn, P. Sollich, Theory of Neural Information Processing Systems (Oxford University Press, 2005).
[34] L. Albanese, F. Alemanno, A. Alessandrelli, A. Barra, J. Stat. Phys. 189, 24 (2022).
A Details of the simulations
Datasets are generated as in Definition 1 with . For the pairwise networks the field is with the couplings of Definition 2; for the exponential network it is Eq. (5), evaluated in log-space for numerical stability. A query is a fresh example of a randomly chosen archetype; the parallel update is applied once for , , , , and until convergence (at most ten parallel updates) for . Fig. 4: , , ; (a) 9 loads at , (b) 15 entropies at ; 3 datasets queries per point. Fig. 7 also uses a grid at , : 13 loads entropies , 4 datasets queries per point. Fig. 1: one dataset and one query, , , , ; the three values of are fixed by the edges (17). Fig. 2: theory only, , , , grid in . Fig. 3: , , , (0.2, 16), (0.5, 8), 28 values of , samples per point. Fig. 5: , , samples. Fig. 6: , , , , samples; the abscissa is computed from as . The REM function is evaluated by averaging over 4000 realizations of Gaussian levels. Fig. 8: MNIST and F-MNIST binarised by thresholding each pixel at its mean over the 60,000 training images; for each of 3 samples, examples per class are drawn from the training set and 400 queries from the test set; 25 values of ; , , skewness and kurtosis are computed query by query over the examples of the query's class, then averaged. Fig. 9: , , , 500 full-batch Adam steps, 400 fresh test examples, 2 datasets per point; the Hebbian attention is evaluated on the same data by one step of (5), predicting the class of largest total weight, maximized over .
B Small-x expansion of
Let and . Expanding in the numerator and in the denominator of the participation ratio gives, realization by realization,
the term is a cubic polynomial in the 's and averages to zero. Taking the average with gives , i.e. ( numerically, 0.916 from the expansion). For and , in the frozen phase [29, 30], and Jensen's inequality gives ; the inequality is strict because keeps a non-degenerate distribution. At the bound is 1.31, 1.40, 1.50 for , 64, 256, while samples of Gaussian levels give , 0.77, 0.72 (above , 0.71, 0.67, a finite- effect), hence , 1.30, 1.39, and , 1.50, 1.65, i.e. 11–19% above the bound. In the text is always evaluated numerically.
C Position of the maximum
Write , , , and ; then (15) reads
with from (12), so that with , and from (13). The condition is
(21)
a scalar equation in , exact within the signal-to-noise closure, that can be solved once is known. For , with ; neglecting the self-coupling ( ), the quadratic term in the exponent of and against , (21) reduces to , i.e.
which for is and whose solution is
with the Lambert function, . Equation (20) is the same condition written for the two terms of , since ; with inside the square root,
the load dependence quoted in Remark 3. The expansion assumes , i.e. a maximum well below the condensation edge, and is not uniform for , where : at , , the closed form is within 2% of the numerical maximum of (14) for ( ), 4% off at , 12% at and 19% at , where (Fig. 2). The self-coupling shifts the maximum only slightly: minimizing the full expression (15) at fixed load , , gives , 0.43, 0.35, 0.32 for , , , , against 0.50, 0.46, 0.40, 0.37 from (23), with within 2%. Asymptotically the term in st of (21) takes over and decreases as rather than , but at any practical size (23) is accurate.