# Modern Hopfield network

Modern Hopfield networks (also known as Dense Associative Memories) are generalizations of the classical Hopfield networks that break the linear scaling relationship between the number of input features and the number of stored memories. This is achieved by introducing stronger non-linearities (either in the energy function or neurons’ activation functions) leading to super-linear (even an exponential) memory storage capacity as a function of the number of feature neurons. The network still requires a sufficient number of hidden neurons.
The key theoretical idea behind the modern Hopfield networks is to use an energy function and an update rule that is more sharply peaked around the stored memories in the space of neuron’s configurations compared to the classical Hopfield network.

## Classical Hopfield networks

Hopfield networks are recurrent neural networks with dynamical trajectories converging to fixed point attractor states and described by an energy function. The state of each model neuron i{\textstyle i} is defined by a time-dependent variable $V_{i}$, which can be chosen to be either discrete or continuous. A complete model describes the mathematics of how the future state of activity of each neuron depends on the known present or previous activity of all the neurons.
In the original Hopfield model of associative memory, the variables were binary, and the dynamics were described by a one-at-a-time update of the state of the neurons. An energy function quadratic in the $V_{i}$ was defined, and the dynamics consisted of changing the activity of each single neuron $i$ only if doing so would lower the total energy of the system. This same idea was extended to the case of $V_{i}$ being a continuous variable representing the output of neuron $i$, and $V_{i}$ being a monotonic function of an input current. The dynamics became expressed as a set of first-order differential equations for which the "energy" of the system always decreased. The energy in the continuous case has one term which is quadratic in the $V_{i}$ (as in the binary model), and a second term which depends on the gain function (neuron's activation function). While having many desirable properties of associative memory, both of these classical systems suffer from a small memory storage capacity, which scales linearly with the number of input features.

## Discrete variables

A simple example of the Modern Hopfield network can be written in terms of binary variables $V_{i}$ that represent the active $V_{i}=+1$ and inactive $V_{i}=-1$ state of the model neuron $i$.$E=-\sum \limits _{\mu =1}^{N_{\text{mem}}}F{\Big\sum \limits _{i=1}^{N_{f}}\xi _{\mu i}V_{i}{\Big )}$In this formula the weights ξ$_{μi}${\textstyle \xi _{\mu i}} represent the matrix of memory vectors (index $\mu =1...N_{\text{mem}}$ enumerates different memories, and index $i=1...N_{f}$ enumerates the content of each memory corresponding to the $i$-th feature neuron), and the function $F(x)$ is a rapidly growing non-linear function. The update rule for individual neurons (in the asynchronous case) can be written in the following form $V_{i}^{(t+1)}=\operatorname {sign} {\bigg\sum \limits _{\mu =1}^{N_{\text{mem}}}{\biggF{\Big\xi _{\mu i}+\sum \limits _{j\neq i}\xi _{\mu j}V_{j}^{(t)}{\Big )}-F{\Big-\xi _{\mu i}+\sum \limits _{j\neq i}\xi _{\mu j}V_{j}^{(t)}{\Big )}{\bigg )}{\bigg ]}$which states that in order to calculate the updated state of the i{\textstyle i}-th neuron the network compares two energies: the energy of the network with the $i$-th neuron in the ON state and the energy of the network with the $i$-th neuron in the OFF state, given the states of the remaining neuron. The updated state of the $i$-th neuron selects the state that has the lowest of the two energies.
In the limiting case when the non-linear energy function is quadratic $F(x)=x^{2}$ these equations reduce to the familiar energy function and the update rule for the classical binary Hopfield network.
The memory storage capacity of these networks can be calculated for random binary patterns. For the power energy function $F(x)=x^{n}$ the maximal number of memories that can be stored and retrieved from this network without errors is given by$N_{\text{mem}}^{\max }\approx {\frac {1}{2(2n-3)!!}}{\frac {N_{f}^{n-1}}{\ln(N_{f})}}$For an exponential energy function F(x)=e$^x${\textstyle F(x)=e^{x}} the memory storage capacity is exponential in the number of feature neurons$N_{\text{mem}}^{\max }\approx 2^{N_{f}/2}$

## Continuous variables

Modern Hopfield networks or Dense Associative Memories can be best understood in continuous variables and continuous time. Consider the network architecture, shown in Fig.1, and the equations for the neurons' state evolution
| ${\begin{cases}\tau _{f}{\frac {dx_{i}}{dt}}=\sum \limits _{\mu =1}^{N_{h}}\xi _{i\mu }f_{\mu }-x_{i}+I_{i}\\\tau _{h}{\frac {dh_{\mu }}{dt}}=\sum \limits _{i=1}^{N_{f}}\xi _{\mu i}g_{i}-h_{\mu }\end{cases}}$ | (1) |
where the currents of the feature neurons are denoted by x$_i${\textstyle x_{i}}, and the currents of the memory neurons are denoted by $h_{\mu }$ ($h$ stands for hidden neurons). There are no synaptic connections among the feature neurons or the memory neurons. A matrix $\xi _{\mu i}$ denotes the strength of synapses from a feature neuron $i$ to the memory neuron $\mu$. The synapses are assumed to be symmetric, so that the same value characterizes a different physical synapse from the memory neuron $\mu$ to the feature neuron $i$. The outputs of the memory neurons and the feature neurons are denoted by $f_{\mu }$ and $g_{i}$, which are non-linear functions of the corresponding currents. In general these outputs can depend on the currents of all the neurons in that layer so that $f_{\mu }=f(\{h_{\mu }\})$ and g$_i$=g({x$_i$}){\textstyle g_{i}=g(\{x_{i}\})}. It is convenient to define these activation function as derivatives of the Lagrangian functions for the two groups of neurons
| $f_{\mu }={\frac {\partial L_{h}}{\partial h_{\mu }}},\ \ \ \ {\text{and}}\ \ \ \ g_{i}={\frac {\partial L_{x}}{\partial x_{i}}}$ | (2) |
This way the specific form of the equations for neuron's states is completely defined once the Lagrangian functions are specified. Finally, the time constants for the two groups of neurons are denoted by $\tau _{f}$ and $\tau _{h}$, $I_{i}$ is the input current to the network that can be driven by the presented data.
General systems of non-linear differential equations can have many complicated behaviors that can depend on the choice of the non-linearities and the initial conditions. For Hopfield networks, however, this is not the case - the dynamical trajectories always converge to a fixed point attractor state. This property is achieved because these equations are specifically engineered so that they have an underlying energy function
| $E(t)={\Big\sum \limits _{i=1}^{N_{f}}(x_{i}-I_{i})g_{i}-L_{x}{\Big ]}+{\Big\sum \limits _{\mu =1}^{N_{h}}h_{\mu }f_{\mu }-L_{h}{\Big ]}-\sum \limits _{\mu ,i}f_{\mu }\xi _{\mu i}g_{i}$ | (3) |
The terms grouped into square brackets represent a Legendre transform of the Lagrangian function with respect to the states of the neurons. If the Hessian matrices of the Lagrangian functions are positive semi-definite, the energy function is guaranteed to decrease on the dynamical trajectory
| ${\frac {dE(t)}{dt}}=-\tau _{f}\sum \limits _{i,j=1}^{N_{f}}{\frac {dx_{i}}{dt}}{\frac {\partial ^{2}L_{x}}{\partial x_{i}\partial x_{j}}}{\frac {dx_{j}}{dt}}-\tau _{h}\sum \limits _{\mu ,\nu =1}^{N_{h}}{\frac {dh_{\mu }}{dt}}{\frac {\partial ^{2}L_{h}}{\partial h_{\mu }\partial h_{\nu }}}{\frac {dh_{\nu }}{dt}}\leq 0$ | (4) |
This property makes it possible to prove that the system of dynamical equations describing temporal evolution of neurons' activities will eventually reach a fixed point attractor state.
In certain situations one can assume that the dynamics of hidden neurons equilibrates at a much faster time scale compared to the feature neurons, τ$_h$≪τ$_f${\textstyle \tau _{h}\ll \tau _{f}}. In this case the steady state solution of the second equation in the system (1) can be used to express the currents of the hidden units through the outputs of the feature neurons. This makes it possible to reduce the general theory (1) to an effective theory for feature neurons only. The resulting effective update rules and the energies for various common choices of the Lagrangian functions are shown in Fig.2. In the case of log-sum-exponential Lagrangian function the update rule (if applied once) for the states of the feature neurons is the attention mechanism commonly used in many modern AI systems (see Ref. for the derivation of this result from the continuous time formulation).

## Relationship to classical Hopfield network with continuous variables

Classical formulation of continuous Hopfield networks can be understood as a special limiting case of the Modern Hopfield networks with one hidden layer. Continuous Hopfield Networks for neurons with graded response are typically described by the dynamical equations
| $\tau _{f}{\frac {dx_{i}}{dt}}=\sum \limits _{j=1}^{N_{f}}T_{ij}V_{j}-x_{i}+I_{i}$ | (5) |
and the energy function
| $E=-{\frac {1}{2}}\sum \limits _{i,j=1}^{N_{f}}T_{ij}V_{i}V_{j}-\sum \limits _{i=1}^{N_{f}}V_{i}I_{i}+\sum \limits _{i=1}^{N_{f}}\int \limits ^{V_{i}}g^{-1}(z)\,dz$ | (6) |
where V$_i$=g(x$_i$){\textstyle V_{i}=g(x_{i})}, and $g^{-1}(z)$ is the inverse of the activation function $g(x)$. This model is a special limit of the class of models that is called models A, with the following choice of the Lagrangian functions
| $L_{v}=\sum \limits _{i=1}^{N_{f}}\int \limits ^{x_{i}}g(x)\,dx,\ \ \ \ \ {\text{and}}\ \ \ \ \ L_{h}={\frac {1}{2}}\sum \limits _{\mu =1}^{N_{h}}h_{\mu }^{2}$ | (7) |
that, according to the definition (2), leads to the activation functions
| $V_{i}=g(x_{i}),\ \ \ \ \ {\text{and}}\ \ \ \ \ f_{\mu }=h_{\mu }$ | (8) |
If we integrate out the hidden neurons the system of equations (1) reduces to the equations on the feature neurons (5) with $T_{ij}=\sum \limits _{\mu =1}^{N_{h}}\xi _{\mu i}\xi _{\mu j}$, and the general expression for the energy (3) reduces to the effective energy
| $E=-{\frac {1}{2}}\sum \limits _{i,j=1}^{N_{f}}T_{ij}V_{i}V_{j}-\sum \limits _{i=1}^{N_{f}}V_{i}I_{i}+\sum \limits _{i=1}^{N_{f}}{\Bigx_{i}V_{i}-\int \limits ^{x_{i}}g(x)\,dx{\Big )}$ | (9) |
While the first two terms in equation (6) are the same as those in equation (9), the third terms look superficially different. In equation (9) it is a Legendre transform of the Lagrangian for the feature neurons, while in (6) the third term is an integral of the inverse activation function. Nevertheless, these two expressions are in fact equivalent, since the derivatives of a function and its Legendre transform are inverse functions of each other. The easiest way to see that these two terms are equal explicitly is to differentiate each one with respect to $x_{i}$. The results of these differentiations for both expressions are equal to $x_{i}g(x_{i})'$. Thus, the two expressions are equal up to an additive constant. This completes the proof that the classical Hopfield network with continuous states is a special limiting case of the modern Hopfield network (1) with energy (3).

## General formulation

Biological neural networks have a large degree of heterogeneity in terms of different cell types. This section describes a mathematical model of a fully connected Modern Hopfield network assuming the extreme degree of heterogeneity: every single neuron is different. Specifically, an energy function and the corresponding dynamical equations are described assuming that each neuron has its own activation function and kinetic time scale. The network is assumed to be fully connected, so that every neuron is connected to every other neuron using a symmetric matrix of weights $W_{IJ}$, indices $I$ and $J$ enumerate different neurons in the network, see Fig.3. The easiest way to mathematically formulate this problem is to define the architecture through a Lagrangian function $L(\{x_{I}\})$ that depends on the activities of all the neurons in the network. The activation function for each neuron is defined as a partial derivative of the Lagrangian with respect to that neuron's activity
| $g_{I}={\frac {\partial L}{\partial x_{I}}}$ | (10) |
From the biological perspective one can think about $g_{I}$ as an axonal output of the neuron $I$. In the simplest case, when the Lagrangian is additive for different neurons, this definition results in the activation that is a non-linear function of that neuron's activity. For non-additive Lagrangians this activation function can depend on the activities of a group of neurons. For instance, it can contain contrastive (softmax) or divisive normalization. The dynamical equations describing temporal evolution of a given neuron are given by
| $\tau _{I}{\frac {dx_{I}}{dt}}=\sum \limits _{J=1}^{N}W_{IJ}g_{J}-x_{I}$ | (11) |
This equation belongs to the class of models called firing rate models in neuroscience. Each neuron $I$ collects the axonal outputs $g_{J}$ from all the neurons, weights them with the synaptic coefficients $W_{IJ}$ and produces its own time-dependent activity $x_{I}$. The temporal evolution has a time constant $\tau _{I}$, which in general can be different for every neuron. This network has a global energy function
| $E=\sum \limits _{I=1}^{N}x_{I}g_{I}-L-{\frac {1}{2}}\sum \limits _{I,J=1}^{N}g_{I}W_{IJ}g_{J}$ | (12) |
where the first two terms represent the Legendre transform of the Lagrangian function with respect to the neurons' currents $x_{I}$. The temporal derivative of this energy function can be computed on the dynamical trajectories leading to (see for details)
| ${\frac {dE}{dt}}=-\sum \limits _{I,K=1}^{N}{\frac {dx_{I}}{dt}}M_{IK}{\frac {dx_{K}}{dt}}\leq 0,\ \ \ \ {\text{where}}\ \ \ \ M_{IK}=\tau _{I}{\frac {\partial ^{2}L}{\partial x_{I}\partial x_{K}}}$ | (13) |
The last inequality sign holds provided that the matrix $M_{IK}$ (or its symmetric part) is positive semi-definite. If, in addition to this, the energy function is bounded from below the non-linear dynamical equations are guaranteed to converge to a fixed point attractor state. The advantage of formulating this network in terms of the Lagrangian functions is that it makes it possible to easily experiment with different choices of the activation functions and different architectural arrangements of neurons. For all those flexible choices the conditions of convergence are determined by the properties of the matrix $M_{IJ}$ and the existence of the lower bound on the energy function.

## Hierarchical associative memory network

The neurons can be organized in layers so that every neuron in a given layer has the same activation function and the same dynamic time scale. If we assume that there are no horizontal connections between the neurons within the layer (lateral connections) and there are no skip-layer connections, the general fully connected network (11), (12) reduces to the architecture shown in Fig.4. It has $N_{\text{layer}}$ layers of recurrently connected neurons with the states described by continuous variables $x_{i}^{A}$ and the activation functions $g_{i}^{A}$, index $A$ enumerates the layers of the network, and index $i$ enumerates individual neurons in that layer. The activation functions can depend on the activities of all the neurons in the layer. Every layer can have a different number of neurons $N_{A}$. These neurons are recurrently connected with the neurons in the preceding and the subsequent layers. The matrices of weights that connect neurons in layers $A$ and $B$ are denoted by $\xi _{ij}^{(A,B)}$ (the order of the upper indices for weights is the same as the order of the lower indices, in the example above this means that the index $i$ enumerates neurons in the layer $A$, and index $j$ enumerates neurons in the layer $B$). The feedforward weights and the feedback weights are equal. The dynamical equations for the neurons' states can be written as
| $\tau _{A}{\frac {dx_{i}^{A}}{dt}}=\sum \limits _{j=1}^{N_{A-1}}\xi _{ij}^{(A,A-1)}g_{j}^{A-1}+\sum \limits _{j=1}^{N_{A+1}}\xi _{ij}^{(A,A+1)}g_{j}^{A+1}-x_{i}^{A}$ | (14) |
with boundary conditions
| $g_{i}^{0}=0,\ \ \ \ \ {\text{and}}\ \ \ \ \ g_{i}^{N_{\text{layer}}+1}=0$ | (15) |
The main difference of these equations from the conventional feedforward networks is the presence of the second term, which is responsible for the feedback from higher layers. These top-down signals help neurons in lower layers to decide on their response to the presented stimuli. Following the general recipe it is convenient to introduce a Lagrangian function $L^{A}(\{x_{i}^{A}\})$ for the $A$-th hidden layer, which depends on the activities of all the neurons in that layer. The activation functions in that layer can be defined as partial derivatives of the Lagrangian
| $g_{i}^{A}={\frac {\partial L^{A}}{\partial x_{i}^{A}}}$ | (16) |
With these definitions the energy (Lyapunov) function is given by
| $E=\sum \limits _{A=1}^{N_{\text{layer}}}{\Big\sum \limits _{i=1}^{N_{A}}x_{i}^{A}g_{i}^{A}-L^{A}{\Big ]}-\sum \limits _{A=1}^{N_{\text{layer}}-1}\sum \limits _{i=1}^{N_{A+1}}\sum \limits _{j=1}^{N_{A}}g_{i}^{A+1}\xi _{ij}^{(A+1,A)}g_{j}^{A}$ | (17) |
If the Lagrangian functions, or equivalently the activation functions, are chosen in such a way that the Hessians for each layer are positive semi-definite and the overall energy is bounded from below, this system is guaranteed to converge to a fixed point attractor state. The temporal derivative of this energy function is given by
| ${\frac {dE}{dt}}=-\sum \limits _{A=1}^{N_{\text{layer}}}\tau _{A}\sum \limits _{i,j=1}^{N_{A}}{\frac {dx_{j}^{A}}{dt}}{\frac {\partial ^{2}L^{A}}{\partial x_{j}^{A}\partial x_{i}^{A}}}{\frac {dx_{i}^{A}}{dt}}\leq 0$ | (18) |
Thus, the hierarchical layered network is indeed an attractor network with the global energy function. This network is described by a hierarchical set of synaptic weights that can be learned for each specific problem.