PhysicsNeMo Sym Loss#
physicsnemo.sym.loss.loss#
- class physicsnemo.sym.loss.loss.CausalLossNorm(ord: int = 2, eps: float = 1.0, n_chunks=10)[source]#
Bases:
LossCausal loss function for pointwise data Computes the p-th order loss of each output tensor
- Parameters:
ord (int) – Order of the loss. For example, ord=2 would be the L2 loss.
eps (float) – Causal parameter determining the slopeness of the temporal weights. “eps=1.0” would be default value.
n_chunks (int) – Number of chunks splitting the temporal domain evenly.
- forward(
- invar: Dict[str, Tensor],
- pred_outvar: Dict[str, Tensor],
- true_outvar: Dict[str, Tensor],
- lambda_weighting: Dict[str, Tensor],
- step: int,
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class physicsnemo.sym.loss.loss.DecayedIntegralLossNorm(
- start_ord: int = 2,
- end_ord: int = 1,
- decay_steps: int = 1000,
- decay_rate: float = 0.95,
Bases:
DecayedLossNormLoss function for integral data where the norm of the loss is decayed from a start value to an end value.
- Parameters:
start_ord (int) – Order of the loss when current iteration is zero.
end_ord (int) – Order of the loss to decay to.
decay_steps (int) – Number of steps to take for each decay_rate.
decay_rate – The rate of decay from start_ord to end_ord. The current ord will be given by ord = start_ord - (start_ord - end_ord) * (1.0 - decay_rate**(current_step / decay_steps)).
- forward(
- list_invar: List[Dict[str, Tensor]],
- list_pred_outvar: List[Dict[str, Tensor]],
- list_true_outvar: List[Dict[str, Tensor]],
- list_lambda_weighting: List[Dict[str, Tensor]],
- step: int,
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class physicsnemo.sym.loss.loss.DecayedLossNorm(
- start_ord: int = 2,
- end_ord: int = 1,
- decay_steps: int = 1000,
- decay_rate: float = 0.95,
Bases:
LossBase class for decayed loss norm
- class physicsnemo.sym.loss.loss.DecayedPointwiseLossNorm(
- start_ord: int = 2,
- end_ord: int = 1,
- decay_steps: int = 1000,
- decay_rate: float = 0.95,
Bases:
DecayedLossNormLoss function for pointwise data where the norm of the loss is decayed from a start value to an end value.
- Parameters:
start_ord (int) – Order of the loss when current iteration is zero.
end_ord (int) – Order of the loss to decay to.
decay_steps (int) – Number of steps to take for each decay_rate.
decay_rate – The rate of decay from start_ord to end_ord. The current ord will be given by ord = start_ord - (start_ord - end_ord) * (1.0 - decay_rate**(current_step / decay_steps)).
- forward(
- invar: Dict[str, Tensor],
- pred_outvar: Dict[str, Tensor],
- true_outvar: Dict[str, Tensor],
- lambda_weighting: Dict[str, Tensor],
- step: int,
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class physicsnemo.sym.loss.loss.IntegralLossNorm(ord: int = 2)[source]#
Bases:
LossL-p loss function for integral data Computes the p-th order loss of each output tensor
- Parameters:
ord (int) – Order of the loss. For example, ord=2 would be the L2 loss.
- forward(
- list_invar: List[Dict[str, Tensor]],
- list_pred_outvar: List[Dict[str, Tensor]],
- list_true_outvar: List[Dict[str, Tensor]],
- list_lambda_weighting: List[Dict[str, Tensor]],
- step: int,
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class physicsnemo.sym.loss.loss.Loss[source]#
Bases:
ModuleBase class for all loss functions
- forward(
- invar: Dict[str, Tensor],
- pred_outvar: Dict[str, Tensor],
- true_outvar: Dict[str, Tensor],
- lambda_weighting: Dict[str, Tensor],
- step: int,
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class physicsnemo.sym.loss.loss.LossL2(*args, **kwargs)[source]#
Bases:
Function- static backward(ctx, grad_output)[source]#
Define a formula for differentiating the operation with backward mode automatic differentiation.
This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the
vjpfunction.)It must accept a context
ctxas the first argument, followed by as many outputs as theforward()returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_gradas a tuple of booleans representing whether each input needs gradient. E.g.,backward()will havectx.needs_input_grad[0] = Trueif the first input toforward()needs gradient computed w.r.t. the output.
- static forward(
- ctx,
- pred_outvar: Tensor,
- true_outvar: Tensor,
- lambda_weighting: Tensor,
- area: Tensor,
Define the forward of the custom autograd Function.
This function is to be overridden by all subclasses. There are two ways to define forward:
Usage 1 (Combined forward and ctx):
@staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
See combining-forward-context for more details
Usage 2 (Separate forward and ctx):
@staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass
The forward no longer accepts a ctx argument.
Instead, you must also override the
torch.autograd.Function.setup_context()staticmethod to handle setting up thectxobject.outputis the output of the forward,inputsare a Tuple of inputs to the forward.See extending-autograd for more details
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()if they are intended to be used inbackward(equivalently,vjp) orctx.save_for_forward()if they are intended to be used for injvp.
- class physicsnemo.sym.loss.loss.PointwiseLossNorm(ord: int = 2)[source]#
Bases:
LossL-p loss function for pointwise data Computes the p-th order loss of each output tensor
- Parameters:
ord (int) – Order of the loss. For example, ord=2 would be the L2 loss.
- forward(
- invar: Dict[str, Tensor],
- pred_outvar: Dict[str, Tensor],
- true_outvar: Dict[str, Tensor],
- lambda_weighting: Dict[str, Tensor],
- step: int,
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
physicsnemo.sym.loss.aggregator#
- class physicsnemo.sym.loss.aggregator.Aggregator(params, num_losses, weights)[source]#
Bases:
ModuleBase class for loss aggregators
- class physicsnemo.sym.loss.aggregator.GradNorm(params, num_losses, alpha=1.0, weights=None)[source]#
Bases:
AggregatorGradNorm for loss aggregation Reference: “Chen, Z., Badrinarayanan, V., Lee, C.Y. and Rabinovich, A., 2018, July. Gradnorm: Gradient normalization for adaptive loss balancing in deep multitask networks. In International Conference on Machine Learning (pp. 794-803). PMLR.”
- class physicsnemo.sym.loss.aggregator.HomoscedasticUncertainty(params, num_losses, weights=None)[source]#
Bases:
AggregatorHomoscedastic task uncertainty for loss aggregation Reference: “Reference: Kendall, A., Gal, Y. and Cipolla, R., 2018. Multi-task learning using uncertainty to weigh losses for scene geometry and semantics. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 7482-7491).”
- class physicsnemo.sym.loss.aggregator.LRAnnealing(
- params,
- num_losses,
- update_freq=1,
- alpha=0.01,
- ref_key=None,
- eps=1e-08,
- weights=None,
Bases:
AggregatorLearning rate annealing for loss aggregation References: “Wang, S., Teng, Y. and Perdikaris, P., 2020. Understanding and mitigating gradient pathologies in physics-informed neural networks. arXiv preprint arXiv:2001.04536.”, and “Jin, X., Cai, S., Li, H. and Karniadakis, G.E., 2021. NSFnets (Navier-Stokes flow nets): Physics-informed neural networks for the incompressible Navier-Stokes equations. Journal of Computational Physics, 426, p.109951.”
- forward(
- losses: Dict[str, Tensor],
- step: int,
Weights and aggregates the losses using the learning rate annealing algorithm
- Parameters:
losses (Dict[str, torch.Tensor]) – A dictionary of losses.
step (int) – Optimizer step.
- Returns:
loss – Aggregated loss.
- Return type:
torch.Tensor
- class physicsnemo.sym.loss.aggregator.NTK(run_per_step: int = 1000, save_name: str | None = None)[source]#
Bases:
Module- forward(constraints, ntk_weights, step)[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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class physicsnemo.sym.loss.aggregator.Relobralo(
- params,
- num_losses,
- alpha=0.95,
- beta=0.99,
- tau=1.0,
- eps=1e-08,
- weights=None,
Bases:
AggregatorRelative loss balancing with random lookback Reference: “Bischof, R. and Kraus, M., 2021. Multi-Objective Loss Balancing for Physics-Informed Deep Learning. arXiv preprint arXiv:2110.09813.”
- class physicsnemo.sym.loss.aggregator.ResNorm(params, num_losses, alpha=1.0, weights=None)[source]#
Bases:
AggregatorResidual normalization for loss aggregation Contributors: T. Nandi, D. Van Essendelft, M. A. Nabian
- class physicsnemo.sym.loss.aggregator.SoftAdapt(params, num_losses, eps=1e-08, weights=None)[source]#
Bases:
AggregatorSoftAdapt for loss aggregation Reference: “Heydari, A.A., Thompson, C.A. and Mehmood, A., 2019. Softadapt: Techniques for adaptive loss weighting of neural networks with multi-part loss functions. arXiv preprint arXiv: 1912.12355.”
- forward(
- losses: Dict[str, Tensor],
- step: int,
Weights and aggregates the losses using the original variant of the softadapt algorithm
- Parameters:
losses (Dict[str, torch.Tensor]) – A dictionary of losses.
step (int) – Optimizer step.
- Returns:
loss – Aggregated loss.
- Return type:
torch.Tensor
- class physicsnemo.sym.loss.aggregator.Sum(params, num_losses, weights=None)[source]#
Bases:
AggregatorLoss aggregation by summation