# mindspore.train¶

## mindspore.train.summary¶

SummaryRecord.

User can use SummaryRecord to dump the summary data, the summary is a series of operations to collect data for analysis and visualization.

class mindspore.train.summary.SummaryRecord(log_dir, file_prefix='events', file_suffix='_MS', network=None, max_file_size=None)[source]

SummaryRecord is used to record the summary data and lineage data.

The API will create a summary file and lineage files lazily in a given directory and writes data to them. It writes the data to files by executing the ‘record’ method. In addition to recording the data bubbled up from the network by defining the summary operators, SummaryRecord also supports to record extra data which can be added by calling add_value.

Note

1. Make sure to close the SummaryRecord at the end, otherwise the process will not exit. Please see the Example section below to learn how to close properly in two ways.

2. Only one SummaryRecord instance is allowed at a time, otherwise it will cause data writing problems.

3. SummaryRecord only supports Linux systems.

Parameters
• log_dir (str) – The log_dir is a directory location to save the summary.

• file_prefix (str) – The prefix of file. Default: “events”.

• file_suffix (str) – The suffix of file. Default: “_MS”.

• network (Cell) – Obtain a pipeline through network for saving graph summary. Default: None.

• max_file_size (Optional[int]) – The maximum size of each file that can be written to disk (in bytes). Unlimited by default. For example, to write not larger than 4GB, specify max_file_size=4 * 1024**3.

Raises
• TypeError – If the type of max_file_size is not int, or the type of file_prefix or file_suffix is not str.

• RuntimeError – If the log_dir is not a normalized absolute path name.

Examples

>>> # use in with statement to auto close
>>> from mindspore.train.summary import SummaryRecord
>>> with SummaryRecord(log_dir="./summary_dir") as summary_record:
...     pass
>>>
>>> # use in try .. finally .. to ensure closing
>>> try:
...     summary_record = SummaryRecord(log_dir="./summary_dir")
... finally:
...     summary_record.close()

add_value(plugin, name, value)[source]

Add value to be recorded later.

Parameters
• plugin (str) – The value of the plugin.

• name (str) – The value of the name.

• value (Union[Tensor, GraphProto, TrainLineage, EvaluationLineage, DatasetGraph, UserDefinedInfo]) –

The value to store.

• The data type of value should be ‘GraphProto’ (see mindspore/ccsrc/anf_ir.proto) object when the plugin is ‘graph’.

• The data type of value should be ‘Tensor’ object when the plugin is ‘scalar’, ‘image’, ‘tensor’ or ‘histogram’.

• The data type of value should be a ‘TrainLineage’ object when the plugin is ‘train_lineage’, see mindspore/ccsrc/lineage.proto.

• The data type of value should be a ‘EvaluationLineage’ object when the plugin is ‘eval_lineage’, see mindspore/ccsrc/lineage.proto.

• The data type of value should be a ‘DatasetGraph’ object when the plugin is ‘dataset_graph’, see mindspore/ccsrc/lineage.proto.

• The data type of value should be a ‘UserDefinedInfo’ object when the plugin is ‘custom_lineage_data’, see mindspore/ccsrc/lineage.proto.

• The data type of value should be a ‘Explain’ object when the plugin is ‘explainer’, see mindspore/ccsrc/summary.proto.

Raises

Examples

>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:

close()[source]

Flush all events and close summary records. Please use the statement to autoclose.

Examples

>>> try:
...     summary_record = SummaryRecord(log_dir="./summary_dir")
... finally:
...     summary_record.close()

flush()[source]

Flush the event file to disk.

Call it to make sure that all pending events have been written to disk.

Examples

>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
...     summary_record.flush()

property log_dir

Get the full path of the log file.

Returns

str, the full path of log file.

Examples

>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
...     log_dir = summary_record.log_dir

record(step, train_network=None, plugin_filter=None)[source]

Record the summary.

Parameters
• step (int) – Represents training step number.

• train_network (Cell) – The network to call the callback.

• plugin_filter (Optional[Callable[[str], bool]]) – The filter function, which is used to filter out plugins from being written by returning False.

Returns

bool, whether the record process is successful or not.

