Class PhysicsInformedLoss<T>
- Namespace
- AiDotNet.PhysicsInformed
- Assembly
- AiDotNet.dll
Loss function for Physics-Informed Neural Networks (PINNs). Combines data loss, PDE residual loss, boundary condition loss, and initial condition loss.
public class PhysicsInformedLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
PhysicsInformedLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Traditional neural networks learn from data alone. Physics-Informed Neural Networks (PINNs) additionally enforce that the solution satisfies physical laws (PDEs) and constraints.
This loss function has multiple components:
- Data Loss: Measures how well predictions match observed data points
- PDE Residual Loss: Measures how much the PDE is violated at collocation points
- Boundary Loss: Ensures boundary conditions are satisfied
- Initial Condition Loss: Ensures initial conditions are satisfied (for time-dependent problems)
The total loss is a weighted sum: L_total = λ_data * L_data + λ_pde * L_pde + λ_bc * L_bc + λ_ic * L_ic
Why This Works: By minimizing this loss, the network learns to:
- Fit the available data
- Obey physical laws everywhere (not just at data points)
- Satisfy boundary and initial conditions This often requires far less data than traditional deep learning!
Key Innovation: PINNs can solve PDEs in regions where we have NO data, as long as the physics is known.
Constructors
PhysicsInformedLoss(IPDESpecification<T>?, IBoundaryCondition<T>[]?, IInitialCondition<T>?, double?, double?, double?, double?)
Initializes a new instance of the Physics-Informed loss function.
public PhysicsInformedLoss(IPDESpecification<T>? pdeSpecification = null, IBoundaryCondition<T>[]? boundaryConditions = null, IInitialCondition<T>? initialCondition = null, double? dataWeight = null, double? pdeWeight = null, double? boundaryWeight = null, double? initialWeight = null)
Parameters
pdeSpecificationIPDESpecification<T>The PDE that the solution must satisfy.
boundaryConditionsIBoundaryCondition<T>[]Boundary conditions for the problem.
initialConditionIInitialCondition<T>Initial condition for time-dependent problems.
dataWeightdouble?Weight for data loss component.
pdeWeightdouble?Weight for PDE residual loss component.
boundaryWeightdouble?Weight for boundary condition loss.
initialWeightdouble?Weight for initial condition loss.
Properties
Name
Gets the name of the loss function.
public string Name { get; }
Property Value
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the derivative (gradient) of the loss function.
public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values from the model.
actualVector<T>The actual (target) values.
Returns
- Vector<T>
A vector containing the derivatives of the loss with respect to each prediction.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the loss between predicted and actual values.
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values from the model.
actualVector<T>The actual (target) values.
Returns
- T
The loss value.
ComputeDerivative(T[], T[])
Computes the derivative of the loss with respect to predictions. Required by the ILossFunction interface.
public T[] ComputeDerivative(T[] predictions, T[] targets)
Parameters
predictionsT[]targetsT[]
Returns
- T[]
ComputeLoss(T[], T[]?, PDEDerivatives<T>, T[])
Computes the total physics-informed loss (compatibility wrapper).
public T ComputeLoss(T[] predictions, T[]? targets, PDEDerivatives<T> derivatives, T[] inputs)
Parameters
predictionsT[]targetsT[]derivativesPDEDerivatives<T>inputsT[]
Returns
- T
Remarks
This overload is domain-specific and intentionally separate from ILossFunction<T>. Prefer ComputePhysicsLoss(T[], T[]?, PDEDerivatives<T>, T[]) for clarity.
ComputePhysicsLoss(T[], T[]?, PDEDerivatives<T>, T[])
Computes the total physics-informed loss for PINN training.
public T ComputePhysicsLoss(T[] predictions, T[]? targets, PDEDerivatives<T> derivatives, T[] inputs)
Parameters
predictionsT[]Network predictions.
targetsT[]Target values (may be null if no data available).
derivativesPDEDerivatives<T>Derivatives needed for PDE computation.
inputsT[]Input points where predictions were made.
Returns
- T
The total loss value.
ComputePhysicsLossGradients(T[], T[]?, PDEDerivatives<T>, T[])
Computes the physics-informed loss and its gradients with respect to outputs and derivatives.
public PhysicsLossGradient<T> ComputePhysicsLossGradients(T[] predictions, T[]? targets, PDEDerivatives<T> derivatives, T[] inputs)
Parameters
predictionsT[]Network predictions.
targetsT[]Target values (may be null if no data available).
derivativesPDEDerivatives<T>Derivatives needed for PDE computation.
inputsT[]Input points where predictions were made.
Returns
- PhysicsLossGradient<T>
The loss and gradients for this sample.