mindspore.train.TimeMonitor

class mindspore.train.TimeMonitor(data_size=None)[源代码]

监控训练或推理的时间。

参数:
  • data_size (int) - 表示每隔多少个step打印一次信息。如果程序在训练期间获取到Model的 batch_num ,则将把 data_size 设为 batch_num ,否则将使用 data_size 。默认值: None

异常:
  • ValueError - data_size 不是正整数。

样例:

>>> from mindspore import nn
>>> from mindspore.train import Model, TimeMonitor
>>>
>>> # Define the network structure of LeNet5. Refer to
>>> # https://gitee.com/mindspore/docs/blob/r2.1/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.1/docs/mindspore/code/mnist.py
>>> dataset = create_dataset()
>>> time_monitor = TimeMonitor()
>>> model.train(10, dataset, callbacks=time_monitor)
epoch_begin(run_context)[源代码]

在epoch开始时记录时间。

参数:
epoch_end(run_context)[源代码]

在epoch结束时打印epoch的耗时。

参数: