Skip to main content

Callbacks

Description

Things that happen between epochs. monitor is checked against the metrics the model actually asks for, so a callback watching a number that will never arrive is refused when the specification is built rather than waited on forever.

Usage

callback_early_stopping(
  monitor = "val_loss",
  patience = 10,
  min_delta = 0,
  restore_best = TRUE,
  start = NULL,
  start_epoch = NULL
)

callback_model_checkpoint(
  path,
  monitor = "val_loss",
  save_best_only = TRUE,
  when = NULL
)

callback_reduce_lr_on_plateau(
  monitor = "val_loss",
  factor = 0.1,
  patience = 5,
  min_lr = 0
)

callback_cosine_annealing(min_lr = 0, epochs = NULL)

callback_linear_annealing(min_lr = 0, epochs = NULL)

callback_step_decay(factor = 0.1, every = NULL, milestones = NULL, min_lr = 0)

callback_csv_logger(path)

callback_ema(decay = 0.999)

callback_stop_after(hours)

callback_terminate_on_collapse(
  floor = 1e-04,
  patience = 3,
  monitor = "val_foreground"
)

callback_terminate_on_nan()

Arguments

monitor

What to watch: “val_loss”, “train_loss”, “val_” followed by a metric name such as “val_dice”, “val_foreground” and “train_foreground” - the share of the prediction that is not background - or “val_dice_worst” and its kind, the lowest any single case scored.

That last one is the number summary() on evaluate_cases() tells you to read, and until the engine’s 0.8.0a36 nothing could act on it: a model averaging 0.86 that scores 0.11 on some patient has a failure mode, and the mean is where it hides. Stopping, checkpointing and cutting the learning rate can all watch it now.

Validation only. The training data is shuffled between epochs, so a case has no identity in that pass and there is nothing honest to pool by - “train_dice_worst” is refused rather than quietly reported as something else.
patience Epochs to wait before acting. For callback_terminate_on_collapse() that is epochs in a row below the floor, and it is not 1 because an early epoch legitimately predicts almost nothing.
min_delta Improvement smaller than this does not count.
restore_best

Put the best weights back when training stops, rather than keeping the last - which are by definition the ones that were not improving. On by default. Note that callback_swa() replaces nothing if a run stops before averaging began.

An averager beside this one wins. callback_ema() and callback_swa() also replace the weights when training ends, and since engine 0.8.0a57 the order is fixed rather than left to wherever the callbacks sat in the list: the average is what the run hands back, because somebody who configured averaging asked for the averaged model and this argument is a statement about which epoch was best. Before that release the answer was whichever of the two this package happened to send last, and the run record reported the EMA steps either way. Re-read here because the engine rewrote its own sentence for this field, which is what the field-prose ledger is for.
start, start_epoch

When the callback may begin to act - one of the two and never both. start is a fraction of the run (0.1 leaves the first tenth alone) and start_epoch is an epoch number. Two arguments rather than one taking either, because a fraction survives a change to epochs and an epoch count does not, and in a YAML specification 1 and 1.0 are the same value once parsed - so one field would make "epoch one" and "the whole run" indistinguishable.

For callback_early_stopping() this is a warm-up: a model that needs fifteen epochs before its validation curve means anything would otherwise need patience > 15 for the whole run, paying that slack at the end as well. It delays the stopping and nothing else - an epoch inside the warm-up can still be the best one and restore_best still returns to it, because those are the best weights the run has if it ends for another reason.

For callback_swa() it is the point averaging begins, which is the same concept and therefore the same two words.
path

Where to write. For callback_model_checkpoint() that is the weights file; for callback_csv_logger() it is one row per epoch appended as the epoch ends - the same numbers training_history() returns, so a run that is interrupted still leaves its history behind. Re-read against the engine’s own text when it changed in 0.8.0a23, which is what the field-prose ledger is for: the engine’s sentence had named this package’s training_history() to a Python reader, and the fix there left R’s @param saying three words about two different files.

The checkpoint’s extension decides its format, since the engine’s 0.8.0a31: .safetensors is written as safetensors and anything else as a torch archive, which is what .pt has always meant. Before that the file was a torch archive whatever it was called, so a checkpoint named after the files this package publishes was written in one format and read as another - and reading now goes by the file’s contents rather than by its name, so a checkpoint written by an older engine still loads. A checkpoint is the run’s safety net; export_weights() is what writes a file for somebody else to load, with the sidecar that says what it is for.
save_best_only Only write when the watched quantity improves, which is the default. FALSE writes every epoch, which is what to use when a run may be interrupted rather than when the best epoch is what you want to keep.
when

For callback_model_checkpoint(), a named list of thresholds that must all hold before anything is written: list(val_dice = 0.9, val_loss = 0.2). The direction is derived, not configured - anything ending in loss has to be below its threshold and everything else above it, the same rule monitor uses - so there is no argument to get backwards. The names are checked against what the model reports when the specification is built, not at the first epoch.

