Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Modern Hopfield Networks

Modern Hopfield Networks

Modern Hopfield Networks (MHN) represent a dramatic reimagining of the classical Hopfield network from the 1980s. By replacing the quadratic energy function with an exponential one, MHNs achieve exponential storage capacity and connect naturally to the transformer attention mechanism, serving as a theoretical bridge between associative memory and modern deep learning.

Classical Hopfield Networks

Introduced by John Hopfield in 1982, classical networks are fully connected recurrent systems designed as content-addressable memories. Given a noisy or partial input, the network retrieves the closest stored pattern.

Energy function:

$$E = -\frac{1}{2} \mathbf{x}^T W \mathbf{x} + \sum_i \theta_i x_i$$

where $W$ is the symmetric weight matrix (zero diagonal) and $\mathbf{x} \in {-1, +1}^N$.

Storage capacity: approximately $0.138N$ patterns for $N$ neurons before retrieval errors dominate.

Asynchronous update rule:

$$x_i \leftarrow \text{sgn}\left(\sum_j W_{ij} x_j - \theta_i\right)$$

The network converges to a local energy minimum corresponding (ideally) to a stored pattern.

Limitations

  • Low capacity: $O(N)$ patterns for $N$ neurons
  • Spurious states: false minima unrelated to stored patterns
  • Binary representations: limited to $\pm 1$ states
  • Slow convergence: many asynchronous update steps required

Dense Associative Memories

Krotov & Hopfield (2016) introduced Dense Associative Memories using higher-order polynomial interactions:

$$E = -\sum_{\mu=1}^{M} F\left(\mathbf{x} \cdot \boldsymbol{\xi}^\mu\right) + \frac{1}{2}|\mathbf{x}|^2$$

where $F(x) = x^n$ for integer $n \geq 2$. Storage capacity scales as $O(N^{n-1})$, a significant improvement over classical networks.

The Modern Hopfield Network

Ramsauer et al. (2020) achieved the breakthrough: replacing polynomial $F$ with the exponential function.

Energy function:

$$E = -\text{lse}(\beta, X^T \boldsymbol{\xi}) + \frac{1}{2}|\boldsymbol{\xi}|^2 + \frac{1}{\beta}\log N + \frac{1}{2}M^2$$

where $\text{lse}(\beta, \mathbf{z}) = \frac{1}{\beta}\log\sum_i e^{\beta z_i}$ is the log-sum-exp function.

Update rule (fixed-point iteration):

$$\boldsymbol{\xi}^{\text{new}} = X \cdot \text{softmax}(\beta X^T \boldsymbol{\xi})$$

This converges in 1–2 steps for well-separated patterns.

Storage capacity: exponential — approximately $2^{N/2}$ patterns before confusion errors occur.

Connection to Transformer Attention

The MHN update rule is mathematically identical to scaled dot-product attention:

$$\text{Attn}(Q, K, V) = \text{softmax}!\left(\frac{QK^T}{\sqrt{d}}\right)V$$

Identifying:

  • $\boldsymbol{\xi} \leftrightarrow Q$ (query / pattern to retrieve)
  • $X^T \leftrightarrow K$ (stored patterns / keys)
  • $X \leftrightarrow V$ (retrieved values)
  • $\beta = 1/\sqrt{d}$ (inverse temperature)

This reveals that transformers perform approximate associative memory retrieval at every attention layer.

Retrieval Properties

Capacity and Separation

For $N$-dimensional continuous patterns, storage capacity is:

$$M \approx \frac{1}{2} e^{\alpha N}, \quad \alpha > 0$$

subject to minimum separation $\Delta_{\min}$ between stored patterns. Closer patterns require lower $\beta$ for reliable retrieval.

Temperature Control

  • $\beta \to \infty$: hard nearest-neighbor retrieval — exact pattern recovery
  • $\beta$ moderate: soft retrieval — weighted average of nearby patterns
  • $\beta \to 0$: global mean — all patterns averaged equally

Learnable $\beta$ adapts retrieval sharpness per attention head.

