Loss

Loss function aggregators that create physics-informed loss functions from the list of defined objective terms and constraints. It also provides probabilistic loss functions for likelihood-based learning.

Currently supported loss functions:

Constraint optimization losses: * PenaltyLoss * BarrierLoss * AugmentedLagrangeLoss

Probabilistic losses: * GPPHSLoss

class neuromancer.loss.AggregateLoss(objectives, constraints)[source]

Abstract aggregate loss class for calculating constraints, objectives, and aggegate loss values.

calculate_constraints(input_dict)[source]

Calculate the values of constraints and constraints violations

calculate_objectives(input_dict)[source]

Calculate the value of the objective function for SGD

abstractmethod forward(input_dict)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class neuromancer.loss.AugmentedLagrangeLoss(objectives, constraints, train_data, inner_loop=10, sigma=2.0, mu_max=1000.0, mu_init=0.001, eta=1.0)[source]
Augmented Lagrangian method loss function.

https://en.wikipedia.org/wiki/Augmented_Lagrangian_method

forward(input_dict)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class neuromancer.loss.BarrierLoss(objectives, constraints, barrier='log10', upper_bound=1.0, shift=1.0, alpha=0.5)[source]

Barrier loss function. * https://en.wikipedia.org/wiki/Barrier_function Available barrier functions are defined in the self.barriers dictionary. References for relaxed barrier functions: * https://arxiv.org/abs/1602.01321 * https://arxiv.org/abs/1904.04205v2 * https://ieeexplore.ieee.org/document/7493643/

calculate_constraints(input_dict)[source]
Calculate the magnitudes of constraint violations via log barriers

cviolation > 0 -> penalty cviolation <= 0 -> barrier

class neuromancer.loss.GPPHSLoss(model, likelihood)[source]

NLML loss for the GP-PHS model. Computes the negative marginal log-likelihood directly via Cholesky:

NLML = 0.5 · rᵀ(K+σ²I)⁻¹r + 0.5 · log|K+σ²I| + const

where r = ẋ_flat - μ(x,u) is the residual over all N·nx observations.

GPyTorch’s ExactMarginalLogLikelihood cannot be used here because ExactGP assumes N inputs → N scalar outputs, but PHS has N inputs → N·nx outputs (the noise would be (N,N) vs the kernel (N·nx, N·nx)). Args:

model : GPPHSModel instance likelihood : gpytorch.likelihoods.GaussianLikelihood instance

Usage:

loss_fn = GPPHSLoss(model, likelihood) loss = loss_fn(x, u, xdot) loss.backward()

forward(x: Tensor, u: Tensor, xdot: Tensor, xdot_var: Tensor = None) Tensor[source]

Compute negative NLML loss. Args:

x : (N, nx) state u : (N, nu) control input xdot : (N·nx,) or (N, nx) state derivatives xdot_var : (N, nx) derivative variances from gp_smoother (Δ diagonal).

If None, falls back to likelihood.noise * I.

Returns:

scalar loss — minimizing this maximizes the marginal likelihood

class neuromancer.loss.PenaltyLoss(objectives, constraints)[source]
Penalty loss function.

https://en.wikipedia.org/wiki/Penalty_method

forward(input_dict)[source]
Parameters:

input_dict – (dict {str: torch.Tensor}) Values from forward pass calculations

Returns:

(dict {str: torch.Tensor}) input_dict appended with calculated loss values

neuromancer.loss.get_loss(objectives, constraints, train_data, args)[source]