mindspore.train.Callback

class mindspore.train.Callback[source]

Abstract base class used to build a Callback class. Callbacks are context managers which will be entered and exited when passing into the Model. You can use this mechanism to do some custom operations.

Each method of Callback class corresponds to a stage in training or eval process, and those methods have the same input run_context, which hold context information of the model in training or eval process. When defining a Callback subclass or creating a custom Callback, note that you should override methods with names prefixed with "on_train" or "on_eval", otherwise ValueError will be raised if the custimized Callbacks used in model.fit.

When creating a custom Callback, model context information can be obtained in Callback methods by calling RunContext.original_args(), which is a dictionary varivable recording current attributes. Users can add custimized attributes to the information. Training process can also be stopped by calling request_stop method. For details of custom Callback, please check Callback tutorial.

Examples

>>> import numpy as np
>>> from mindspore import nn
>>> from mindspore import dataset as ds
>>> from mindspore.train import Model, Callback
>>> class Print_info(Callback):
...     def step_end(self, run_context):
...         cb_params = run_context.original_args()
...         print("step_num: ", cb_params.cur_step_num)
>>>
>>> print_cb = Print_info()
>>> data = {"x": np.float32(np.random.rand(64, 10)), "y": np.random.randint(0, 5, (64,))}
>>> dataset = ds.NumpySlicesDataset(data=data).batch(32)
>>> net = nn.Dense(10, 5)
>>> 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)
>>> model.train(1, dataset, callbacks=print_cb)
step_num: 1
step_num: 2
begin(run_context)[source]

Called once before the network executing. A backwards compatibility alias for on_train_begin and on_eval_begin.

Note

begin is deprecated and will be deleted in a future version, please use on_train_begin and on_eval_begin instead.

Parameters

run_context (RunContext) – Include some information of the model.

end(run_context)[source]

Called once after network training. A backwards compatibility alias for on_train_end and on_eval_end.

Note

end is deprecated and will be deleted in a future version, please use on_train_end and on_eval_end instead.

Parameters

run_context (RunContext) – Include some information of the model.

epoch_begin(run_context)[source]

Called before each epoch beginning. A backwards compatibility alias for on_train_epoch_begin and on_eval_epoch_begin.

Note

epoch_begin is deprecated and will be deleted in a future version, please use on_train_epoch_begin and on_eval_epoch_begin instead.

Parameters

run_context (RunContext) – Include some information of the model.

epoch_end(run_context)[source]

Called after each epoch finished. A backwards compatibility alias for on_train_epoch_end and on_eval_epoch_end.

Note

epoch_end is deprecated and will be deleted in a future version, please use on_train_epoch_end and on_eval_epoch_end instead.

Parameters

run_context (RunContext) – Include some information of the model.

on_eval_begin(run_context)[source]

Called before eval begin.

Parameters

run_context (RunContext) – Include some information of the model.

on_eval_end(run_context)[source]

Called after eval end.

Parameters

run_context (RunContext) – Include some information of the model.

on_eval_epoch_begin(run_context)[source]

Called before eval epoch begin.

Parameters

run_context (RunContext) – Include some information of the model.

on_eval_epoch_end(run_context)[source]

Called after eval epoch end.

Parameters

run_context (RunContext) – Include some information of the model.

on_eval_step_begin(run_context)[source]

Called before each eval step begin.

Parameters

run_context (RunContext) – Include some information of the model.

on_eval_step_end(run_context)[source]

Called after each eval step end.

Parameters

run_context (RunContext) – Include some information of the model.

on_train_begin(run_context)[source]

Called once before the network training.

Parameters

run_context (RunContext) – Include some information of the model.

on_train_end(run_context)[source]

Called after training end.

Parameters

run_context (RunContext) – Include some information of the model.

on_train_epoch_begin(run_context)[source]

Called before each training epoch begin.

Parameters

run_context (RunContext) – Include some information of the model.

on_train_epoch_end(run_context)[source]

Called after each training epoch end.

Parameters

run_context (RunContext) – Include some information of the model.

on_train_step_begin(run_context)[source]

Called before each training step begin.

Parameters

run_context (RunContext) – Include some information of the model.

on_train_step_end(run_context)[source]

Called after each training step end.

Parameters

run_context (RunContext) – Include some information of the model.

step_begin(run_context)[source]

Called before each step beginning. A backwards compatibility alias for on_train_step_begin and on_eval_step_begin.

Note

step_begin is deprecated and will be deleted in a future version, please use on_train_step_begin and on_eval_step_begin instead.

Parameters

run_context (RunContext) – Include some information of the model.

step_end(run_context)[source]

Called after each step finished. A backwards compatibility alias for on_train_step_end and on_eval_step_end.

Note

step_end is deprecated and will be deleted in a future version, please use on_train_step_end and on_eval_step_end instead.

Parameters

run_context (RunContext) – Include some information of the model.