Examples

>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
...     summary_record.record(step=2)
...
True

set_mode(mode)[source]

Sets the training phase. Different training phases affect data recording.

Parameters

mode (str) – The mode to be set, which should be ‘train’ or ‘eval’. When the mode is ‘eval’, summary_record will not record the data of summary operators.

Raises

ValueError – When the mode is not recognized.

Examples

>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
...     summary_record.set_mode('eval')


## mindspore.train.callback¶

Callback related classes and functions.

class mindspore.train.callback.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 initialize and release resources automatically.

Callback function will execute some operations in the current step or epoch.

Examples

>>> class Print_info(Callback):
>>>     def step_end(self, run_context):
>>>         cb_params = run_context.original_args()
>>>         print(cb_params.cur_epoch_num)
>>>         print(cb_params.cur_step_num)
>>>
>>> print_cb = Print_info()
>>> model.train(epoch, dataset, callbacks=print_cb)

begin(run_context)[source]

Called once before the network executing.

Parameters

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

end(run_context)[source]

Called once after network training.

Parameters

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

epoch_begin(run_context)[source]

Called before each epoch beginning.

Parameters

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

epoch_end(run_context)[source]

Called after each epoch finished.

Parameters

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

step_begin(run_context)[source]

Called before each epoch beginning.

Parameters

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

step_end(run_context)[source]

Called after each step finished.

Parameters

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

class mindspore.train.callback.LossMonitor(per_print_times=1)[source]

Monitor the loss in training.

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) – Print the loss each every time. Default: 1.

Raises

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

class mindspore.train.callback.TimeMonitor(data_size=None)[source]

Monitor the time in training.

Parameters

data_size (int) – Dataset size. Default: None.

class mindspore.train.callback.ModelCheckpoint(prefix='CKP', directory=None, config=None)[source]

The checkpoint callback class.

It is called to combine with train process and save the model and network parameters after traning.

Parameters
• prefix (str) – The prefix name of checkpoint files. Default: “CKP”.

• directory (str) – The path of the folder which will be saved in the checkpoint file. Default: None.

• config (CheckpointConfig) – Checkpoint strategy configuration. Default: None.

Raises
• ValueError – If the prefix is invalid.

• TypeError – If the config is not CheckpointConfig type.

end(run_context)[source]

Save the last checkpoint after training finished.

Parameters

run_context (RunContext) – Context of the train running.

property latest_ckpt_file_name

Return the latest checkpoint path and file name.

step_end(run_context)[source]

Save the checkpoint at the end of step.

Parameters

run_context (RunContext) – Context of the train running.

class mindspore.train.callback.SummaryCollector(summary_dir, collect_freq=10, collect_specified_data=None, keep_default_action=True, custom_lineage_data=None, collect_tensor_freq=None, max_file_size=None)[source]

It can help you to collect loss, learning late, computational graph and so on. SummaryCollector also enables the summary operator to collect data from a summary file.

Note

1. Multiple SummaryCollector instances in callback list are not allowed.

2. Not all information is collected at the training phase or at the eval phase.

3. SummaryCollector always record the data collected by the summary operator.

4. SummaryCollector only supports Linux systems.

Parameters
• summary_dir (str) – The collected data will be persisted to this directory. If the directory does not exist, it will be created automatically.

• collect_freq (int) – Set the frequency of data collection, it should be greater then zero, and the unit is step. Default: 10. If a frequency is set, we will collect data when (current steps % freq) equals to 0, and the first step will be collected at any time. It is important to note that if the data sink mode is used, the unit will become the epoch. It is not recommended to collect data too frequently, which can affect performance.

• collect_specified_data (Union[None, dict]) –

Perform custom operations on the collected data. Default: None. By default, if set to None, all data is collected as the default behavior. You can customize the collected data with a dictionary. For example, you can set {‘collect_metric’: False} to control not collecting metrics. The data that supports control is shown below.

• collect_metric: Whether to collect training metrics, currently only the loss is collected. The first output will be treated as the loss and it will be averaged. Optional: True/False. Default: True.

