mindspore.train.History

class mindspore.train.History[source]

Records the network outputs and metrics information into a History object.

The network outputs information will be the loss value if not custimizing the train network or eval network; if the custimized network returns a Tensor or numpy.ndarray, the mean value of network output will be recorded, if the custimized network returns a tuple or list, the first element of network outputs will be recorded.

Note

Normally used in mindspore.train.Model.train or mindspore.train.Model.fit.

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> from mindspore import nn
>>> from mindspore.train import Model, History
>>> data = {"x": np.float32(np.random.rand(64, 10)), "y": np.random.randint(0, 5, (64,))}
>>> train_dataset = ds.NumpySlicesDataset(data=data).batch(32)
>>> net = nn.Dense(10, 5)
>>> crit = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> opt = nn.Momentum(net.trainable_params(), 0.01, 0.9)
>>> history_cb = History()
>>> model = Model(network=net, optimizer=opt, loss_fn=crit, metrics={"recall"})
>>> model.train(2, train_dataset, callbacks=[history_cb])
>>> print(history_cb.epoch)
>>> print(history_cb.history)
{'epoch': [1, 2]}
{'net_output': [1.607877, 1.6033841]}
begin(run_context)[source]

Initialize the epoch property at the begin of training.

Parameters

run_context (RunContext) – Context of the mindspore.train.Model.{train | eval}. For more details, please refer to mindspore.train.RunContext.

epoch_end(run_context)[source]

Records the first element of network outputs and metrics information at the end of epoch.

Parameters

run_context (RunContext) – Context of the mindspore.train.Model.{train | eval}. For more details, please refer to mindspore.train.RunContext.