Callback that terminates training when a NaN loss is encountered.
Source:R/callbacks.R
callback_terminate_on_nan.RdThis callback monitors the loss during training and terminates training when
a NaN or Inf loss is detected. By default, training stops gracefully by
setting the model's stop_training flag, which allows callback cleanup
methods such as on_train_end() to run.
Set raise_error = TRUE to raise an error immediately when a NaN or Inf is
detected. In this mode, on_train_end() is not called on other callbacks.
This can preserve backup states or prevent unintended cleanup after a
training failure.
Value
A Callback instance that can be passed to fit.keras.src.models.model.Model().
Examples
# Graceful termination (default)
callback <- callback_terminate_on_nan()
model |> fit(x, y, callbacks = list(callback))
# Immediate error
callback <- callback_terminate_on_nan(raise_error = TRUE)
model |> fit(x, y, callbacks = list(callback))See also
Other callbacks: Callback() callback_backup_and_restore() callback_csv_logger() callback_early_stopping() callback_lambda() callback_learning_rate_scheduler() callback_model_checkpoint() callback_orbax_checkpoint() callback_reduce_lr_on_plateau() callback_remote_monitor() callback_swap_ema_weights() callback_tensorboard()