PyTorch Implementation

import torch
import torch.nn as nn
import torch.nn.functional as F


class ModernHopfieldLayer(nn.Module):
    """
    Modern Hopfield Network layer — associative memory retrieval.

    Args:
        input_dim: dimensionality of stored patterns and queries
        beta: inverse temperature controlling retrieval sharpness
    """

    def __init__(self, input_dim: int, beta: float = 8.0):
        super().__init__()
        self.beta = beta
        self.W_q = nn.Linear(input_dim, input_dim, bias=False)
        self.W_k = nn.Linear(input_dim, input_dim, bias=False)
        self.W_v = nn.Linear(input_dim, input_dim, bias=False)
        self.W_out = nn.Linear(input_dim, input_dim, bias=False)

    def forward(self, queries: torch.Tensor, stored: torch.Tensor) -> torch.Tensor:
        # queries: (B, d), stored: (M, d)
        Q = self.W_q(queries)   # (B, d)
        K = self.W_k(stored)    # (M, d)
        V = self.W_v(stored)    # (M, d)

        scores = self.beta * Q @ K.T     # (B, M)
        weights = F.softmax(scores, dim=-1)  # (B, M)
        retrieved = weights @ V          # (B, d)
        return self.W_out(retrieved)


# Usage
dim = 64
memory_bank = torch.randn(100, dim)   # 100 stored patterns
queries = torch.randn(8, dim)         # batch of 8 queries

layer = ModernHopfieldLayer(dim, beta=8.0)
output = layer(queries, memory_bank)
print(output.shape)  # (8, 64)

Hopfield Pooling

A key application is Hopfield pooling for set-valued inputs — aggregating variable-length sequences into a fixed-size representation.

class HopfieldPooling(nn.Module):
    """Aggregate a variable-length set into a fixed-size output."""

    def __init__(self, input_dim: int, num_seeds: int = 4):
        super().__init__()
        self.seeds = nn.Parameter(torch.randn(num_seeds, input_dim))
        self.hopfield = ModernHopfieldLayer(input_dim)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # x: (N, d) variable-length set
        return self.hopfield(self.seeds, x)  # (num_seeds, d)

This enables permutation-invariant processing of drug-protein binding data, immune repertoire sequences, and any bag-of-features input.

Comparison: Classical vs Modern Hopfield

PropertyClassicalDense (poly $n$)Modern (exp)
State space${-1,+1}^N$$\mathbb{R}^N$$\mathbb{R}^N$
EnergyQuadraticPolynomial $x^n$Log-sum-exp
Capacity$0.138N$$O(N^{n-1})$$\sim 2^{N/2}$
Convergence stepsManyFew1–2
Spurious statesManyFewerRare
Equivalent to attentionNoNoYes

Applications

Drug–Protein Interaction Prediction

MHN layers pool molecular fingerprints against a memory bank of known drug-target pairs, enabling sample-efficient binding affinity prediction.

Immune Repertoire Classification

Hopfield pooling aggregates antibody sequence sets of variable size to predict immune response to novel antigens.

Few-Shot Learning

Support-set examples are stored as memories; MHN retrieval produces task-relevant prototypes without gradient updates at inference.

Anomaly Detection

Normal patterns are stored; high retrieval reconstruction error signals anomalous inputs.

Limitations

  • Memory bank size: large $M$ requires proportionally large key matrices
  • Pattern interference: highly correlated patterns may be confused even at high $\beta$
  • Static memory: standard MHNs require fixed patterns at inference — online updating is an open problem
  • Biological plausibility: exponential interactions are not obviously implementable by neurons

Summary

Modern Hopfield Networks extend classical associative memory to exponential capacity through an energy function based on log-sum-exp. Their update rule is mathematically equivalent to transformer self-attention, revealing that transformers implicitly perform content-addressable memory retrieval. MHNs are practical tools for set pooling, drug discovery, and immune repertoire classification, and provide a rigorous theoretical lens for understanding the attention mechanism.