Hamiltonian Neural Network
In this tutorial we build a Hamiltonian neural network.
Training a HNN Based on VectorField Data
We first train a HNN based on vector field data:
using GeometricMachineLearning: QPT
using LinearAlgebra: norm
using Zygote: gradient
𝕁 = PoissonTensor(2)
vf(z) = 𝕁 * z
domain = [[q, p] for q in -1:.1:1 for p in -1:.1:1]
vf_data = vf.(domain)
domain_matrix = hcat(domain...)
vf_matrix = hcat(vf_data...)
dl = DataLoader(domain_matrix, vf_matrix)
[ Info: You have provided an input and an output.
We then build the neural network:
const intermediate_dim = 5
hnn_arch = StandardHamiltonianArchitecture(2, intermediate_dim)
hnn = NeuralNetwork(hnn_arch)
Next we define the loss function
loss = HNNLoss(hnn_arch)
We can now train the network
batch = Batch(10)
n_epochs = 100
o = Optimizer(AdamOptimizer(Float64), hnn)
loss_array = o(hnn, dl, batch, n_epochs, loss)

Usually we use Zygote
for computing derivatives in GeometricMachineLearning
, but as the Zygote
documentation itself points out: "Often using a different AD system over Zygote is a better solution [for computing second-order derivatives]." For this reason we compute the loss of the HNN with SymbolicNeuralNetworks
and optionally also its gradient.