← all repositories
greydanus/hamiltonian-nn

Teaching neural networks to obey conservation of energy

A 2019 research project that bakes Hamiltonian mechanics into neural network architectures so models learn physics instead of just curve-fitting it.

541 stars Jupyter Notebook Domain AppsML Frameworks
hamiltonian-nn
Not currently ranked — collecting fresh signals.
star history

What it does

Hamiltonian Neural Networks (HNNs) constrain a neural network to learn dynamics that respect exact conservation laws—energy, momentum—without supervision. The repo implements the paper’s architecture and trains it on five physics tasks: mass-spring, pendulum (ideal, real, and pixel-based), and N-body problems up to three bodies. Each experiment has a training script and a matching Jupyter notebook for analysis.

The interesting bit

The trick is architectural, not data-driven. The network learns a scalar Hamiltonian function and derives dynamics from its gradients, which forces energy conservation by construction. For pixel observations, an autoencoder compresses images into a latent space where the HNN operates; the authors treat latent factors as canonical coordinates (position and velocity). A side effect the authors note: the model is perfectly reversible in time, unlike standard neural nets.

Key highlights

  • Energy conservation is dramatically better than baseline: on the two-body problem, energy MSE drops from 6.3e-02 to 3.9e-05; on pixel pendulum, from 9.3e-03 to 1.5e-04
  • Test loss improvements are modest or mixed for simple systems (ideal spring, ideal pendulum), but substantial for complex ones (two-body: ~10x better; real pendulum: ~2.5x better on test loss, ~65x better on energy)
  • Includes a full pipeline from raw pixels: autoencoder → latent HNN → integrate → decode back to pixels
  • Reversible dynamics emerge for free from the Hamiltonian structure

Caveats

  • The 3-body problem shows high variance and marginal improvement on test loss; the HNN actually underperforms the baseline (4.88e-01 vs 3.80e-01, though with large error bars)
  • Real pendulum test loss is worse than baseline (5.96e-03 vs 2.19e-03) despite better energy conservation—suggesting the inductive bias trades trajectory accuracy for physical correctness
  • Code is PyTorch 2019-era; dependencies include OpenAI Gym, ImageIO, Scipy

Verdict

Worth studying if you’re building physics-informed models or care about long-horizon stability in learned simulators. Skip if you need production-ready code or are working on problems where energy conservation isn’t the bottleneck.

Frequently asked

What is greydanus/hamiltonian-nn?
A 2019 research project that bakes Hamiltonian mechanics into neural network architectures so models learn physics instead of just curve-fitting it.
Is hamiltonian-nn open source?
Yes — greydanus/hamiltonian-nn is open source, released under the Apache-2.0 license.
What language is hamiltonian-nn written in?
greydanus/hamiltonian-nn is primarily written in Jupyter Notebook.
How popular is hamiltonian-nn?
greydanus/hamiltonian-nn has 541 stars on GitHub.
Where can I find hamiltonian-nn?
greydanus/hamiltonian-nn is on GitHub at https://github.com/greydanus/hamiltonian-nn.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.