HamiltonianNeuralNetwork<T>
Models & Types · AiDotNet.PhysicsInformed.ScientificML
Implements Hamiltonian Neural Networks (HNN) for learning conservative dynamical systems.
Inheritance: NeuralNetworkBase<T> → HamiltonianNeuralNetwork<T>
How It Works
For Beginners: Hamiltonian Neural Networks learn the laws of physics by respecting conservation principles.
Classical Mechanics - Hamiltonian Formulation: Many physical systems are described by Hamilton’s equations:
- dq/dt = ∂H/∂p (position changes with momentum gradient)
- dp/dt = -∂H/∂q (momentum changes with negative position gradient)
Where:
- q = position coordinates
- p = momentum coordinates
- H(q,p) = Hamiltonian (total energy of the system)
Key Property: Energy Conservation For conservative systems, H(q,p) = constant (energy is conserved)
Traditional Neural Networks vs. HNN:
Traditional NN:
- Learn dynamics directly: (q,p) → (dq/dt, dp/dt)
- Can violate physics laws
- May not conserve energy
- Can accumulate errors over time
HNN:
- Learn the Hamiltonian: (q,p) → H
- Compute dynamics from H: dq/dt = ∂H/∂p, dp/dt = -∂H/∂q
- Automatically conserves energy (by construction!)
- More accurate long-term predictions
How It Works:
- Neural network learns H(q,p)
- Use automatic differentiation to get ∂H/∂q and ∂H/∂p
- Apply Hamilton’s equations to get dynamics
- Guaranteed to preserve Hamiltonian structure
Applications:
- Planetary motion (gravitational systems)
- Molecular dynamics (particle interactions)
- Robotics (mechanical systems)
- Quantum mechanics (Schrödinger equation)
- Any conservative system
Example - Pendulum: H(q,p) = p²/(2m) + mgl(1 - cos(q))
- q = angle, p = angular momentum
- HNN learns this from data without knowing the formula!
Key Benefit: By encoding physics structure (Hamiltonian formulation), the network learns faster, generalizes better, and makes physically consistent predictions.
Example
var hnn = new HamiltonianNeuralNetwork<float>();
float[] derivatives = hnn.ComputeTimeDerivative(new float[] { 1.0f, 0.0f, 0.0f, 1.0f });
Constructors
| Constructor | Summary |
|---|---|
HamiltonianNeuralNetwork() |
|
HamiltonianNeuralNetwork(NeuralNetworkArchitecture<T> architecture, int stateDim, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, HamiltonianNeuralNetworkOptions options) |
Properties
| Property | Summary |
|---|---|
TrainingObjectiveKind TrainingObjectiveKind { get; } |
|
bool SupportsTraining { get; } |
Serializes Hamiltonian-specific data. |
Methods
| Method | Summary |
|---|---|
ModelMetadata<T> GetModelMetadata() |
Gets metadata about the Hamiltonian network. |
T ComputeHamiltonian(Vector<T> state) |
Computes the Hamiltonian (energy) for a given state. |
Tensor<T> Forward(Tensor<T> input) |
Performs a forward pass through the network. |
Tensor<T> ForwardForTraining(Tensor<T> input) |
Produces the symplectic time-derivative field optimized by the cited HNN paper. |
Tensor<T> Simulate(Vector<T> initialState, T dt, int numSteps) |
Simulates the system forward in time. |
Vector<T> ComputeTimeDerivative(Vector<T> state) |
Computes the time derivative of the state using Hamilton’s equations. |