Energy Transformer

A single Energy Transformer block is applied recurrently, and each pass updates the token representations by descending the engineered energy, so the forward pass is an optimization rather than a stack of distinct layers. The energy decreases monotonically until the tokens settle at a fixed point. The same block handles graphs (top) and image completion (bottom), where masked patches are filled in as the energy is minimized.

Abstract

Our work combines aspects of three promising paradigms in machine learning, namely, attention mechanism, energy-based models, and associative memory. Attention is the power-house driving modern deep learning successes, but it lacks clear theoretical foundations. Energy-based models allow a principled approach to discriminative and generative tasks, but the design of the energy functional is not straightforward. At the same time, Dense Associative Memory models or Modern Hopfield Networks have a well-established theoretical foundation, and allow an intuitive design of the energy function. We propose a novel architecture, called the Energy Transformer (or ET for short), that uses a sequence of attention layers that are purposely designed to minimize a specifically engineered energy function, which is responsible for representing the relationships between the tokens. In this work, we introduce the theoretical foundations of ET, explore its empirical capabilities using the image completion task, and obtain strong quantitative results on the graph anomaly detection and graph classification tasks.

Highlights

Attention as the Gradient of an Energy

A single block holds two modules that read the same layer-normalized tokens. Energy Attention contributes a term that is low when queries align with keys, so attention here is not a hand-designed operation but a sum in the energy across all attention heads and positions. The Hopfield Network contributes a second term that is low when tokens resemble the stored memory vectors, which is what makes the tokens behave like memories. Their sum, \(E = E^{\text{ATT}} + E^{\text{HN}}\), is the entire objective, and the block updates the tokens by descending its gradient, either in continuous time or in discrete steps of size \(\alpha\). Because both terms are differentiable scalars, the resulting implementation is short: the update is nothing more than automatic differentiation of the energy.

The Energy Transformer block, its two energy terms, and the gradient descent update on their sum
Top. Layer-normalized tokens are read by two modules. Energy Attention makes queries align with keys in the latent space, and the Hopfield Network makes tokens look like memories. The Energy Transformer minimizes the sum of the two energies. Bottom. Each token descends the total energy gradient, either as a continuous flow or in discrete steps of size α. Since the energy is a differentiable scalar, an implementation is a few lines of autodiff.

An Interpretable System That Converges

Because every update is the gradient of an energy, the contribution of each module can be decoded back into the input space and simply looked at. The figure follows one image completion across the descent, where at each step the token state receives a Hopfield Network update and an Energy Attention update, and each can be rendered on its own. Early in the descent the two updates are large and coarse as they rough in the masked patches. Late in the descent they have shrunk and the reconstruction has stopped changing, which is what convergence looks like from the outside: the energy has flattened and the tokens have settled at a fixed point.

Hopfield Network and Energy Attention updates decoded into image space at an early and a late step of the descent
One image completion viewed across the descent. At every step the token state is updated by the sum of the Hopfield Network update and the Energy Attention update, and each contribution can be decoded back into image space on its own. At an early time s the updates are large and coarse; by a late time t they have shrunk and the reconstruction has stopped changing, as the tokens settle into a fixed point of the energy.

Node Anomaly Detection

DatasetSplitGraphConsisCAREGNNPC-GNNBWGNNMLPGTET (Ours)
Macro-F1Yelp1%56.8±2.862.1±1.359.8±1.461.1±0.453.9±0.261.7±0.463.0±0.6
40%58.7±2.063.3±0.963.0±2.371.0±0.957.5±0.868.7±0.471.5±0.1
Amazon1%68.5±3.468.7±1.679.8±5.690.9±0.774.6±1.288.6±0.589.3±0.7
40%75.1±3.286.3±1.789.5±0.792.2±0.479.1±1.291.7±0.892.8±0.3
T-Finance1%71.773.362.084.861.081.585.1±1.0
40%73.477.563.186.870.583.688.2±1.0
T-Social1%52.455.851.175.950.064.379.1±0.7
40%56.556.252.183.950.368.283.5±0.4
AUCYelp1%66.4±3.475.0±3.875.4±0.972.0±0.559.8±0.472.5±0.673.2±0.8
40%69.8±3.076.1±2.979.8±0.184.0±0.966.5±1.081.9±0.584.9±0.3
Amazon1%74.1±3.588.6±3.590.4±2.089.4±0.383.6±1.789.0±1.291.9±1.0
40%87.4±3.390.5±1.695.8±0.198.0±0.489.8±1.095.4±0.697.3±0.4
T-Finance1%90.290.590.791.182.990.092.8±1.1
40%91.492.191.294.387.188.295.0±3.0
T-Social1%65.271.259.888.056.381.491.9±0.6
40%71.271.868.495.256.982.593.9±0.2

