You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mindspore.nn.DynamicLossScaleUpdateCell.rst 2.7 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. mindspore.nn.DynamicLossScaleUpdateCell
  2. =======================================
  3. .. py:class:: mindspore.nn.DynamicLossScaleUpdateCell(loss_scale_value, scale_factor, scale_window)
  4. 用于动态地更新梯度放大系数(loss scale)的神经元。
  5. 使用梯度放大功能进行训练时,初始梯度放大系数值为 `loss_scale_value`。在每个训练步骤中,当出现溢出时,通过计算公式 `loss_scale`/`scale_factor` 减小梯度放大系数。如果连续 `scale_window` 步(step)未溢出,则将通过 `loss_scale` * `scale_factor` 增大梯度放大系数。
  6. 该类是 :class:`mindspore.nn.DynamicLossScaleManager` 的 `get_update_cell` 方法的返回值。训练过程中,类 :class:`mindspore.TrainOneStepWithLossScaleCell` 会调用该Cell来更新梯度放大系数。
  7. **参数:**
  8. - **loss_scale_value** (float) - 初始的梯度放大系数。
  9. - **scale_factor** (int) - 增减系数。
  10. - **scale_window** (int) - 未溢出时,增大梯度放大系数的最大连续训练步数。
  11. **输入:**
  12. - **loss_scale** (Tensor) - 训练期间的梯度放大系数,shape为 :math:`()`。
  13. - **overflow** (bool) - 是否发生溢出。
  14. **输出:**
  15. Bool,即输入 `overflow` 。
  16. **支持平台:**
  17. ``Ascend`` ``GPU``
  18. **样例:**
  19. >>> import numpy as np
  20. >>> from mindspore import Tensor, Parameter, nn
  21. >>> import mindspore.ops as ops
  22. >>>
  23. >>> class Net(nn.Cell):
  24. ... def __init__(self, in_features, out_features):
  25. ... super(Net, self).__init__()
  26. ... self.weight = Parameter(Tensor(np.ones([in_features, out_features]).astype(np.float32)),
  27. ... name='weight')
  28. ... self.matmul = ops.MatMul()
  29. ...
  30. ... def construct(self, x):
  31. ... output = self.matmul(x, self.weight)
  32. ... return output
  33. ...
  34. >>> in_features, out_features = 16, 10
  35. >>> net = Net(in_features, out_features)
  36. >>> loss = nn.MSELoss()
  37. >>> optimizer = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  38. >>> net_with_loss = nn.WithLossCell(net, loss)
  39. >>> manager = nn.DynamicLossScaleUpdateCell(loss_scale_value=2**12, scale_factor=2, scale_window=1000)
  40. >>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_sense=manager)
  41. >>> input = Tensor(np.ones([out_features, in_features]), mindspore.float32)
  42. >>> labels = Tensor(np.ones([out_features,]), mindspore.float32)
  43. >>> output = train_network(input, labels)
  44. .. py:method:: get_loss_scale()
  45. 获取当前梯度放大系数。