neuralop.losses
.LpLoss
- class neuralop.losses.LpLoss(d=1, p=2, measure=1.0, reduction='sum')[source]
LpLoss provides the L-p norm between two discretized d-dimensional functions. Note that LpLoss always averages over the spatial dimensions.
Note
In function space, the Lp norm is an integral over the entire domain. To ensure the norm converges to the integral, we scale the matrix norm by quadrature weights along each spatial dimension.
If no quadrature is passed at a call to LpLoss, we assume a regular discretization and take
1 / measure
as the quadrature weights.- Parameters:
- dint, optional
dimension of data on which to compute, by default 1
- pint, optional
order of L-norm, by default 2 L-p norm: [sum_{i=0}^n (x_i - y_i)**p] ** (1/p)
- measurefloat or list, optional
measure of the domain, by default 1.0 either single scalar for each dim, or one per dim
To perform quadrature,
LpLoss
scalesmeasure
by the size of each spatial dimension ofx
, and multiplies them with ||x-y||, such that the final norm is a scaled average over the spatial dimensions ofx
.- reductionstr, optional
whether to reduce across the batch and channel dimensions by summing (‘sum’) or averaging (‘mean’)
Warning
LpLoss
always reduces over the spatial dimensions according toself.measure
. reduction only applies to the batch and channel dimensions.
- Attributes:
- name
Methods
__call__
(y_pred, y, **kwargs)Call self as a function.
abs
(x, y[, quadrature])absolute Lp-norm
reduce_all
(x)reduce x across the batch according to self.reduction
rel
(x, y)rel: relative LpLoss computes ||x-y||/||y||
uniform_quadrature creates quadrature weights scaled by the spatial size of
x
to ensure thatLpLoss
computes the average over spatial dims.Examples
- uniform_quadrature(x)[source]
uniform_quadrature creates quadrature weights scaled by the spatial size of
x
to ensure thatLpLoss
computes the average over spatial dims.- Parameters:
- xtorch.Tensor
input data
- Returns:
- quadraturelist
list of quadrature weights per-dim
- reduce_all(x)[source]
reduce x across the batch according to self.reduction
- abs(x, y, quadrature=None)[source]
absolute Lp-norm
- Parameters:
- xtorch.Tensor
inputs
- ytorch.Tensor
targets
- quadraturefloat or list, optional
quadrature weights for integral either single scalar or one per dimension
- rel(x, y)[source]
rel: relative LpLoss computes ||x-y||/||y||
- Parameters:
- xtorch.Tensor
inputs
- ytorch.Tensor
targets