mindspore.train.BackupAndRestore

class mindspore.train.BackupAndRestore(backup_dir, save_freq='epoch', delete_checkpoint=True)[source]

Callback to back up and restore the parameters during training.

Note

This function can only use in training.

Parameters
  • backup_dir (str) – Path to store and load the checkpoint file.

  • save_freq (Union['epoch', int]) – When set to ‘epoch’ the callback saves the checkpoint at the end of each epoch. When set to an integer, the callback saves the checkpoint every save_freq epoch. Default: ‘epoch’.

  • delete_checkpoint (bool) – If delete_checkpoint=True, the checkpoint will be deleted after training is finished. Default: True.

Raises

Examples

Note

Before running the following example, you need to customize the network LeNet5 and dataset preparation function create_dataset. Refer to Building a Network and Dataset .

>>> from mindspore import nn
>>> from mindspore.train import Model, BackupAndRestore
>>>
>>> net = LeNet5()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> optim = nn.Momentum(net.trainable_params(), 0.01, 0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> backup_ckpt = BackupAndRestore("backup")
>>> model.train(10, dataset, callbacks=backup_ckpt)
on_train_begin(run_context)[source]

Load the backup checkpoint file at the beginning of epoch.

Parameters

run_context (RunContext) – Context of the process running. For more details, please refer to mindspore.train.RunContext.

on_train_end(run_context)[source]

Deleted checkpoint file at the end of train.

Parameters

run_context (RunContext) – Context of the process running. For more details, please refer to mindspore.train.RunContext.

on_train_epoch_end(run_context)[source]

Backup checkpoint file at the end of train epoch.

Parameters

run_context (RunContext) – Context of the process running. For more details, please refer to mindspore.train.RunContext.