Loss¶
The loss module provides standard and dynamic loss functions for training.
MSE and compatibility¶
loss: mse is the default. It computes squared error in the model's normalized
units, averages over wet cells and batch entries separately for each channel,
then the training step averages the channels equally. It does not use cell-area
weights. Channels with fewer wet cells receive the same coefficient as other
channels. An all-dry channel returns zero loss and zero gradients. Nonfinite
land values are ignored; nonfinite wet values remain visible in the loss.
loss: legacy_mse preserves the previous reduction: zero land values, then
average over the full rectangular grid. Its channel loss equals the wet-cell
MSE multiplied by the channel's wet-cell fraction. Use this option to reproduce
old training objectives or resume a run without changing its loss reduction.
Existing configurations containing mse now select the wet-cell reduction.
When resuming, set loss: legacy_mse in the training configuration passed to
the new invocation; the checkpoint does not select the loss implementation.
For a dynamic loss, set loss.metric: legacy_mse in that configuration to
preserve the old base reduction.
Loss values from the two reductions are not directly comparable.
Dynamic Loss¶
The DynamicLoss class implements the variance-weighted loss used in Samudra 2. It maintains per-channel scaling weights updated via an exponential moving average of inverse prediction error:
- Channels with lower running error receive larger inverse-error weights, balancing channel contributions
-
A configurable
limitparameter clamps the ratio between channel weights; the shipped presets use 20, while the class default is no limit -
Uses a rolling window of 25 steps to smooth scale estimates
Configuration (choose one alternative):
API Reference¶
samudra.utils.loss
¶
DynamicLoss(loss_fn, *, limit, device, num_channels)
¶
A loss function that scales each channel to contribute equally to the loss.
This uses a rolling estimate of the loss of each channel to scale each channel's loss, discouraging the model from focusing on only a few channels.
See: https://openathena.slack.com/archives/C08CYM42DT3/p1752275713570969
Source code in src/samudra/utils/loss.py
N_WINDOW = 25
class-attribute
instance-attribute
¶
Rolling window size to average over. (~number of steps)
update(loss_per_channel)
¶
Given the unscaled per-channel loss, update the per-channel scale.
Source code in src/samudra/utils/loss.py
state_dict()
¶
load_state_dict(state)
¶
GradientLoss(loss_fn, *, gradient_weight, pad_mode)
¶
Combine a base loss with a gradient matching penalty.
Applies the provided per-channel loss metric then adds an L1 penalty on
spatial gradients, scaled by gradient_weight.
Source code in src/samudra/utils/loss.py
wet_cell_mse(pred, target, ctx)
¶
Per-channel MSE over wet batch/grid entries, with no cell-area weighting.
All-dry channels return zero with zero gradients. Mask before subtraction so nonfinite land values cannot contaminate the loss or its gradients. Accumulate low-precision inputs in float32 to avoid overflowing cell counts or squared-error sums on large grids.
Source code in src/samudra/utils/loss.py
decomposed_mse(pred, target)
¶
Unmasked per-channel MSE; legacy_mse uses this after zeroing land.
decomposed_mae(pred, target)
¶
decomposed_mse_diff_weighted(pred, target)
¶
MSE loss with weighted differences.
Source code in src/samudra/utils/loss.py
decomposed_mse_scaled(pred, target, scaling)
¶
MSE loss with scaled residuals.
Source code in src/samudra/utils/loss.py
decomposed_mse_mae(pred, target)
¶
Combined MSE and MAE loss.
Source code in src/samudra/utils/loss.py
gradient_l1_loss(pred, target, pad_mode)
¶
L1 loss on spatial gradients, averaged per channel.
Source code in src/samudra/utils/loss.py
decomposed_mae_gradient_weighted(pred, target, gradient_weight, pad_mode='constant')
¶
MAE loss with spatial gradient matching penalty.