mindspore.train.OnRequestExit

class mindspore.train.OnRequestExit(save_ckpt=True, save_mindir=True, file_name='Net', directory='./', sig=signal.SIGTERM)[source]

Respond to the user’s closing request, exit the training or eval process, and save the checkpoint and mindir.

Register OnRequestExit Callback before training, when the user want to exit the training process and save the training data, could send the registered exit signal ‘sig’ to the training process. After the training process executes the current step, saves the current training status, including checkpoint and mindir, and then exit the training process.

Parameters
  • save_ckpt (bool) – Whether save the checkpoint before the training process exit. Default: True.

  • save_mindir (bool) – Whether save the mindir before the training process exit. Default: True.

  • file_name (str) – The saved checkpoint and mindir file name, the checkpoint file add suffix ‘.ckpt’, the mindir file add suffix ‘.mindir’. Default: ‘Net’.

  • directory (str) – The directory save checkpoint and mindir. Default: ‘./’.

  • sig (int) – The user registered exit signal, it must be a captureable and negligible signal. When the process receives the signal, exits the training or eval process. Default: signal.SIGTERM.

Raises
  • ValueError – If the ‘save_ckpt’ is not a bool.

  • ValueError – If the ‘save_mindir’ is not a bool.

  • ValueError – If the ‘file_name’ is not a str.

  • ValueError – If the ‘directory’ is not a str.

  • ValueError – If the ‘sig’ is not an int or the ‘sig’ is signal.SIGKILL.

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import dataset as ds
>>> from mindspore import nn
>>>
>>> # Define the forward net
>>> class ForwardNet(nn.Cell):
>>>     def __init__(self, num_class=10, channel=1):
>>>         super(ForwardNet, self).__init__()
>>>         self.param = ms.Parameter(1.0)
>>>         self.relu = ms.ops.ReLU()
>>>
>>>     def construct(self, x):
>>>         return self.relu(x + self.param)
>>> forward_net = ForwardNet()
>>> loss = nn.MAELoss()
>>> opt = nn.Momentum(forward_net.trainable_params(), 0.01, 0.9)
>>> model = ms.Model(forward_net, loss_fn=loss, optimizer=opt)        >>>
>>> # Create dataset
>>> def generator_multi_column():
>>>    i = 0
>>>    while i < 1000:
>>>        i += 1
>>>        yield np.ones((1, 32, 32)).astype(np.float32) * 0.01, np.array(1).astype(np.int32)
>>> dataset = ds.GeneratorDataset(source=generator_multi_column, column_names=["data", "label"])
>>> dataset = dataset.batch(32, drop_remainder=True)
>>>
>>> on_request_exit = ms.train.OnRequestExit(file_name='LeNet5')
>>> model.train(10, dataset, callbacks=on_request_exit)
>>> # The user send the signal SIGTERM to the training process,
>>> # the process would save the checkpoint and mindir, and then exit the training process.
on_eval_begin(run_context)[source]

When the eval begin, register the handler for exit signal transferred by user.

Parameters

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

on_eval_end(run_context)[source]

When the eval end, if received the exit signal, the checkpoint and mindir would be saved according to the user config.

Parameters

run_context (RunContext) – Include some information of the model. For more details, please refer to mindspore.train.RunContext.

on_eval_step_end(run_context)[source]

When the eval step end, if received the exit signal, set the ‘run_context’ attribute ‘_stop_requested’ to True. Then exit the eval process after this step eval.

Parameters

run_context (RunContext) – Include some information of the model. For more details, please refer to mindspore.train.RunContext.

on_train_begin(run_context)[source]

When the train begin, register the handler for exit signal transferred by user.

Parameters

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

on_train_end(run_context)[source]

When the train end, if received the exit signal, the checkpoint and mindir would be saved according to the user config.

Parameters

run_context (RunContext) – Include some information of the model. For more details, please refer to mindspore.train.RunContext.

on_train_epoch_end(run_context)[source]

When the train epoch end, if received the exit signal, set the ‘run_context’ attribute ‘_stop_requested’ to True. Then exit the training process after this epoch training.

Parameters

run_context (RunContext) – Include some information of the model. For more details, please refer to mindspore.train.RunContext.

on_train_step_end(run_context)[source]

When the train step end, if received the exit signal, set the ‘run_context’ attribute ‘_stop_requested’ to True. Then exit the training process after this step training.

Parameters

run_context (RunContext) – Include some information of the model. For more details, please refer to mindspore.train.RunContext.