• collect_graph: Whether to collect the computational graph. Currently, only training computational graph is collected. Optional: True/False. Default: True.

• collect_train_lineage: Whether to collect lineage data for the training phase, this field will be displayed on the lineage page of Mindinsight. Optional: True/False. Default: True.

• collect_eval_lineage: Whether to collect lineage data for the evaluation phase, this field will be displayed on the lineage page of Mindinsight. Optional: True/False. Default: True.

• collect_input_data: Whether to collect dataset for each training. Currently only image data is supported. Optional: True/False. Default: True.

• collect_dataset_graph: Whether to collect dataset graph for the training phase. Optional: True/False. Default: True.

• histogram_regular: Collect weight and bias for parameter distribution page and displayed in MindInsight. This field allows regular strings to control which parameters to collect. Default: None, it means only the first five parameters are collected. It is not recommended to collect too many parameters at once, as it can affect performance. Note that if you collect too many parameters and run out of memory, the training will fail.

• keep_default_action (bool) – This field affects the collection behavior of the ‘collect_specified_data’ field. Optional: True/False, Default: True. True: it means that after specified data is set, non-specified data is collected as the default behavior. False: it means that after specified data is set, only the specified data is collected, and the others are not collected.

• custom_lineage_data (Union[dict, None]) – Allows you to customize the data and present it on the MingInsight lineage page. In the custom data, the type of the key supports str, and the type of value supports str, int and float. Default: None, it means there is no custom data.

• collect_tensor_freq (Optional[int]) – The same semantics as the collect_freq, but controls TensorSummary only. Because TensorSummary data is too large to be compared with other summary data, this parameter is used to reduce its collection. By default, The maximum number of steps for collecting TensorSummary data is 20, but it will not exceed the number of steps for collecting other summary data. Default: None, which means to follow the behavior as described above. For example, given collect_freq=10, when the total steps is 600, TensorSummary will be collected 20 steps, while other summary data 61 steps, but when the total steps is 20, both TensorSummary and other summary will be collected 3 steps. Also note that when in parallel mode, the total steps will be splitted evenly, which will affect the number of steps TensorSummary will be collected.

• max_file_size (Optional[int]) – The maximum size in bytes of each file that can be written to the disk. Default: None, which means no limit. For example, to write not larger than 4GB, specify max_file_size=4 * 1024**3.

Raises
• ValueError – If the parameter value is not expected.

• TypeError – If the parameter type is not expected.

• RuntimeError – If an error occurs during data collection.

Examples

>>> # Simple usage:
>>> from mindspore.train import Model
>>> summary_collector = SummaryCollector(summary_dir='./summary_dir')
>>> dataset = get_dataset('/path/to/MNIST')
>>> network = LeNet5()
>>> model = Model(network)
>>> model.train(epoch=1, dataset=dataset, callbacks=summary_collector)
>>>
>>> # Do not collect metric and collect the first layer parameter, others are collected by default
>>> specified={'collect_metric': False, 'histogram_regular': '^conv1.*'}
>>> summary_collector = SummaryCollector(summary_dir='./summary_dir', collect_specified_data=specified)
>>> model.train(epoch=1, dataset=dataset, callbacks=summary_collector)
>>>
>>> # Only collect metric, custom lineage data and record data that collected by the summary operator,
>>> # others are not collected
>>> specified = {'collect_metric': True}
>>> summary_collector = SummaryCollector('./summary_dir',
>>>                                      collect_specified_data=specified,
>>>                                      keep_default_action=False,
>>>                                      custom_lineage_data={'version': 'resnet50_v1'}
>>>                                      )
>>> model.train(epoch=1, dataset=dataset, callbacks=summary_collector)

class mindspore.train.callback.CheckpointConfig(save_checkpoint_steps=1, save_checkpoint_seconds=0, keep_checkpoint_max=5, keep_checkpoint_per_n_minutes=0, integrated_save=True, async_save=False, saved_network=None)[source]

The configuration of model checkpoint.

Note

During the training process, if dataset is transmitted through the data channel, It is suggested to set ‘save_checkpoint_steps’ to an integer multiple of loop_size. Otherwise, the time to save the checkpoint may be biased.

