mindspore.train.ConvertNetUtils

class mindspore.train.ConvertNetUtils[源代码]

将网络转换为thor层网络,用于计算并存储二阶信息矩阵。

样例:

>>> import mindspore as ms
>>> convert_net_utils = ms.train.ConvertNetUtils()
convert_to_thor_net(net)[源代码]

该接口用于将网络转换为thor层网络,用于计算并存储二阶信息矩阵。

说明

此接口由二阶优化器thor自动调用。

参数:
  • net (Cell) - 由二阶优化器thor训练的网络。

支持平台:

Ascend GPU

样例:

>>> import mindspore as ms
>>> from mindspore import nn
>>> from mindspore import Tensor
>>> from mindspore.nn import thor
>>>
>>> # Define the network structure of LeNet5. Refer to
>>> # https://gitee.com/mindspore/docs/blob/r2.1/docs/mindspore/code/lenet.py
>>> net = LeNet5()
>>> temp = Tensor([4e-4, 1e-4, 1e-5, 1e-5], ms.float32)
>>> opt = thor(net, learning_rate=temp, damping=temp, momentum=0.9, loss_scale=128, frequency=4)
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> loss_scale = ms.FixedLossScaleManager(128, drop_overflow_update=False)
>>> model = ms.Model(net, loss_fn=loss, optimizer=opt, loss_scale_manager=loss_scale, metrics={'acc'},
...               amp_level="O2", keep_batchnorm_fp32=False)
>>> model = ms.train.ConvertModelUtils.convert_to_thor_model(model=model, network=net, loss_fn=loss,
...                                           optimizer=opt,loss_scale_manager=loss_scale, metrics={'acc'},
...                                           amp_level="O2", keep_batchnorm_fp32=False)