Rectified Lagrangian for Out-of-Distribution Detection in Modern Hopfield Networks

Ryo Moriai, Nakamasa Inoue, Masayuki Tanaka, Rei Kawakami, Satoshi Ikehata, Ikuro Sato

Abstract

Modern Hopfield Networks (MHNs) serve as powerful associative memory systems capable of storing an exponential number of patterns. However, their inherent dynamics typically force any input toward a stored memory attractor, leading to a "hallucination" problem when encountering Out-of-Distribution (OOD) data. This paper introduces the Rectified Lagrangian (RecLag), a novel mathematical framework that modifies the network's energy landscape to include a dedicated "trivial attractor" at the origin. This modification allows the network to effectively categorize unknown inputs as "none of the above." Empirical evaluations across multiple architectures and benchmarks demonstrate that RecLag significantly outperforms existing energy-based OOD detection methods, providing a more robust and theoretically grounded approach to uncertainty in associative memories.

Introduction

Modern Hopfield Networks (MHNs) have revolutionized associative memory in artificial intelligence by leveraging high-storage capacity and precise retrieval dynamics. Unlike their classical counterparts, MHNs utilize a continuous state space and energy functions that allow for the storage of complex patterns. Despite these advancements, a fundamental limitation remains: the "closed-world" assumption. Because the system is designed to minimize energy by pulling inputs into established basins of attraction, it lacks a mechanism to reject samples that do not belong to the training distribution. When presented with OOD data, a standard MHN will stubbornly map the input to the nearest known pattern, a behavior that can be catastrophic in safety-critical applications.

To address this, researchers from the Institute of Science Tokyo and Denso IT Laboratory propose RecLag (Rectified Lagrangian). By introducing a rectification step into the Lagrangian function of the memory neurons, the authors create a "catch-all" sink for inputs that do not strongly align with any stored memory.

The RecLag Framework

1. The Rectified Lagrangian

The core innovation of RecLag lies in the modification of the Lagrangian function ($L_H$) for memory neurons. By applying a max(..., 0) operation—analogous to the ReLU activation function used in deep learning—the authors introduce a constant $\gamma$ representing inverse memory strength. This rectification ensures that if the input signal to a memory neuron is sufficiently weak, its contribution to the network's dynamics is nullified.

2. The Trivial Attractor

Mathematically, this modification introduces a stable fixed point at the origin ($v=0$) of the feature space. In a standard MHN, the energy landscape consists only of valleys corresponding to stored patterns. In a RecLag-equipped network, the origin becomes a "trivial attractor." If an input is sufficiently distant from all stored memories, the network's dynamics will cause the state to converge to zero. This provides a definitive, geometrically grounded signal for OOD detection: any sample that converges to the origin is classified as OOD.

3. Probabilistic Training and Theoretical Continuity

The authors treat the interaction between neurons as a probabilistic system, training the network to maximize the probability density of In-Distribution (ID) samples. This ensures that the basins of attraction for known patterns are well-defined, while the "low-probability" regions of the feature space are funneled toward the $v=0$ attractor. Furthermore, the authors provide a theoretical bridge showing that as memory strength approaches infinity, RecLag reverts to a standard MHN, proving it is a natural and backward-compatible extension of the architecture.

Experimental Evaluation

Datasets and Architectures

The effectiveness of RecLag was tested using three neural network architectures: ResNet18, ResNet34, and WideResNet40-2. The models were trained on In-Distribution (ID) datasets (CIFAR-10 and CIFAR-100) and tested against a comprehensive suite of OOD benchmarks, including:

  • SVHN, LSUN-C, LSUN-R, iSUN, Places365, DTD, TinyImageNet, SUN, and iNaturalist.

Performance Metrics

The primary metrics for evaluation were the False Positive Rate at 95% True Positive Rate (FPR95) and the Area Under the Curve (AUC) for OOD detection.

Key Results and Insights

RecLag consistently outperformed five major baselines, including state-of-the-art energy-based methods like Modern Hopfield Energy (MHE) and Softmax-Hopfield Energy (SHE).

  • Superior Detection Accuracy: On ResNet18 trained on CIFAR-10, RecLag achieved an average FPR95 of 5.85%, a significant improvement over the 7.41% achieved by SHE. In 23 out of 27 experimental configurations, RecLag yielded the highest AUC.
  • Convergence Stability: A common issue in MHN-based OOD detection is that detection scores often degrade as the dynamical system converges (as the OOD sample is pulled closer to an ID attractor). With RecLag, the separation between ID and OOD samples remains stable or even improves during convergence, as OOD samples are actively pushed toward the trivial attractor.
  • Computational Efficiency: Because the detection mechanism is integrated into the natural dynamics of the network, RecLag adds no additional computational overhead during the inference phase compared to other post-hoc detection methods.

Conclusion and Future Work

RecLag provides a elegant solution to the hallucination problem in Modern Hopfield Networks by linking the geometric concept of a "basin of attraction" to the statistical concept of "probability density." By creating a dedicated space for unknown data, it allows associative memories to operate more safely in open-world environments.

Future research directions include extending the Rectified Lagrangian to the feature neurons (which currently use standard Euclidean Lagrangians) and exploring non-linear feature spaces such as spherical or hyperbolic geometries. Additionally, while RecLag excels at broad OOD detection, distinguishing between visually similar sub-classes remains a challenge that warrants further investigation in the context of structured memory patterns.

Summary updated

BibTeX
@inproceedings{moriai2025rectified,
  author = {Ryo Moriai and Nakamasa Inoue and Masayuki Tanaka and Rei Kawakami and Satoshi Ikehata and Ikuro Sato},
  title = {{Rectified Lagrangian for Out-of-Distribution Detection in Modern Hopfield Networks}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i18.34153},
}
Figure