mindspore_rec
- class mindspore_rec.RecModel(network, loss_fn=None, optimizer=None, metrics=None, eval_network=None, eval_indexes=None, amp_level='O0', boost_level='O0')[source]
A high-level API for recommending network models, providing interfaces such as online training.
- Parameters
network (Cell) – A training network.
loss_fn (Cell) – Objective function. If loss_fn is None, the network should contain the calculation of loss and parallel if needed. Default: None.
optimizer (Cell) – Optimizer for updating the weights. If optimizer is None, the network needs to do backpropagation and update weights. Default value: None.
metrics (Union[dict, set]) – A Dictionary or a set of metrics for model evaluation. eg: {‘accuracy’, ‘recall’}. Default: None.
eval_network (Cell) – Network for evaluation. If not defined, network and loss_fn would be wrapped as eval_network . Default: None.
eval_indexes (list) – It is used when eval_network is defined. If eval_indexes is None by default, all outputs of the eval_network would be passed to metrics. If eval_indexes is set, it must contain three elements: the positions of loss value, predicted value and label in outputs of the eval_network. In this case, the loss value will be passed to the Loss metric, the predicted value and label will be passed to other metrics. mindspore.train.Metric.set_indexes is recommended instead of eval_indexes. Default: None.
amp_level (str) –
Option for argument level in mindspore.amp.build_train_network, level for mixed precision training. Supports [“O0”, “O2”, “O3”, “auto”]. Default: “O0”.
”O0”: Do not change.
”O2”: Cast network to float16, keep BatchNorm run in float32, using dynamic loss scale.
”O3”: Cast network to float16, the BatchNorm is also cast to float16, loss scale will not be used.
auto: Set level to recommended level in different devices. Set level to “O2” on GPU, set level to “O3” on Ascend. The recommended level is chosen by the expert experience, not applicable to all scenarios. User should specify the level for special network.
”O2” is recommended on GPU, “O3” is recommended on Ascend. The BatchNorm strategy can be changed by keep_batchnorm_fp32 settings in kwargs. keep_batchnorm_fp32 must be a bool. The loss scale strategy can be changed by loss_scale_manager setting in kwargs. loss_scale_manager should be a subclass of mindspore.amp.LossScaleManager. The more detailed explanation of amp_level setting can be found at mindspore.amp.build_train_network.
boost_level (str) –
Option for argument level in mindspore.boost, level for boost mode training. Supports [“O0”, “O1”, “O2”]. Default: “O0”.
”O0”: Do not change.
”O1”: Cast the operators in white_list to float16, the remaining operators are kept in float32.
”O1”: Enable the boost mode, the performance is improved by about 20%, and the accuracy is the same as the original accuracy.
”O2”: Enable the boost mode, the performance is improved by about 30%, and the accuracy is reduced by less than 3%.
If you want to config boost mode by yourself, you can set boost_config_dict as boost.py. In order for this function to work, you need to set the optimizer, eval_network or metric parameters at the same time.
Notice: The current optimization enabled by default only applies to some networks, and not all networks can obtain the same benefits. It is recommended to enable this function on the Graph mode + Ascend platform, and for better acceleration, refer to the documentation to configure boost_config_dict.
- online_train(train_dataset, callbacks=None, dataset_sink_mode=True, sink_size=1)[source]
Online training API for recommend model.
Note
If dataset_sink_mode is True, data will be sent to device queue. If the device is Ascend, features of data will be transferred one by one. The limitation of data transmission per time is 256M.
When dataset_sink_mode is True, the step_end method of the instance of Callback will be called at the end of epoch, and dataset will be bound to this model and cannot be used by other models.
When setting device target is CPU, the training process will be performed with dataset not sink mode.
The dataset for online training is unbounded, it has an infinite number of batch data, which is the main difference from the offline training dataset.
- Parameters
train_dataset (Dataset) – A training dataset iterator. If loss_fn is defined, the data and label will be passed to the network and the loss_fn respectively, so a tuple (data, label) should be returned from dataset. If there is multiple data or labels, set loss_fn to None and implement calculation of loss in network, then a tuple (data1, data2, data3, …) with all data returned from dataset will be passed to the network. The train dataset is unbounded.
callbacks (Optional[list[Callback], Callback]) – List of callback objects or callback object, which should be executed while training. Default: None.
dataset_sink_mode (bool) – Determines whether to pass the data through dataset channel. Configure device target of CPU, the training process will be performed with dataset not sink. Default: True.
sink_size (int) – Controls how many batches of data in each sink. Default: 1