Parameters
• save_checkpoint_steps (int) – Steps to save checkpoint. Default: 1.

• save_checkpoint_seconds (int) – Seconds to save checkpoint. Default: 0. Can’t be used with save_checkpoint_steps at the same time.

• keep_checkpoint_max (int) – Maximum number of checkpoint files can be saved. Default: 5.

• keep_checkpoint_per_n_minutes (int) – Keep one checkpoint every n minutes. Default: 0. Can’t be used with keep_checkpoint_max at the same time.

• integrated_save (bool) – Whether to perform integrated save function in automatic model parallel scene. Default: True. Integrated save function is only supported in automatic parallel scene, not supported in manual parallel.

• async_save (bool) – Whether asynchronous execution saves the checkpoint to a file. Default: False.

• saved_network (Cell) – Network to be saved in checkpoint file. If the saved_network has no relation with the network in training, the initial value of saved_network will be saved. Default: None.

Raises

ValueError – If the input_param is None or 0.

Examples

>>> class LeNet5(nn.Cell):
>>>     def __init__(self, num_class=10, num_channel=1):
>>>         super(LeNet5, self).__init__()
>>>         self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
>>>         self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
>>>         self.fc1 = nn.Dense(16 * 5 * 5, 120, weight_init=Normal(0.02))
>>>         self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))
>>>         self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))
>>>         self.relu = nn.ReLU()
>>>         self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
>>>         self.flatten = nn.Flatten()
>>>
>>>     def construct(self, x):
>>>         x = self.max_pool2d(self.relu(self.conv1(x)))
>>>         x = self.max_pool2d(self.relu(self.conv2(x)))
>>>         x = self.flatten(x)
>>>         x = self.relu(self.fc1(x))
>>>         x = self.relu(self.fc2(x))
>>>         x = self.fc3(x)
>>>         return x
>>>
>>> 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)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> config = CheckpointConfig(saved_network=net)
>>> ckpoint_cb = ModelCheckpoint(prefix='LeNet5', directory='./checkpoint', config=config)
>>> model.train(10, dataset, callbacks=ckpoint_cb)

property async_save

Get the value of _async_save.

get_checkpoint_policy()[source]

Get the policy of checkpoint.

property integrated_save

Get the value of _integrated_save.

property keep_checkpoint_max

Get the value of _keep_checkpoint_max.

property keep_checkpoint_per_n_minutes

Get the value of _keep_checkpoint_per_n_minutes.

property save_checkpoint_seconds

Get the value of _save_checkpoint_seconds.

property save_checkpoint_steps

Get the value of _save_checkpoint_steps.

property saved_network

Get the value of _saved_network

class mindspore.train.callback.RunContext(original_args)[source]

Provides information about original request to model function. Callback objects can stop the loop by calling request_stop() of run_context.

Parameters

original_args (dict) – Holding the related information of model.

get_stop_requested()[source]

Returns whether a stop is requested or not.

Returns

bool, if true, model.train() stops iterations.

original_args()[source]

Get the _original_args object.

Returns

Dict, an object that holds the original arguments of model.

request_stop()[source]

Sets stop requirement during training.

Callbacks can use this function to request stop of iterations. model.train() checks whether this is called or not.

class mindspore.train.callback.LearningRateScheduler(learning_rate_function)[source]

Change the learning_rate during training.

Note

This class are not supported on CPU.

Parameters

learning_rate_function (Function) – The function about how to change the learning rate during training.

Examples

>>> from _lr_scheduler_callback import LearningRateScheduler
>>> import mindspore.nn as nn
>>> from mindspore.train import Model
...
>>> def learning_rate_function(lr, cur_step_num):
...     if cur_step_num%1000 == 0:
...         lr = lr*0.1
...     return lr
...
>>> lr = 0.1
>>> momentum = 0.9
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> optim = nn.Momentum(net.trainable_params(), learning_rate=lr, momentum=momentum)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
...
>>> dataset = create_custom_dataset("custom_dataset_path")
>>> model.train(1, dataset, callbacks=[LearningRateScheduler(learning_rate_function)],
...             dataset_sink_mode=False)