Table of Contents

Class LagrangianNeuralNetwork<T>

Namespace
AiDotNet.PhysicsInformed.ScientificML
Assembly
AiDotNet.dll

Implements Lagrangian Neural Networks (LNN) for learning mechanical systems.

public class LagrangianNeuralNetwork<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
LagrangianNeuralNetwork<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: Lagrangian Neural Networks learn physics using the Lagrangian formulation of mechanics.

Lagrangian Mechanics: Alternative to Hamiltonian mechanics, uses the Lagrangian: L(q, q̇) = T - V = Kinetic Energy - Potential Energy

Equations of Motion (Euler-Lagrange): d/dt(∂L/∂q̇) - ∂L/∂q = 0

Where:

  • q = generalized coordinates (positions)
  • q̇ = generalized velocities
  • T = kinetic energy (usually ½m q̇²)
  • V = potential energy (depends on q)

Why Lagrangian vs. Hamiltonian?

  • Lagrangian: Uses (q, q̇) - position and velocity
  • Hamiltonian: Uses (q, p) - position and momentum
  • Lagrangian often more intuitive for mechanical systems
  • Both give same physics, different formulations

How LNN Works:

  1. Neural network learns L(q, q̇)
  2. Compute ∂L/∂q and ∂L/∂q̇ using automatic differentiation
  3. Apply Euler-Lagrange equation to get acceleration q̈
  4. Guaranteed to conserve energy and satisfy principle of least action

Applications:

  • Robotics (manipulator dynamics)
  • Biomechanics (human motion)
  • Aerospace (satellite dynamics)
  • Any mechanical system

Constructors

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

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

Parameters

architecture NeuralNetworkArchitecture<T>
configurationDim 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.

ComputeAcceleration(T[], T[])

Computes acceleration using Euler-Lagrange equation.

public T[] ComputeAcceleration(T[] q, T[] qDot)

Parameters

q T[]
qDot T[]

Returns

T[]

ComputeLagrangian(T[], T[])

Computes the Lagrangian L(q, q̇) = T - V.

public T ComputeLagrangian(T[] q, T[] qDot)

Parameters

q T[]
qDot T[]

Returns

T

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 Lagrangian network instance.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes Lagrangian-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 Lagrangian 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 Lagrangian network.

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

Parameters

input Tensor<T>

Input tensor.

Returns

Tensor<T>

Predicted output tensor.

SerializeNetworkSpecificData(BinaryWriter)

Serializes Lagrangian-specific data.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

Binary writer.

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

Trains the Lagrangian 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, q̇]).

expectedOutput Tensor<T>

The expected output tensor (Lagrangian values).

Remarks

For Beginners: This method trains the network to predict the correct Lagrangian (kinetic minus potential 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.