• Stars
    star
    412
  • Rank 105,024 (Top 3 %)
  • Language
    Jupyter Notebook
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code for our paper "Hamiltonian Neural Networks"

Hamiltonian Neural Networks

Sam Greydanus, Misko Dzamba, Jason Yosinski | 2019

overall-idea.png

Basic usage

To train a Hamiltonian Neural Network (HNN):

  • Task 1: Ideal mass-spring system: python3 experiment-spring/train.py --verbose
  • Task 2: Ideal pendulum: python3 experiment-pend/train.py --verbose
  • Task 3: Real pendulum (from this Science paper): python3 experiment-real/train.py --verbose
  • Task 4: Two-body problem: python3 experiment-2body/train.py --verbose
  • Task 4b: Three-body problem: python3 experiment-3body/train.py --verbose
  • Task 5: Pixel pendulum (from OpenAI Gym): python3 experiment-pixels/train.py --verbose

To analyze results

Summary

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.

The HNN recipe

  1. Make a dataset of pixel-space observations of a physical system where energy is conserved. Here we're working with a pendulum.

pendulum-dataset.gif.png

  1. Train an autoencoder on the dataset. The autoencoder is a bit unusual: its latent representation gets fed to the HNN, which tries to model the system's dynamics in latent space.

autoencoder-hnn.png

  1. Since the HNN uses the latent representation to model dynamics, we can think of the latent factors as being analogous to canonical coordinates (e.g. position and velocity).

latents-hnn.png

  1. Phase space plots are a common way to visualize Hamiltonians. We can make a phase space plot in the autoencoder's latent space. We can also integrate along the energy contours of phase space to predict the dynamics of the system (in the figure below, we intentionally "add energy" halfway through).

integrate-latent-hnn.png

  1. After integrating in latent space, we can project back into pixel space to simulate the dynamics of the system.

pendulum-compare-labeled.gif

Here's what it looks like when we add energy halfway through the simulation:

pendulum-compare-labeled.gif

Modeling larger systems

We can also model larger systems -- systems with more than one pair of canonical coordinates. The two-body problem, for example, has four coordinate pairs.

orbits-compare.gif

Numbers

Train loss

  • Choose data of the form x=[x0, x1,...] and dx=[dx0, dx1,...] where dx is the time derivative of x
  • Let dx' = model.time_derivative(x)
  • Compute L2 distance between dx and dx'
Baseline NN Hamiltonian NN
Ideal mass-spring 3.7134e-02 +/- 1.9143e-03 3.6933e-02 +/- 1.9128e-03
Ideal pendulum 3.2606e-02 +/- 1.7434e-03 3.2787e-02 +/- 1.7567e-03
Real pendulum 2.7455e-03 +/- 2.0735e-04 9.2376e-03 +/- 5.0263e-04
2 body problem 3.2682e-05 +/- 1.2022e-06 2.9959e-06 +/- 6.5500e-08
3 body problem 9.5573e-02 +/- 6.5610e-02 8.0346e-02 +/- 2.1470e-02
Pixel pendulum 1.7731e-04 +/- 2.4202e-06 1.8683e-04 +/- 2.4238e-06

Test loss

Do the same thing with test data

Baseline NN Hamiltonian NN
Ideal mass-spring 3.6656e-02 +/- 1.8652e-03 3.5928e-02 +/- 1.8328e-03
Ideal pendulum 3.5273e-02 +/- 1.7970e-03 3.5586e-02 +/- 1.8178e-03
Real pendulum 2.1864e-03 +/- 3.3296e-04 5.9584e-03 +/- 6.1798e-04
2 body problem 2.9575e-05 +/- 8.8900e-07 2.8218e-06 +/- 4.2020e-08
3 body problem 3.8000e-01 +/- 4.1612e-01 4.8809e-01 +/- 4.7745e-01
Pixel pendulum 1.7306e-04 +/- 3.2413e-06 1.8451e-04 +/- 3.3422e-06

Energy MSE

  • Choose a trajectory [x0, x1,...] from test data
  • Use RK4 integration to estimate [x0', x1',...] using the model
  • Compute the L2 distance between [energy(x0), energy(x1),...] and [energy(x0'), energy(x1'),...]
