API — nefi.losses¶
Generated by scripts/gen_api_docs.py — do not edit by hand. Index
nefi.losses¶
Loss terms and their composition.
nefi.losses.base¶
Loss composition: a :class:Context per step, :class:Loss terms, and a weighted :class:LossSet.
class Context(fields: dict[str, torch.Tensor], pred: torch.Tensor, obs: Measurement, domain: Domain, operator: Operator | None = None, field_module: Field | None = None, ...¶
Everything a loss term may look at during one optimization step.
field(self, name: str | None = None) -> torch.Tensor
class Loss(name: str | None = None)¶
A scalar loss term. is_data marks data-fidelity terms (used for stopping / restarts).
forward(self, ctx: Context) -> torch.Tensor
class LossSet(losses: Mapping[str, Loss | dict | str] | Sequence[Loss], weights: Mapping[str, float] | None = None)¶
Weighted sum of named loss terms with per-stage weight overrides.
auto_balance(self, ctx: Context, target: float = 1.0, only: Sequence[str] | None = None)— Rescale weights so each active term contributestargetat the current iterate.data_loss(self, comps: Mapping[str, float]) -> float— Weighted sum of the data-fidelity components of acompsdict.data_terms(self) -> tuple[str, ...]forward(self, ctx: Context) -> tuple[torch.Tensor, dict[str, float]]forward_tensors(self, ctx: Context) -> tuple[torch.Tensor, dict[str, torch.Tensor]]— Total loss and detached tensor components (no device synchronization).names(property)with_weights(self, overrides: Mapping[str, float] | None) -> LossSet— Shallow copy sharing the loss modules with (partially) overridden weights.
nefi.losses.data¶
Data-fidelity terms. All reduce by mean over observed entries so weights are resolution-free.
class DataLoss(name: str | None = None, noise_aware: bool = False)¶
residual_weight(self, ctx: Context) -> torch.Tensor | float
class Huber(delta: float = 1.0, name: str | None = None, noise_aware: bool = False)¶
Robust fidelity (quadratic below delta, linear above) for outlier-contaminated data.
forward(self, ctx: Context) -> torch.Tensor
class L2(name: str | None = None, noise_aware: bool = False)¶
mean((pred - obs)²) over observed entries (optionally divided by noise variance).
forward(self, ctx: Context) -> torch.Tensor
class LogMSE(normalize: str = 'max', reduce_axes: Sequence[int] | None = None, eps: float = 1e-10, name: str | None = None)¶
Pixelwise log-MSE on (optionally reduced and) normalized maps — NeTMY Eq. (19).
forward(self, ctx: Context) -> torch.Tensormaps(self, ctx: Context) -> tuple[torch.Tensor, torch.Tensor]
class MSE(name: str | None = None, noise_aware: bool = False)¶
mean((pred - obs)²) over observed entries (optionally divided by noise variance).
forward(self, ctx: Context) -> torch.Tensor
class NormalizedMSE(normalize: str = 'mean', reduce_axes: Sequence[int] | None = None, eps: float = 1e-12, name: str | None = None)¶
MSE between reduced maps normalized by mean (NeTMY R_nm) or max/sum.
forward(self, ctx: Context) -> torch.Tensor
class PoissonNLL(eps: float = 1e-08, name: str | None = None)¶
Poisson negative log-likelihood for count data (pred = expected counts > 0).
forward(self, ctx: Context) -> torch.Tensor
class RelativeMSE(name: str | None = None, noise_aware: bool = False)¶
‖pred - obs‖² / ‖obs‖² — scale-free fidelity.
forward(self, ctx: Context) -> torch.Tensor
class RMSE(name: str | None = None, noise_aware: bool = False)¶
forward(self, ctx: Context) -> torch.Tensor
nefi.losses.physics¶
Physics-knowledge losses: soft PDE residuals, conservation, symmetry, support, monotonicity.
class Conservation(field: str | None = None, total: float = 1.0, kind: str = 'sum', relative: bool = True, name: str | None = None)¶
Penalize deviation of the field's total from a known value (mass / charge / energy).
current(self, ctx: Context) -> torch.Tensorforward(self, ctx: Context) -> torch.Tensor
class GradientL2(field: str | None = None, periodic_axes: Sequence[int] = (), name: str | None = None)¶
First-order Tikhonov (H¹ seminorm) mean |∇x|² with physical spacing.
forward(self, ctx: Context) -> torch.Tensor
class KnownSupportLoss(field: str | None = None, mask: torch.Tensor | None = None, fill: float = 0.0, name: str | None = None)¶
Soft known support: mean over the exterior of (x − fill)² (mask 1 = inside).
forward(self, ctx: Context) -> torch.Tensor
class Monotone(field: str | None = None, axis: int = 0, direction: str = 'increasing', name: str | None = None)¶
Penalize violations of monotonicity along an axis: mean relu(∓∂x/∂x_axis)².
forward(self, ctx: Context) -> torch.Tensor
class PDEResidual(residual_fn: Callable[[dict[str, torch.Tensor], Context], torch.Tensor], fields: Sequence[str] | None = None, name: str | None = None, normalize: Callable[[...¶
Soft PDE residual penalty mean(R(fields)²) — for problems without a hard solver.
forward(self, ctx: Context) -> torch.Tensor
class SymmetryLoss(field: str | None = None, kind: str = 'mirror_x', axes: Sequence[int] | None = None, n_bins: int | None = None, name: str | None = None)¶
Soft symmetry: mean (x − S x)² for a mirror (flip about the grid center) or radial symmetry (S x = radial shell average). Zero iff the field has the symmetry.
forward(self, ctx: Context) -> torch.Tensor
def gradient(x: torch.Tensor, spacing: Sequence[float], axis: int, scheme: str = 'central') -> torch.Tensor¶
Finite-difference ∂x/∂x_axis on a grid (same shape; one-sided at the boundaries).
def laplacian(x: torch.Tensor, spacing: Sequence[float], periodic_axes: Sequence[int] = ()) -> torch.Tensor¶
Five-point (2-D) / seven-point (3-D) Laplacian with replicate (Neumann) or periodic edges.
nefi.losses.reg¶
Regularizers on fields. Gradients use physical spacing so weights transfer across resolutions.
class FieldLoss(field: str | None = None, name: str | None = None)¶
value(self, ctx: Context) -> torch.Tensor
class L1(field: str | None = None, name: str | None = None)¶
mean |x| — sparsity prior for point-like sources (NeTMY Eq. 3).
forward(self, ctx: Context) -> torch.Tensor
class Laplacian(field: str | None = None, periodic_axes: Sequence[int] = (), use_spacing: bool = True, name: str | None = None)¶
mean (Δx)² — second-order smoothness; suppresses axis-aligned cross artifacts (NeTMY App. E.10) at the price of slightly blurrier peaks.
forward(self, ctx: Context) -> torch.Tensor
class PriorMSE(prior: torch.Tensor, field: str | None = None, name: str | None = None)¶
mean (x - x_prior)² towards a reference field (warm start / external prior).
forward(self, ctx: Context) -> torch.Tensor
class RangePenalty(lo: float | None = 0.0, hi: float | None = None, field: str | None = None, name: str | None = None)¶
Soft box constraint mean relu(lo - x)² + relu(x - hi)² (for grids without bounded heads).
forward(self, ctx: Context) -> torch.Tensor
class Tikhonov(field: str | None = None, name: str | None = None)¶
mean x² (ridge).
forward(self, ctx: Context) -> torch.Tensor
class TV(field: str | None = None, isotropic: bool = True, eps: float = 1e-06, periodic_axes: Sequence[int] = (), use_spacing: bool = True, name: str | None = None)¶
Total variation.
forward(self, ctx: Context) -> torch.Tensor
def forward_differences(x: torch.Tensor, spacing: Sequence[float] | None = None, periodic_axes: Sequence[int] = ()) -> list[torch.Tensor]¶
Per-axis forward differences of a (*shape) field, same shape as x.