It is an extra gate beside save_best_only, not a replacement: with both, a file is written when the watched number improves and every threshold holds. A threshold is a number about your data and does not transfer between datasets, and a run whose conditions never hold writes nothing and says so when it ends.
factor, min_lr What the rate is multiplied by, and the floor it does not go below. factor is the cut on a plateau for callback_reduce_lr_on_plateau() and the drop at each step for callback_step_decay(), where it must be below 1 - a step schedule that raises the rate is a different idea and accepting it here would hide a typo. min_lr is the floor for all four, and two of the four reach it and two do not: a plateau cut and a step drop land on it and stay, while the two annealers approach it and write it only after the last epoch unless epochs is set shorter than the run. See the cosine section.
epochs For callback_cosine_annealing() and callback_linear_annealing(), how many epochs to spread the decay over. Unset means the model’s epochs, which is what you want: a curve that ends where the run ends.
every, milestones For callback_step_decay(), one of the two and never both. every = 30 drops every thirty epochs; milestones = c(30, 60) drops at those epochs, 1-based and strictly increasing, which is how a published recipe states it. Two arguments rather than one that takes either, because 30 and c(30) are different schedules and a single argument holding either would be read as whichever was typed. A period survives a change to epochs; a list of milestones does not.
decay For callback_ema(), how much of the average survives each optimiser step - not each epoch. 0.999 is the usual value and gives a half-life of about 700 steps; it is the published number because the average is fed per step, which is what the engine’s fourth callback hook was added for. Unlike callback_swa() this needs no pass over the training data at the end: the batch-normalisation statistics are averaged alongside the weights rather than recomputed.
hours For callback_stop_after(), a wall-clock budget. An epoch cap cannot express it, because how long an epoch takes is a property of the data and the shape. Checked at the end of an epoch, so the real limit is this plus one epoch; the run’s stop reason names the clock, so a run that ran out of time does not read as one that converged.
floor For callback_terminate_on_collapse(), the share of foreground below which a prediction counts as collapsed. A floor rather than a test for zero, because 0.01% foreground against a target that is 14% foreground has collapsed in every sense that matters. The default sits far below any real target on purpose: this guards against a failure, not against a model being merely bad, and a floor near a real foreground share would end runs that were only disappointing.

Details

Whether the watched quantity should rise or fall is worked out from its name: anything ending in loss is minimised, everything else maximised.

Value

A callback specification.

The one number that is not about quality

Every epoch reports foreground as well - for both passes, train_foreground and val_foreground - which is the share of the prediction that is not background. Nothing is compared against the truth, so it is not a measure of how good the model is; it answers whether the model is still producing a segmentation at all.

It is reported whether or not you ask for it, and that is the point. A model that collapses to predicting background everywhere is invisible in the numbers it already produced: with include_background = TRUE the Dice is a mean over class channels and the background channel is nearly perfect, so a collapsed model reports around 0.5 while its loss falls contentedly. A collapse you have to opt into noticing is one you will not notice.

callback_terminate_on_collapse() acts on it, evaluate() carries it as a column, and callback_model_checkpoint()’s when can take a condition on it.

There is a twelfth, documented separately because it needs a page of its own: callback_swa() averages the weights over the last part of the run. It watches nothing and changes how reproducible a run is rather than how well it scores.

One averager per run

callback_swa() and callback_ema() both replace the model’s weights when training ends, so a run takes one of them; the engine refuses both. They answer the same observation - the last epoch’s corner is not the middle of the basin - with different weightings. swa folds the tail uniformly from a declared start; ema weights recent steps more and needs no start, because an average with a decay forgets the beginning by itself.

ema is updated after every optimiser step, which is what makes its decay mean what the literature means by it: 0.999 is 0.999 per step, so over 60 epochs of 200 batches that is 12,000 updates and a half-life of roughly 700 steps. A run with few steps wants a smaller number, because an average that forgets more slowly than the run is long never leaves where it started.

One hand on the learning rate

Three of these compute the rate from the epoch rather than from how the run is going - callback_cosine_annealing(), callback_linear_annealing() and callback_step_decay() - and at most one of them may be used, because each recomputes the rate every epoch and two of them would simply overwrite each other.

For the same reason a schedule and callback_reduce_lr_on_plateau() cannot be combined, and the engine refuses the pair. That refusal replaced a sentence claiming the opposite: a schedule recomputes the rate from the optimiser’s initial value at the end of every epoch, so it erases whatever the plateau callback cut. Measured on six epochs of a loss that never improves, the rate with both configured follows the cosine exactly - 0.0976, 0.0905, 0.0794 from 0.1 - where the plateau alone reaches 1e-6.

A schedule beside callback_swa() is fine and the order is no longer luck: swa pins its constant rate after averaging begins, whatever position the callbacks sit in.

Cosine annealing

callback_cosine_annealing() decays the learning rate from its initial value towards min_lr along a cosine. It watches nothing: it is a function of how far through the run you are rather than of how the run is going, which is the difference from callback_reduce_lr_on_plateau() - and a run may legitimately want both.

Towards rather than to, which is the engine’s 0.8.0a69 and worth a sentence because the obvious reading is wrong: a scheduler steps after each epoch, so the value computed for the epoch after the last one is min_lr and no epoch ever trains at it. The last epoch trains one step above. That is torch’s own behaviour for the same schedule, and setting epochs shorter than the run is the only way a run trains at min_lr at all - the rate parks there for the remaining epochs.

Each parameter group decays from its own initial rate, so an encoder_learning_rate set on a model is not flattened at the first epoch. That would have been invisible: the history records one learning_rate, the first group’s.

epochs unset means the model’s own epochs, which is what you want - a cosine that ends where the run ends.

Examples

library("rplatypus")

callback_early_stopping(monitor = "val_dice", patience = 10)
$name
[1] "early_stopping"

$monitor
[1] "val_dice"

$patience
[1] 10

$min_delta
[1] 0

$restore_best
[1] TRUE