Baseline NN Hamiltonian NN
Ideal mass-spring 1.7077e-01 +/- 2.06e-02 3.8416e-04 +/- 6.53e-05
Ideal pendulum 4.1519e-02 +/- 9.62e-03 2.4852e-02 +/- 5.42e-03
Real pendulum 3.8564e-01 +/- 6.92e-02 1.4477e-02 +/- 4.65e-03
2 body problem 6.3276e-02 +/- 3.36e-02 3.8751e-05 +/- 5.04e-06
3 body problem 1.0906e+02 +/- 7.74e+01 4.1926e-02 +/- 3.39e-02
Pixel pendulum 9.2748e-03 +/- 1.14e-03 1.5315e-04 +/- 8.42e-06

Dependencies

  • OpenAI Gym
  • PyTorch
  • NumPy
  • ImageIO
  • Scipy

This project is written in Python 3.

More Repositories

1

scribe

Realistic Handwriting with Tensorflow
Jupyter Notebook
238
star
2

baby-a3c

A high-performance Atari A3C agent in 180 lines of PyTorch
Python
164
star
3

crypto-rnn

Learning the Enigma with Recurrent Neural Networks
Jupyter Notebook
156
star
4

visualize_atari

Code for our paper "Visualizing and Understanding Atari Agents" (https://goo.gl/AMAoSc)
Jupyter Notebook
115
star
5

mnist1d

A 1D analogue of the MNIST dataset for measuring spatial biases and answering Science of Deep Learning questions.
Jupyter Notebook
97
star
6

pythonic_ocr

A convolutional neural network implemented in pure numpy.
Python
68
star
7

excitationbp

Visualizing how deep networks make decisions
Jupyter Notebook
63
star
8

dnc

Differentiable Neural Computer in TensorFlow
Jupyter Notebook
29
star
9

optimize_wing

We simulate a wind tunnel, place a rectangular occlusion in it, and then use gradient descent to turn the occlusion into a wing.
Python
26
star
10

psi0nn

A neural network quantum ground state solver
Jupyter Notebook
26
star
11

ncf

Nature's Cost Function (NCF). Finding paths of least action with gradient descent.
Jupyter Notebook
12
star
12

greydanus.github.io

My academic blog
HTML
11
star
13

structural_optimization

Coding structural optimization, from scratch, in 200 lines of Python
Python
10
star
14

stereograms

Code for playing with random dot stereograms.
Jupyter Notebook
9
star
15

mr_london

A LSTM recurrent neural network implemented in pure numpy
Python
7
star
16

mnist-gan

Generative Adversarial Networks for the MNIST dataset
Jupyter Notebook
6
star
17

rlzoo

A central location for my reinforcement learning experiments
Jupyter Notebook
5
star
18

subspace-nn

Optimizing neural networks in subspaces
Jupyter Notebook
5
star
19

np_nets

Neural network experiments written purely in numpy
Jupyter Notebook
4
star
20

fractal_tree

A numerical model of fractal dynamics
Jupyter Notebook
4
star
21

studying_growth

Studying Cell Growth with Neural Cellular Automata
Python
4
star
22

piecewise_node

Temporal abstraction for autoregressive sampling
Python
3
star
23

regularization

I use a one-layer neural network trained on the MNIST dataset to give an intuition for how common regularization techniques affect learning.
Jupyter Notebook
3
star
24

dlfun

Forays into the world of deep learning using TensorFlow
Jupyter Notebook
2
star
25

compton

Exploring the quantum nature of light with compton scattering
Jupyter Notebook
2
star
26

friendly_qlearning

Exploring social behavior with qLearning agents
JavaScript
1
star
27

deep_thesaurus

Use a pretrained NLP model to rank thesaurus suggestions
HTML
1
star
28

artiste

The idea here was to teach an RNN to draw, pixel by pixel, over a template image using DDPG
Jupyter Notebook
1
star
29

baselines

Simple MNIST baselines for 1) numpy backprop 2) dense nns 3) cnns 3) seq2seq
Jupyter Notebook
1
star
30

billiards

A simple RL environment for studying planning.
Jupyter Notebook
1
star