You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/examples/hamiltonian_nn.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,8 +8,8 @@ m\ddot(x) + kx = 0
8
8
9
9
Now we make some simplifying assumptions, and assign ``m = 1`` and ``k = 1``. Analytically solving this equation, we get ``x = sin(t)``. Hence, ``q = sin(t)``, and ``p = cos(t)``. Using these solutions we generate our dataset and fit the `NeuralHamiltonianDE` to learn the dynamics of this system.
10
10
11
-
```julia
12
-
using DiffEqFlux, DifferentialEquations, Statistics, Plots
11
+
```@example hamiltonian_cp
12
+
using Flux, DiffEqFlux, DifferentialEquations, Statistics, Plots, ReverseDiff
The HNN predicts the gradients ``(\dot(q), \dot(p))`` given ``(q, p)``. Hence, we generate the pairs ``(q, p)`` using the equations given at the top. Additionally to supervise the training we also generate the gradients. Next we use use Flux DataLoader for automatically batching our dataset.
67
67
68
-
```julia
68
+
```@example hamiltonian
69
+
using Flux, DiffEqFlux, DifferentialEquations, Statistics, Plots, ReverseDiff
We parameterize the HamiltonianNN with a small MultiLayered Perceptron (HNN also works with the Fast* Layers provided in DiffEqFlux). HNNs are trained by optimizing the gradients of the Neural Network. Zygote currently doesn't support nesting itself, so we will be using ReverseDiff in the training loop to compute the gradients of the HNN Layer for Optimization.
84
86
85
-
```julia
87
+
```@example hamiltonian
86
88
hnn = HamiltonianNN(
87
89
Chain(Dense(2, 64, relu), Dense(64, 1))
88
90
)
@@ -112,7 +114,7 @@ callback()
112
114
113
115
In order to visualize the learned trajectories, we need to solve the ODE. We will use the `NeuralHamiltonianDE` layer which is essentially a wrapper over `HamiltonianNN` layer and solves the ODE.
0 commit comments