Table of Contents

Class HamiltonianNeuralNetwork<T>

Namespace
AiDotNet.PhysicsInformed.ScientificML
Assembly
AiDotNet.dll

Implements Hamiltonian Neural Networks (HNN) for learning conservative dynamical systems.

public class HamiltonianNeuralNetwork<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations.

Inheritance
HamiltonianNeuralNetwork<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Remarks

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:

  1. Neural network learns H(q,p)
  2. Use automatic differentiation to get ∂H/∂q and ∂H/∂p
  3. Apply Hamilton's equations to get dynamics
  4. 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.

Constructors

HamiltonianNeuralNetwork(NeuralNetworkArchitecture<T>, int, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?)

public HamiltonianNeuralNetwork(NeuralNetworkArchitecture<T> architecture, int stateDim, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null)

Parameters

architecture NeuralNetworkArchitecture<T>
stateDim int
optimizer IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>

Properties

SupportsTraining

Indicates whether this model supports training.

public override bool SupportsTraining { get; }

Property Value

bool

Methods

Backward(Tensor<T>)

Performs a backward pass through the network to calculate gradients.

public Tensor<T> Backward(Tensor<T> outputGradient)

Parameters

outputGradient Tensor<T>

The gradient of the loss with respect to the network's output.

Returns

Tensor<T>

The gradient of the loss with respect to the network's input.

ComputeHamiltonian(T[])

Computes the Hamiltonian (energy) for a given state.

public T ComputeHamiltonian(T[] state)

Parameters

state T[]

State vector [q₁, ..., qₙ, p₁, ..., pₙ].

Returns

T

Hamiltonian value (energy).

ComputeTimeDerivative(T[])

Computes the time derivative of the state using Hamilton's equations.

public T[] ComputeTimeDerivative(T[] state)

Parameters

state T[]

Current state [q, p].

Returns

T[]

Time derivative [dq/dt, dp/dt].

Remarks

For Beginners: This is where the physics happens! Instead of predicting the derivative directly, we:

  1. Compute H(q,p) using the network
  2. Compute ∂H/∂p (derivative of H with respect to momentum)
  3. Compute ∂H/∂q (derivative of H with respect to position)
  4. Apply Hamilton's equations: dq/dt = ∂H/∂p, dp/dt = -∂H/∂q

This guarantees that energy is conserved!

CreateNewInstance()

Creates a new instance with the same configuration.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

New Hamiltonian network instance.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes Hamiltonian-specific data.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

Binary reader.

Forward(Tensor<T>)

Performs a forward pass through the network.

public Tensor<T> Forward(Tensor<T> input)

Parameters

input Tensor<T>

Input tensor for evaluation.

Returns

Tensor<T>

Network output tensor.

GetModelMetadata()

Gets metadata about the Hamiltonian network.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

Model metadata.

InitializeLayers()

Initializes the layers of the neural network based on the architecture.

protected override void InitializeLayers()

Remarks

For Beginners: This method sets up all the layers in your neural network according to the architecture you've defined. It's like assembling the parts of your network before you can use it.

Predict(Tensor<T>)

Makes a prediction using the Hamiltonian network.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

Input tensor.

Returns

Tensor<T>

Predicted output tensor.

SerializeNetworkSpecificData(BinaryWriter)

Serializes Hamiltonian-specific data.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

Binary writer.

Simulate(T[], T, int)

Simulates the system forward in time.

public T[,] Simulate(T[] initialState, T dt, int numSteps)

Parameters

initialState T[]

Initial state [q₀, p₀].

dt T

Time step.

numSteps int

Number of time steps.

Returns

T[,]

Trajectory [numSteps + 1, stateDim].

Train(Tensor<T>, Tensor<T>)

Trains the Hamiltonian neural network using the provided input and expected output.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

The input tensor for training (state vectors [q, p]).

expectedOutput Tensor<T>

The expected output tensor (Hamiltonian values).

Remarks

For Beginners: This method trains the network to predict the correct Hamiltonian (energy) for given state vectors. The training follows the standard neural network pattern: forward pass → loss calculation → backward pass → parameter update.

UpdateParameters(Vector<T>)

Updates the network parameters from a flattened vector.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

Parameter vector.