mindspore.train.LossMonitor

View Source On Gitee
class mindspore.train.LossMonitor(per_print_times=1)[source]

Monitor the loss in train or monitor the loss and eval metrics in fit.

If the loss is NAN or INF, it will terminate training.

Note

If per_print_times is 0, do not print loss.

Parameters

per_print_times (int) – How many steps to print once loss. During sink mode, it will print loss in the nearest step. Default: 1 .

Raises

ValueError – If per_print_times is not an integer or less than zero.

Examples

>>> from mindspore import nn
>>> from mindspore.train import Model, LossMonitor
>>>
>>> # Define the network structure of LeNet5. Refer to
>>> # https://gitee.com/mindspore/docs/blob/r2.2/docs/mindspore/code/lenet.py
>>> 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)
>>> # Create the dataset taking MNIST as an example. Refer to
>>> # https://gitee.com/mindspore/docs/blob/r2.2/docs/mindspore/code/mnist.py
>>> dataset = create_dataset()
>>> loss_monitor = LossMonitor()
>>> model.train(10, dataset, callbacks=loss_monitor)
on_train_epoch_end(run_context)[source]

When LossMonitor used in model.fit, print eval metrics at the end of epoch if current epoch should do evaluation.

Parameters

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

step_end(run_context)[source]

Print training loss at the end of step.

Parameters

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