All models

ModernHopfield

A continuous Hopfield memory whose update rule is transformer attention, packaged as a layer.

37 operations Checked

100%

Showing the top corner; full diagram not loaded yetShowing a small copy; full diagram not loaded yetLarge diagram, not loaded yet

Open SVG

Loading diagram (37 KB)

Open in a new tab

Click or tap a spot to read it up close. Scroll or drag inside the diagram to move through it. Zoom with + and − or Ctrl and scroll. Press 0 for the whole graph. Swipe inside the diagram to move through it. Use + and − to zoom.

At a glance

Parameters
17K
Operations
37
Framework
PyTorch
Recurrent
No
Published
ICLR 2021
Cited by
236 (as of 17 October 2026)
Authors
Hubert Ramsauer, Bernhard Schäfl, Johannes Lehner and others
License
BSD-3-Clause
Checked
Passed: the trace was replayed operation by operation and matched the model's own output for this input.

Files

The diagram is a drawing of a TorchLens trace of the source code. It contains no code or weights.

Details
Stable ID
m1797
Graph hash
da5a0f8c65e3232ed67db43f86db563460035cfb15a14f327ab983fd79f6778a
Skeleton hash
9e86ace7fad48c50449e08812e2445581b2c0ae6c39b6ca13bb121ac4e18387d
Architecture hash
646411f2b5eed54ddba5062e3609768482cf9d1930147c9dc0141a16616e69fd
Diagram SHA-256
1562da6b08c7e0512b60ffcb1e6f50a22490a2d98767b6d1f59bed0bd5693ac4
TorchLens version
2.36.0
Renderer
graphviz 2.43.0+pyvips 3.2.0+libvips 8.18.7+render-opts@06c1f0d3
Check
Forward pass replayed in PyTorch

What it is

The modern Hopfield network, from the paper titled Hopfield Networks is All You Need, is an associative memory with continuous states and a new update rule. Its central claim is that this update rule is the attention mechanism used in transformers, so a transformer head can be read as a memory retrieval. The network stores exponentially many patterns in the dimension of the associative space, retrieves a pattern in one update, and has exponentially small retrieval errors. Its energy landscape has three kinds of minima: a global fixed point that averages over all patterns, metastable states that average over a subset, and fixed points that store a single pattern. The authors use that picture to characterise transformer heads: early layers mostly average globally, while higher layers do partial averaging through metastable states.

The traced member is the Hopfield module from the authors' hflayers package, a PyTorch layer meant to equip deep architectures with Hopfield networks as a memory concept. Building one takes a single argument, the size of the input. The package also ships HopfieldPooling, which trains a state pattern and uses it to compute pooling weights over the input. A third module, HopfieldLayer, trains stored patterns and pattern projections as a lookup that does not depend on the input data. With one static state pattern the layer collapses to a pooling over the sequence, a softmax over the stored patterns.

In the paper's experiments Hopfield layers improved the state of the art on three of four multiple instance learning problems and on immune repertoire classification with several hundred thousand instances.

Key idea. A continuous-state Hopfield network whose update rule is transformer attention, giving exponential storage, one-step retrieval and a layer that can replace pooling, memory and attention blocks.

  • modern Hopfield network
  • Hopfield layer
  • hflayers
  • Hopfield Networks is All You Need
  • associative memory
  • attention
  • HopfieldPooling
  • HopfieldLayer

AI-written summary, based on the paper and checked automatically against it; not reviewed by a person. Corrections welcome.

Cite the paper

@misc{ramsauer2021hopfieldnetworksneed,
      title={Hopfield Networks is All You Need}, 
      author={Hubert Ramsauer and Bernhard Schäfl and Johannes Lehner and Philipp Seidl and Michael Widrich and Thomas Adler and Lukas Gruber and Markus Holzleitner and Milena Pavlović and Geir Kjetil Sandve and Victor Greiff and David Kreil and Michael Kopp and Günter Klambauer and Johannes Brandstetter and Sepp Hochreiter},
      year={2021},
      eprint={2008.02217},
      archivePrefix={arXiv},
      primaryClass={cs.NE},
      url={https://arxiv.org/abs/2008.02217}, 
}