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_optimizer.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 optimizer """
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor
  19. from mindspore.common.parameter import Parameter
  20. from mindspore.nn.optim import Optimizer, SGD, Adam, AdamWeightDecay
  21. class IterableObjc:
  22. def __iter__(self):
  23. cont = 0
  24. while cont < 3:
  25. cont += 1
  26. yield Parameter(Tensor(cont), name="cont")
  27. params = IterableObjc()
  28. class TestOptimizer():
  29. def test_init(self):
  30. Optimizer(0.5, params)
  31. with pytest.raises(ValueError):
  32. Optimizer(-0.5, params)
  33. def test_construct(self):
  34. opt_2 = Optimizer(0.5, params)
  35. with pytest.raises(NotImplementedError):
  36. opt_2.construct()
  37. class TestAdam():
  38. """ TestAdam definition """
  39. def test_init(self):
  40. Adam(params, learning_rate=1e-3, beta1=0.9, beta2=0.999, eps=1e-8, use_locking=False,
  41. use_nesterov=False, weight_decay=0.0, loss_scale=1.0)
  42. def test_construct(self):
  43. with pytest.raises(RuntimeError):
  44. gradient = Tensor(np.zeros([1, 2, 3]))
  45. adam = Adam(params, learning_rate=1e-3, beta1=0.9, beta2=0.999, eps=1e-8, use_locking=False,
  46. use_nesterov=False, weight_decay=0.0, loss_scale=1.0)
  47. adam(gradient)
  48. class TestSGD():
  49. """ TestSGD definition """
  50. def test_init(self):
  51. with pytest.raises(ValueError):
  52. SGD(params, learning_rate=0.1, momentum=-0.1, dampening=0, weight_decay=0, nesterov=False)
  53. with pytest.raises(ValueError):
  54. SGD(params, learning_rate=0.12, momentum=-0.1, dampening=0, weight_decay=0, nesterov=False)
  55. SGD(params)
  56. class TestNullParam():
  57. """ TestNullParam definition """
  58. def test_optim_init(self):
  59. with pytest.raises(ValueError):
  60. Optimizer(0.1, None)
  61. def test_AdamWightDecay_init(self):
  62. with pytest.raises(ValueError):
  63. AdamWeightDecay(None)
  64. def test_Sgd_init(self):
  65. with pytest.raises(ValueError):
  66. SGD(None)
  67. class TestUnsupportParam():
  68. """ TestUnsupportParam definition """
  69. def test_optim_init(self):
  70. with pytest.raises(TypeError):
  71. Optimizer(0.1, (1, 2, 3))
  72. def test_AdamWightDecay_init(self):
  73. with pytest.raises(TypeError):
  74. AdamWeightDecay(9)
  75. def test_Sgd_init(self):
  76. with pytest.raises(TypeError):
  77. paramsTensor = Parameter(Tensor(np.zeros([1, 2, 3])), "x")
  78. SGD(paramsTensor)