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 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import TrainOneStepCell, WithLossCell
  22. from mindspore.nn.optim import Adam, AdamWeightDecay, AdamWeightDecayDynamicLR
  23. from mindspore.ops import operations as P
  24. class Net(nn.Cell):
  25. """ Net definition """
  26. def __init__(self):
  27. super(Net, self).__init__()
  28. self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight")
  29. self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias")
  30. self.matmul = P.MatMul()
  31. self.biasAdd = P.BiasAdd()
  32. def construct(self, x):
  33. x = self.biasAdd(self.matmul(x, self.weight), self.bias)
  34. return x
  35. class NetWithoutWeight(nn.Cell):
  36. def __init__(self):
  37. super(NetWithoutWeight, self).__init__()
  38. self.matmul = P.MatMul()
  39. def construct(self, x):
  40. x = self.matmul(x, x)
  41. return x
  42. class NetWithSparseGatherV2(nn.Cell):
  43. """ NetWithSparseGatherV2 definition """
  44. def __init__(self):
  45. super(NetWithSparseGatherV2, self).__init__()
  46. self.weight1 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight1", sparse_grad=True)
  47. self.weight2 = Parameter(Tensor(np.ones([2, 1, 2]).astype((np.float32))), name="weight2")
  48. self.axis = 0
  49. self.gather = P.SparseGatherV2()
  50. def construct(self, indices, label):
  51. return self.gather(self.weight1, indices, self.axis) + self.weight2
  52. def test_adamwithoutparam():
  53. net = NetWithoutWeight()
  54. net.set_train()
  55. with pytest.raises(ValueError, match=r"Optimizer got an empty parameter list"):
  56. AdamWeightDecay(net.trainable_params(), learning_rate=0.1)
  57. def test_adamw_compile():
  58. """ test_adamw_compile """
  59. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  60. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  61. net = Net()
  62. net.set_train()
  63. loss = nn.SoftmaxCrossEntropyWithLogits()
  64. optimizer = AdamWeightDecay(net.trainable_params(), learning_rate=0.1)
  65. net_with_loss = WithLossCell(net, loss)
  66. train_network = TrainOneStepCell(net_with_loss, optimizer)
  67. _executor.compile(train_network, inputs, label)
  68. def test_adam_compile():
  69. """ test adam compile """
  70. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  71. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  72. net = Net()
  73. net.set_train()
  74. loss = nn.SoftmaxCrossEntropyWithLogits()
  75. optimizer = Adam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9)
  76. net_with_loss = WithLossCell(net, loss)
  77. train_network = TrainOneStepCell(net_with_loss, optimizer)
  78. _executor.compile(train_network, inputs, label)
  79. def test_sparse_adam_compile():
  80. """ test_sparse_adam_compile """
  81. indices = Tensor(np.array([0, 1]).astype(np.int32))
  82. label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
  83. net = NetWithSparseGatherV2()
  84. net.set_train()
  85. optimizer = Adam(net.trainable_params(), learning_rate=0.1, loss_scale=1024.0)
  86. train_network = TrainOneStepCell(net, optimizer)
  87. _executor.compile(train_network, indices, label)
  88. def test_AdamWeightDecay_beta1():
  89. net = Net()
  90. print("**********", net.get_parameters())
  91. with pytest.raises(ValueError):
  92. AdamWeightDecay(net.get_parameters(), beta1=1.0, learning_rate=0.1)
  93. def test_AdamWeightDecay_beta2():
  94. net = Net()
  95. with pytest.raises(ValueError):
  96. AdamWeightDecay(net.get_parameters(), beta2=1.0, learning_rate=0.1)
  97. def test_AdamWeightDecay_e():
  98. net = Net()
  99. with pytest.raises(ValueError):
  100. AdamWeightDecay(net.get_parameters(), eps=-0.1, learning_rate=0.1)
  101. def test_AdamWeightDecayDynamicLR():
  102. """ test_AdamWeightDecayDynamicLR """
  103. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  104. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  105. net = Net()
  106. net.set_train()
  107. loss = nn.SoftmaxCrossEntropyWithLogits()
  108. optimizer = AdamWeightDecayDynamicLR(net.trainable_params(), decay_steps=20, learning_rate=0.1)
  109. net_with_loss = WithLossCell(net, loss)
  110. train_network = TrainOneStepCell(net_with_loss, optimizer)
  111. _executor.compile(train_network, inputs, label)
  112. def test_adam_mindspore_with_empty_params():
  113. net = nn.Flatten()
  114. with pytest.raises(ValueError, match=r"Optimizer got an empty parameter list"):
  115. AdamWeightDecay(net.get_parameters())