Mean and standard deviation over 5 runs with different train/dev/test splits, following the protocol of Tang et al. Standard deviations are shown only where prior work reports them. Bold marks the best result in each row.

Baselines. GraphConsis (Liu et al., SIGIR 2020) · CARE-GNN (Dou et al., CIKM 2020) · PC-GNN (Liu et al., WWW 2021) · BWGNN (Tang et al., 2022) · GT (Dwivedi & Bresson, 2020). MLP is a plain multilayer perceptron with no graph structure.

Graph Classification

MethodPROTEINSNCI1NCI109DDENZYMESMUTAGMUTAGENICITYFRANKENSTEIN
WKPI (kmeans)78.5±0.4
▼ 6.4
87.5±0.585.9±0.4
▼ 1.5
82.0±0.5
▼ 13.7
–85.8±2.5
▼ 14.2
––
WKPI (kcenters)75.2±0.4
▼ 9.7
84.5±0.5
▼ 3.0
87.4±0.380.3±0.4
▼ 15.4
–88.3±2.6
▼ 11.7
––
Spec-GN–84.8±1.6
▼ 2.7
83.6±0.8
▼ 3.8
–72.5±5.8
▼ 5.9
–––
Norm-GN–84.9±1.7
▼ 2.6
83.5±1.3
▼ 3.9
–73.3±8.0
▼ 5.1
–––
GWL-WL75.8±0.6
▼ 9.1
–––71.3±1.1
▼ 7.1
––78.9±0.3
HGP-SL84.9±1.678.5±0.8
▼ 9.1
80.7±1.2
▼ 6.7
81.0±1.3
▼ 14.7
68.8±2.1
▼ 9.6
–82.2±0.6–
DSGCN77.3±0.4
▼ 7.6
–––78.4±0.6–––
U2GNN80.0±3.2
▼ 4.9
––95.7±1.9–88.5±7.1
▼ 11.5
––
NDP73.4±3.1
▼ 11.5
74.2±1.7
▼ 13.3
–72.8±5.4
▼ 22.9
44.5±7.4
▼ 34.9
87.9±5.7
▼ 12.1
77.9±1.4
▼ 4.3
–
ASAP74.2±0.8
▼ 10.7
71.5±0.4
▼ 16.0
70.1±0.6
▼ 17.3
76.9±0.7
▼ 18.8
–––66.3±0.5
▼ 12.6
EvoG––––55.7
▼ 22.7
100.0––
ET (Ours)90.3±0.7
▲ 5.4
90.1±0.1
▲ 2.6
90.5±0.1
▲ 3.1
95.9±0.8
▲ 0.2
99.8
▲ 21.4
96.6±0.2
▼ 3.4
98.7±0.1
▲ 16.5
99.8±0.1
▲ 20.9

Mean and standard deviation from 100 runs of 10-fold cross validation, following the TUDataset protocol. Entries unavailable in prior work are marked –. The shaded cell in each column is the strongest published baseline for that dataset, and every other entry carries its difference against it: ▲ above, ▼ below.

Baselines. WKPI (Zhao & Wang, NeurIPS 2019) · Spec-GN / Norm-GN (Yang et al., ICML 2022) · GWL-WL (Orsini et al., IJCAI 2015) · HGP-SL (Zhang et al., TKDE 2021) · DSGCN (Balcilar et al., 2020) · U2GNN (Nguyen et al., WWW 2022) · NDP (Bianchi et al., TNNLS 2020) · ASAP (Ranjan et al., 2019) · EvoG (Domingue et al., WNYISPW 2019).

Related Work

The Associative Memory foundations that ET is built on are surveyed in Modern Methods in Associative Memory, and the same energy-based perspective is applied to generative models in Memorization to Generalization: Emergence of Diffusion Models from Associative Memory and Language Diffusion Models are Associative Memories Capable of Retrieving Unseen Data. For background, see my blog post on energy-based modeling.

BibTeX

@inproceedings{HooverET23,
  author = {Hoover, Benjamin and Liang, Yuchen and Pham, Bao and Panda, Rameswar and Strobelt Hendrik and Chau, Duen Horng and Zaki, Mohammed and Krotov, Dmitry},
  booktitle = {Advances in Neural Information Processing Systems},
  doi = {10.52202/075280-1197},
  editor = {A. Oh and T. Naumann and A. Globerson and K. Saenko and M. Hardt and S. Levine},
  pages = {27532--27559},
  publisher = {Curran Associates, Inc.},
  title = {Energy Transformer},
  url = {https://proceedings.neurips.cc/paper_files/paper/2023/file/57a9b97477b67936298489e3c1417b0a-Paper-Conference.pdf},
  volume = {36},
  year = {2023}
}