Reconfigure the unified focal loss hyperparameters at specified epochs during training.
Arguments
- loss
An instantiated
defineUnifiedFocalLossordefineUnifiedFocalLossDSobject. Must be the same object passed aslosstoluz::setup().- schedule
A list of named lists, each specifying one reconfiguration event. Each entry must contain:
epochSingle integer. The training epoch at the end of which the parameters are updated. The new values take effect from the following epoch onward.
paramsNamed list of parameter name–value pairs to update. Names must match those listed in the Details section above.
Details
A luz callback that updates one or more parameters of an instantiated
defineUnifiedFocalLoss or defineUnifiedFocalLossDS loss object at defined
epoch boundaries. Because torch nn_module objects are reference types, the
same object passed here is used by the luz training loop, so changes applied by the
callback take effect immediately for the following epoch.
For defineUnifiedFocalLossDS, parameters are routed to the inner
defineUnifiedFocalLoss sub-object automatically.
Parameters that have pre-computed tensor counterparts (see Details) have both the scalar field and the corresponding tensor updated atomically so that the forward pass always reads consistent values.
The following parameters may be reconfigured:
lambdaRelative weight of distribution vs. region loss.
gammaFocal weighting strength.
deltaFalse positive / false negative balance (scalar or per-class vector).
clsWghtsDistClass weights for the distribution loss (scalar or per-class vector).
clsWghtsRegClass weights for the region loss (scalar or per-class vector).
smoothSmoothing constant for numeric stability.
distThresholdDistance threshold for edge weighting.
minWghtMinimum per-pixel edge weight.
maxWghtMaximum per-pixel edge weight.
useLogCosHToggle log-cosh transform on the region loss.
labelSmoothingLabel smoothing factor for the distribution loss.
classAvgDistSwitch between macro-averaged and pixel-level distribution loss.
prioritizeEdgeFlip which pixels receive the higher edge weight.
edgeModeEdge weighting mode:
"threshold","linear", or"gaussian".applyDistWghtWhich loss component receives edge weights:
"distribution","region", or"both".cropFactorMskMask crop factor.
cropFactorPredPrediction crop factor.
zeroStartWhether class indices start at 0.
Examples
if (FALSE) { # \dontrun{
loss_fn <- defineUnifiedFocalLoss(nCls = 3, lambda = 0.5, gamma = 0.5, delta = 0.6)
reconfigure_cb <- callback_reconfigure_loss(
loss = loss_fn,
schedule = list(
list(epoch = 5, params = list(lambda = 0.7, gamma = 0.3)),
list(epoch = 10, params = list(lambda = 0.9, clsWghtsDist = c(1, 2, 3))),
list(epoch = 15, params = list(labelSmoothing = 0.1))
)
)
fitted <- model |>
luz::setup(loss = loss_fn, optimizer = torch::optim_adamw) |>
luz::fit(train_dl, epochs = 20,
callbacks = list(reconfigure_cb))
} # }
