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
TThe numeric type used for calculations.
- Inheritance
-
LagrangianNeuralNetwork<T>
- Implements
- 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:
- Neural network learns L(q, q̇)
- Compute ∂L/∂q and ∂L/∂q̇ using automatic differentiation
- Apply Euler-Lagrange equation to get acceleration q̈
- 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
architectureNeuralNetworkArchitecture<T>configurationDimintoptimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>
Properties
SupportsTraining
Indicates whether this model supports training.
public override bool SupportsTraining { get; }
Property Value
Methods
Backward(Tensor<T>)
Performs a backward pass through the network to calculate gradients.
public Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<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
qT[]qDotT[]
Returns
- T[]
ComputeLagrangian(T[], T[])
Computes the Lagrangian L(q, q̇) = T - V.
public T ComputeLagrangian(T[] q, T[] qDot)
Parameters
qT[]qDotT[]
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
readerBinaryReaderBinary reader.
Forward(Tensor<T>)
Performs a forward pass through the network.
public Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<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
inputTensor<T>Input tensor.
Returns
- Tensor<T>
Predicted output tensor.
SerializeNetworkSpecificData(BinaryWriter)
Serializes Lagrangian-specific data.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriterBinary 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
inputTensor<T>The input tensor for training (state vectors [q, q̇]).
expectedOutputTensor<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
parametersVector<T>Parameter vector.