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.

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.