Callbacks

best_model_callback

Callback for saving and loading best model

class mindnlp.engine.callbacks.best_model_callback.BestModelCallback(save_path=None, larger_better=True, auto_load=False, save_on_exception=False)[source]

Bases: Callback

Save the model with the best metrics value and reload the model at the end of the training. The best model can only be loaded at the end of the training.

Parameters
  • save_path (str) – Folder for saving.

  • larger_better (bool) – Whether the larger metrics, the better metrics. Default: True.

  • auto_load (bool) – Whether load the best model at the end of the training.

  • save_on_exception (bool) – Whether save the model on exception.

evaluate_end(run_context)[source]

Called after evaluating.

Parameters

run_context (RunContext) – Information about the model.

is_better_metric_value(metrics_values)[source]

Compare each metrics values with the best metrics values.

Parameters

metrics_values (float) – metrics values used to compared with the best metrics values so far.

train_end(run_context)[source]

Called once after network training and load the best model params.

Parameters

run_context (RunContext) – Information about the model.

callback_manager

Callback Manager.

class mindnlp.engine.callbacks.callback_manager.CallbackManager(callbacks)[source]

Bases: Callback

Callback Manager.

Parameters

callbacks (Optional[list[Callback], Callback]) – List of callback objects which should be executed while training. Default: None.

backward_begin(*arg)[source]

Called before each forward beginning.

backward_end(*arg)[source]

Called after each backward finished.

ds_sink_begin(*arg)[source]

Called before each data_sink beginning.

ds_sink_end(*arg)[source]

Called after each data_sink finished.

evaluate_begin(*arg)[source]

Called before evaluating.

evaluate_end(*arg)[source]

Called after evaluating.

exception(*arg)[source]

Called if having exceptions.

fetch_data_begin(*arg)[source]

Called before fetch each batch/ds_sink_size data.

fetch_data_end(*arg)[source]

Called after fetch each batch/ds_sink_size data.

forward_begin(*arg)[source]

Called before each forward beginning.

forward_end(*arg)[source]

Called after each step finished.

load_model(*arg)[source]

Called before loading model.

save_model(*arg)[source]

Called before saving model.

train_begin(*arg)[source]

Called once before network training.

train_end(*arg)[source]

Called once after network training.

train_epoch_begin(*arg)[source]

Called before each train epoch beginning.

train_epoch_end(*arg)[source]

Called after each train epoch finished.

train_step_begin(*arg)[source]

Called before each train step beginning.

train_step_end(*arg)[source]

Called after each train step finished.

class mindnlp.engine.callbacks.callback_manager.RunContext(engine_args)[source]

Bases: object

Provide information about the model. This class needs to be used with mindspore.train.callback.Callback.

Parameters

engine_args (dict) – Holding the related information of model.

checkpoint_callback

Callback for saving checkpoint.

class mindnlp.engine.callbacks.checkpoint_callback.CheckpointCallback(save_path=None, epochs=None, keep_checkpoint_max=5)[source]

Bases: Callback

Save checkpoint of the model. save the current Trainer state at the end of each epoch, which can be used to resume previous operations. Continue training a sample code using the most recent epoch

Parameters
  • save_path (str) – The path to save the state. A specific path needs to be specified, such as ‘checkpoints/chtp.pt’. Default: None.

  • epochs (int) – Save a checkpoint file every n epochs.

  • keep_checkpoint_max (int) – Save checkpoint files at most. Default:5.

train_begin(run_context)[source]

Notice the file saved path of checkpoints at the beginning of training.

Parameters

run_context (RunContext) – Information about the model.

train_epoch_end(run_context)[source]

Save checkpoint every n epochs at the end of the epoch.

Parameters

run_context (RunContext) – Information about the model.

earlystop_callback

Callback for Early Stop.

class mindnlp.engine.callbacks.earlystop_callback.EarlyStopCallback(patience=10, larger_better=True)[source]

Bases: Callback

Stop training without getting better after n epochs.

Parameters
  • patience (int) – Numbers of epochs evaluations without raising. Default:10.

  • larger_better (bool) – Whether the larger value of the metric is better. Default:True.

evaluate_end(run_context)[source]

Called after evaluating.

Parameters

run_context (RunContext) – Information about the model.

is_better_metric_value(metrics_values)[source]

Compare each metrics values with the best metrics values.

Parameters

metrics_values (float) – metrics values used to compared with the best metrics values so far.

timer_callback

Callback for timing.

class mindnlp.engine.callbacks.timer_callback.TimerCallback(print_steps=0, time_ndigit=3)[source]

Bases: Callback

Print relevant event information during the training process, such as training duration, evaluation duration, total duration.

Parameters
  • print_steps (int) –

    When to print time information.Default:-1.

    • -1: print once at the end of each epoch.

    • positive number n: print once n steps.

  • time_ndigit (int) – Number of decimal places to keep. Default:3

evaluate_begin(run_context)[source]

Called once before the network evaluating.

Parameters

run_context (RunContext) – Information about the model.

evaluate_end(run_context)[source]

Called once after the network evaluating.

Parameters

run_context (RunContext) – Information about the model.

format_timer(reset=True, train_end=False)[source]

Format the output.

Parameters

run_context (RunContext) – Information about the model.

train_begin(run_context)[source]

Called once before the network training.

Parameters

run_context (RunContext) – Information about the model.

train_end(run_context)[source]

Called once after network training.

Parameters

run_context (RunContext) – Information about the model.

train_epoch_begin(run_context)[source]

Called before each train epoch beginning.

Parameters

run_context (RunContext) – Information about the model.

train_epoch_end(run_context)[source]

Called after each train epoch finished.

Parameters

run_context (RunContext) – Information about the model.

train_step_begin(run_context)[source]

Called before each train step beginning.

Parameters

run_context (RunContext) – Information about the model.

train_step_end(run_context)[source]

Called after each train step finished.

Parameters

run_context (RunContext) – Information about the model.

class mindnlp.engine.callbacks.timer_callback.Timers[source]

Bases: object

Group of timers.

reset()[source]

Callbacks.