ScalarWeighting#
Module for the Scalar Weighting.
- class ScalarWeighting(weights)[source]#
Bases:
BaseWeightingWeighting strategy based on fixed scalar coefficients.
This scheme assigns a constant multiplicative weight to each loss term, without adapting over time. The same weight can be applied to all terms, or distinct weights can be specified for individual conditions.
- Example:
>>> import torch >>> from pina.weighting import ScalarWeighting >>> # Uniform weighting >>> weighting = ScalarWeighting(weights=1.0) >>> losses = {"residual": torch.tensor(0.1), "data": torch.tensor(0.2)} >>> weighting.aggregate(losses) tensor(0.3000) >>> # Per-condition weighting >>> weighting = ScalarWeighting(weights={"residual": 0.5, "data": 2.0}) >>> weighting.aggregate(losses) tensor(0.4500)
Initialization of the
ScalarWeightingclass.- Parameters:
weights (float | int | dict) – The scalar weights associated with each loss term. It can be provided either as a single numeric value or as a dictionary. If a scalar is given, the same weight is applied to all loss terms. If a dictionary is provided, its keys represent the loss identifiers (e.g., conditions) and its values specify the corresponding weights. Loss terms not explicitly defined in the dictionary are assigned a default weight of
1.- Raises:
ValueError – If the input weights are neither numeric nor a dictionary.