Source-linked AI summary
Hamiltonian Neural Networks
Sam Greydanus, Misko Dzamba, Jason Yosinski
TL;DR
Neural networks lack exact physics priors and can drift from true dynamics as errors accumulate. This paper parameterizes a Hamiltonian with a neural network and learns it from data, using gradients to train dynamics that conserve energy-like quantities. HNNs match baseline losses while substantially improving long-term energy conservation, but they assume a conserved quantity exists and therefore do not directly model effects such as friction.
Problem
Neural networks learn approximate physics from data and existing methods may fail to learn exact conservation laws, causing long-term dynamical drift.
Method
HNNs parameterize the Hamiltonian with a neural network, output an energy-like scalar, and optimize an L2 loss based on its input gradient.
Results
HNNs train as quickly as baseline models, achieve similar final losses, and dramatically outperform them on the energy MSE metric across three tasks.
Takeaways & Limitations
HNNs learn conserved quantities from data and provide dynamics that diverge more slowly or not at all while remaining reversible in time.
Takeaways & Limitations
HNNs assume a conserved quantity exists and cannot account for effects such as friction without modeling them separately.
Abstract
from arXiv · showhide
Even though neural networks enjoy widespread use, they still struggle to learn the basic laws of physics. How might we endow them with better inductive biases? In this paper, we draw inspiration from Hamiltonian mechanics to train models that learn and respect exact conservation laws in an unsupervised manner. We evaluate our models on problems where conservation of energy is important, including the two-body problem and pixel observations of a pendulum. Our model trains faster and generalizes better than a regular neural network. An interesting side effect is that our model is perfectly reversible in time.
1 Introduction
The paper addresses neural networks’ lack of exact physics priors by parameterizing a Hamiltonian with a neural network and learning it from data. HNNs learn energy-like conserved quantities and generalize across physical systems.
- The proposed physics priors are motivated as transferable across diverse tasks governed by shared physical laws.
- Neural networks learn approximate physics directly from data rather than exact physical laws, causing energy drift in forward simulations.In the frictionless mass-spring example, the baseline approximates the conserved quantity proportional to q^2 + p^2 but gradually reaches incorrect energy states.
- Hamiltonian Neural Networks use Hamiltonian mechanics to incorporate conservation laws and invariances into learned dynamics.The Hamiltonian relates system state to a conserved quantity, usually energy, and determines how the system changes over time.
- HNNs parameterize the Hamiltonian with a neural network and learn it directly from data instead of crafting it by hand.
- HNNs learn an energy-like conserved quantity on the mass-spring system, unlike the baseline model.
2 Theory
The theory motivates continuous-time dynamics and exact conservation because discrete-step methods can accumulate errors. HNNs address these issues by learning a Hamiltonian whose gradients generate dynamics and support reversibility.
- Predicting dynamics: Existing next-state predictors use discrete time steps, whereas continuous-time dynamics can be represented by differential equations integrated between times t0 and t1.Equation 1 describes this approach using S for the time derivatives of system coordinates.
- Predicting dynamics: Existing methods often fail to learn exact conservation laws, so accumulated errors can make their trajectories drift from the true dynamics.
- Hamiltonian Mechanics: Hamiltonian mechanics represents a system with position coordinates q and momenta p, together forming a complete system description.
- Hamiltonian Mechanics: The symplectic gradient of the Hamiltonian generates time evolution while keeping the Hamiltonian’s output exactly constant.Hamiltonian mechanics relates q and p to total energy Etot = H(q, p), then obtains system dynamics by integrating the symplectic-gradient field.
- Hamiltonian Neural Networks: HNNs learn a parametric H from data, output an energy-like scalar, and optimize an L2 loss computed from its input gradient.The gradient is obtained in-graph before the loss is computed.
- Hamiltonian Neural Networks: HNN mappings are perfectly reversible in time, and their conserved quantity can be manipulated to construct energy-based counterfactuals.
3 Learning a Hamiltonian from Data
The paper evaluates HNNs on synthetic and real pendulum and mass-spring tasks, comparing them with baseline networks on fitting, energy stability, and dynamics prediction. HNNs conserve energy-like quantities and avoid the long-term divergence seen in baselines, while real-world friction exposes a fundamental limitation.
- Tasks: HNNs were evaluated on ideal mass-spring, ideal pendulum, and noisy real-pendulum dynamics tasks.The real pendulum dataset included friction and therefore did not strictly obey conservation laws.
- Training and metrics: Both models used fully connected networks, but the HNN estimated dynamics from the derivative of a scalar quantity rather than predicting derivatives directly.The scalar quantity was trained using an in-graph gradient and an L2 loss.
- Results: HNNs trained as quickly as baselines and converged to similar final losses across the three tasks.Their advantage appeared in the energy MSE metric rather than ordinary training or test loss.
- Results: HNNs dramatically outperformed baselines on energy MSE because baseline errors accumulated during integration and caused eventual divergence.The HNN conserved a quantity resembling total energy and diverged more slowly or not at all.
- Results: The HNN-conserved quantity differed from total energy by a constant factor but remained on the same scale, which the paper considers acceptable.The paper emphasizes that the learned quantity is close to, but not equivalent to, total energy.
- Limitations: On the real pendulum, HNNs roughly conserved total energy despite friction, revealing that they assume a conserved quantity exists.Modeling friction would require a component separate from the HNN.
4 Modeling Larger Systems
The HNN scaled to the two-body problem, learning an energy-like conserved quantity and maintaining more accurate long-term dynamics than the baseline. Three-body results showed a relative HNN advantage but remained insufficiently accurate for the main text.
- Two-body problem: The two-body problem models point particles interacting through an attractive force and uses eight degrees of freedom.The experiments set m1 = m2 = g = 1 and restricted the center-of-mass momentum to zero.
- Two-body problem: The dataset comprised 1000 near-circular two-body trajectories generated with controlled initial conditions and velocity noise.Initial radii ranged from 0.5 to 1.5, with σ2 = 0.05 producing a balance between stability and diversity.
- Results: The HNN learned to conserve a quantity nearly equal to total energy, whereas the baseline did not.The HNN model scaled well to this larger system.
- Results: After one orbit, baseline trajectories completely diverged from ground truth while HNN trajectories accumulated only a small amount of error.At longer times, both models diverged, but the HNN did so more slowly and retained stable total energy.
- Results: The HNN’s train and test losses were about an order of magnitude lower than the baseline’s, while its energy MSE was several orders below the baseline.These quantitative results were reported for the two-body task.
- Three-body problem: On the chaotic three-body problem, the HNN outperformed its baseline considerably, but both models struggled to model the dynamics accurately.The authors therefore relegated these preliminary results to the Appendix.
5 Learning
The pixel-pendulum experiment combines an autoencoder with an HNN to learn dynamics from image observations in latent space. Compared with the baseline, the HNN preserves energy and remains accurate over much longer trajectories.
- Motivation: The experiment tests whether HNNs can learn dynamics from arbitrary coordinates, specifically autoencoder latent vectors rather than position and momentum coordinates.The model is evaluated on pixel observations of a pendulum.
- Methods: The dataset uses paired 28 x 28 x 1 grayscale frames so velocity is observable to the nonrecurrent autoencoder.Inputs concatenate each frame with its successor, producing tensors shaped batch x 28 x 28 x 2.
- Methods: The model combines an HNN loss, pixel-level autoencoder reconstruction loss, and an auxiliary latent-space loss.The auxiliary term encourages the second half of the latent vector to resemble derivatives of the first half.
- Results: In latent space, the baseline rapidly decays toward lower-energy states, whereas the HNN remains close to ground truth after hundreds of frames.Predicted latent trajectories are projected back into pixel space using the decoder for visualization.
- Results: The HNN learned a scalar quantity analogous to total energy and dramatically outperformed the baseline on the energy metric despite comparable train and test losses.Dynamics were integrated in the autoencoder’s latent space before projection to pixels.
- Results: Table 1 reports results across all five tasks, with HNNs competitive on train/test loss and substantially better on the energy metric.Values are multiplied by 10^3 unless noted otherwise.
6 Useful properties of HNNs
Beyond conservation, HNNs support controlled changes to the learned energy-like quantity and provide exact reversibility. These properties suggest uses in counterfactual simulation and memory-efficient dynamics modeling.
- Adding and removing energy: Integrating the Riemann gradient RH can add or remove the quantity conserved by the HNN.Alternating RH with the symplectic gradient SH can bump the pendulum to a higher energy level.
- Adding and removing energy: This energy manipulation can support counterfactual questions such as what would happen if a torque were applied.The paper presents this as a possible use of the technique.
- Adding and removing energy: Alternating low-energy SH integration, RH integration, and higher-energy SH integration changes the pendulum’s energy level.Figure 5 visualizes the sequence with blue and red circles connected by a purple line.
- Perfect reversibility: HNN dynamics are perfectly reversible: the mapping from (q, p) at one time to (q, p) at another is bijective.This contrasts with methods described as only approximately reversible.
7 Related work
The paper situates HNNs among approaches that learn physical laws or encode physics priors with neural networks. It emphasizes a general conservation-law formulation rather than domain-specific modeling or hand-picked function classes.
- Learning physical laws from data: Prior work used genetic algorithms to recover conservation laws and physical functions, whereas this paper parameterizes the Hamiltonian with a neural network.The neural approach avoids restricting the search to hand-picked functions.
- Physics priors for neural networks: Many physics-prior methods are domain-specific, while Interaction Networks and Relational Networks were designed to be fully general.The paper likewise aims to keep its approach fully general.
- Modeling energy surfaces: Molecular-dynamics research uses neural networks to model energy surfaces and integrates energy derivatives to obtain dynamics.The paper identifies this derivative-based dynamics construction as a key point of comparison.
8 Discussion
Hamiltonian Neural Networks combine first-principles Hamiltonian mechanics with data-driven deep learning. The authors present this combination as a promising way to unite the strengths of both approaches.
- Hamiltonian Neural Networks bring together Hamiltonian mechanics’ first-principles description of the real world and deep learning’s data-driven modeling.
A Supplementary Information for Tasks 1-3
The supplementary material describes the HNN schema, training setup, and an unusual Task 3 split. The first three tasks include ideal mass-spring, ideal pendulum, and real pendulum systems.
- The HNN forward pass combines a differentiable-model forward pass with backpropagation through the model.
- Hyperparameters were selected by coarse grid search, with settings chosen for good qualitative performance rather than state-of-the-art results.
- All experiments were trained on a desktop CPU.
- Task 3: Task 3 used the first 4/5 of a single real-pendulum trajectory for training and the last 1/5 for testing.
- Task 3: The Task 3 test set had a slightly different distribution from its training set, unlike the other two tasks.
- The first three physics tasks were ideal mass-spring, ideal pendulum, and real pendulum.
B Supplementary Information for Task 4: Two-body problem
The two-body supplementary results compare HNNs with baseline networks using qualitative trajectories and quantitative energy-conservation analyses. Additional three-body results show a relative HNN advantage, but both models struggle with the dynamics.
- Two-body problem: Two-body qualitative results show numerical errors accumulating in the baseline until the bodies travel in opposite directions.
- Two-body problem: The baseline’s total energy diverges toward infinity, whereas the HNN trajectory roughly conserves total energy despite diverging from ground truth.
- Two-body problem: Figure B.2 compares how well the HNN conserves total energy against the baseline on the two-body task.
- Three-body problem: Both models struggled to accurately model the three-body dynamics, although the reported results show a relative advantage for the HNN.
- Three-body problem: Table 2 summarizes quantitative results for the three-body problem, with all values multiplied by 10^2.
- Three-body problem: The three-body confidence intervals suggest that both models struggle with the dataset distribution, possibly because of its dynamic range.
- Three-body problem: Three-body qualitative analysis shows that the baseline fails to conserve total energy and quickly diverges, while the HNN roughly conserves energy and resembles ground truth.
C Supplementary Information for Task 5: Pixel Pendulum
The Pixel Pendulum supplementary material examines learned latent representations and notes training sensitivity to weight decay. The learned latent space strongly resembles the pendulum’s true phase space.
- Weight decay had a significant impact on the Pixel Pendulum results, unlike in previous experiments.The authors used weight decay of 10^-5 and suspected differing gradient scales between the HNN and autoencoder portions.
- The Pixel Pendulum experiment was trained on a desktop CPU.
- The learned latent space strongly resembles the true phase space of a pendulum, including a faint diamond-shaped outer contour.