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.

test_adam.py 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """ test adam """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter, context
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import TrainOneStepCell, WithLossCell
  22. from mindspore.nn.optim import Adam, AdamWeightDecay
  23. from mindspore.ops import operations as P
  24. context.set_context(enable_sparse=True)
  25. class Net(nn.Cell):
  26. """ Net definition """
  27. def __init__(self):
  28. super(Net, self).__init__()
  29. self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight")
  30. self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias")
  31. self.matmul = P.MatMul()
  32. self.biasAdd = P.BiasAdd()
  33. def construct(self, x):
  34. x = self.biasAdd(self.matmul(x, self.weight), self.bias)
  35. return x
  36. class NetWithoutWeight(nn.Cell):
  37. def __init__(self):
  38. super(NetWithoutWeight, self).__init__()
  39. self.matmul = P.MatMul()
  40. def construct(self, x):
  41. x = self.matmul(x, x)
  42. return x
  43. class NetWithSparseGatherV2(nn.Cell):
  44. """ NetWithSparseGatherV2 definition """
  45. def __init__(self):
  46. super(NetWithSparseGatherV2, self).__init__()
  47. self.weight1 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight1")
  48. self.weight2 = Parameter(Tensor(np.ones([2, 1, 2]).astype((np.float32))), name="weight2")
  49. self.axis = 0
  50. self.gather = P.SparseGatherV2()
  51. def construct(self, indices, label):
  52. return self.gather(self.weight1, indices, self.axis) + self.weight2
  53. def test_adamwithoutparam():
  54. net = NetWithoutWeight()
  55. net.set_train()
  56. with pytest.raises(ValueError, match=r"Optimizer got an empty parameter list"):
  57. AdamWeightDecay(net.trainable_params(), learning_rate=0.1)
  58. def test_adamw_compile():
  59. """ test_adamw_compile """
  60. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  61. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  62. net = Net()
  63. net.set_train()
  64. loss = nn.SoftmaxCrossEntropyWithLogits()
  65. optimizer = AdamWeightDecay(net.trainable_params(), learning_rate=0.1)
  66. net_with_loss = WithLossCell(net, loss)
  67. train_network = TrainOneStepCell(net_with_loss, optimizer)
  68. _executor.compile(train_network, inputs, label)
  69. def test_adam_compile():
  70. """ test adam compile """
  71. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  72. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  73. net = Net()
  74. net.set_train()
  75. loss = nn.SoftmaxCrossEntropyWithLogits()
  76. optimizer = Adam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9)
  77. net_with_loss = WithLossCell(net, loss)
  78. train_network = TrainOneStepCell(net_with_loss, optimizer)
  79. _executor.compile(train_network, inputs, label)
  80. def test_sparse_adam_compile():
  81. """ test_sparse_adam_compile """
  82. indices = Tensor(np.array([0, 1]).astype(np.int32))
  83. label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
  84. net = NetWithSparseGatherV2()
  85. net.set_train()
  86. optimizer = Adam(net.trainable_params(), learning_rate=0.1, loss_scale=1024.0, weight_decay=0.9)
  87. optimizer.target = 'CPU'
  88. train_network = TrainOneStepCell(net, optimizer)
  89. _executor.compile(train_network, indices, label)
  90. def test_sparse_adam():
  91. """ test_sparse_adam """
  92. indices = Tensor(np.array([0, 1]).astype(np.int32))
  93. label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
  94. net = NetWithSparseGatherV2()
  95. net.set_train()
  96. optimizer = Adam(net.trainable_params(), learning_rate=0.1, loss_scale=1024.0, weight_decay=0.9)
  97. train_network = TrainOneStepCell(net, optimizer)
  98. _executor.compile(train_network, indices, label)
  99. def test_adam_group1():
  100. """ test_adam_group_lr_and_weight_decay """
  101. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  102. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  103. net = Net()
  104. net.set_train()
  105. loss = nn.SoftmaxCrossEntropyWithLogits()
  106. net_with_loss = WithLossCell(net, loss)
  107. all_params = net.trainable_params()
  108. poly_decay_lr = nn.polynomial_decay_lr(0.01, 0.0001, total_step=10, step_per_epoch=1, decay_epoch=3, power=1.0)
  109. group_params = [{'params': [all_params[0]], 'lr': poly_decay_lr, 'weight_decay': 0.9},
  110. {'params': [all_params[1]]}]
  111. optimizer = nn.Adam(group_params, learning_rate=0.1)
  112. train_network = TrainOneStepCell(net_with_loss, optimizer)
  113. _executor.compile(train_network, inputs, label)
  114. def test_adam_group2():
  115. """ test_adam_group_lr_and_weight_decay """
  116. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  117. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  118. net = Net()
  119. net.set_train()
  120. loss = nn.SoftmaxCrossEntropyWithLogits()
  121. net_with_loss = WithLossCell(net, loss)
  122. all_params = net.trainable_params()
  123. schedule_lr = nn.PolynomialDecayLR(0.01, 0.0001, 3, power=1.0)
  124. group_params = [{'params': [all_params[0]], 'lr': 0.02, 'weight_decay': 0.9},
  125. {'params': [all_params[1]]}]
  126. optimizer = nn.Adam(group_params, learning_rate=schedule_lr)
  127. train_network = TrainOneStepCell(net_with_loss, optimizer)
  128. _executor.compile(train_network, inputs, label)
  129. def test_adamweightdecay_group():
  130. """ test_adam_group_lr_and_weight_decay """
  131. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  132. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  133. net = Net()
  134. net.set_train()
  135. loss = nn.SoftmaxCrossEntropyWithLogits()
  136. net_with_loss = WithLossCell(net, loss)
  137. all_params = net.trainable_params()
  138. schedule_lr = nn.PolynomialDecayLR(0.01, 0.0001, 3, power=1.0)
  139. group_params = [{'params': [all_params[0]], 'lr': 0.02, 'weight_decay': 0.9},
  140. {'params': [all_params[1]]}]
  141. optimizer = nn.AdamWeightDecay(group_params, learning_rate=schedule_lr)
  142. train_network = TrainOneStepCell(net_with_loss, optimizer)
  143. _executor.compile(train_network, inputs, label)
  144. def test_adamoffload_group():
  145. """ test_adam_group_lr_and_weight_decay """
  146. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  147. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  148. net = Net()
  149. net.set_train()
  150. loss = nn.SoftmaxCrossEntropyWithLogits()
  151. net_with_loss = WithLossCell(net, loss)
  152. all_params = net.trainable_params()
  153. schedule_lr = nn.PolynomialDecayLR(0.01, 0.0001, 3, power=1.0)
  154. group_params = [{'params': [all_params[0]], 'lr': 0.02, 'weight_decay': 0.9},
  155. {'params': [all_params[1]]}]
  156. optimizer = nn.AdamOffload(group_params, learning_rate=schedule_lr)
  157. train_network = TrainOneStepCell(net_with_loss, optimizer)
  158. _executor.compile(train_network, inputs, label)
  159. def test_AdamWeightDecay_beta1():
  160. net = Net()
  161. print("**********", net.get_parameters())
  162. with pytest.raises(ValueError):
  163. AdamWeightDecay(net.get_parameters(), beta1=1.0, learning_rate=0.1)
  164. def test_AdamWeightDecay_beta2():
  165. net = Net()
  166. with pytest.raises(ValueError):
  167. AdamWeightDecay(net.get_parameters(), beta2=1.0, learning_rate=0.1)
  168. def test_AdamWeightDecay_e():
  169. net = Net()
  170. with pytest.raises(ValueError):
  171. AdamWeightDecay(net.get_parameters(), eps=-0.1, learning_rate=0.1)
  172. def test_adam_mindspore_with_empty_params():
  173. net = nn.Flatten()
  174. with pytest.raises(ValueError, match=r"Optimizer got an empty parameter list"):
  175. AdamWeightDecay(net.get_parameters())