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

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright 2021 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 ASGD """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter
  20. from mindspore.nn.optim import ASGD
  21. from mindspore.ops import operations as P
  22. class Net(nn.Cell):
  23. """ Net definition """
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight")
  27. self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias")
  28. self.matmul = P.MatMul()
  29. self.biasAdd = P.BiasAdd()
  30. def construct(self, x):
  31. x = self.biasAdd(self.matmul(x, self.weight), self.bias)
  32. return x
  33. class NetWithoutWeight(nn.Cell):
  34. def __init__(self):
  35. super(NetWithoutWeight, self).__init__()
  36. self.matmul = P.MatMul()
  37. def construct(self, x):
  38. x = self.matmul(x, x)
  39. return x
  40. def test_asgdwithoutparam():
  41. """
  42. Feature: Test ASGD optimizer.
  43. Description: Test if error is raised when trainable_params is empty.
  44. Expectation: ValueError is raised.
  45. """
  46. net = NetWithoutWeight()
  47. net.set_train()
  48. with pytest.raises(ValueError, match=r"For 'Optimizer', the argument parameters must not be empty"):
  49. ASGD(net.trainable_params(), learning_rate=0.1)
  50. def test_asgd_lambd():
  51. """
  52. Feature: Test ASGD optimizer.
  53. Description: Test if error is raised when the type of lambd is not correct.
  54. Expectation: ValueError is raised.
  55. """
  56. net = Net()
  57. with pytest.raises(TypeError):
  58. ASGD(net.get_parameters(), lambd=1, learning_rate=0.1)
  59. def test_asgd_alpha():
  60. """
  61. Feature: Test ASGD optimizer.
  62. Description: Test if error is raised when the type of alpha is not correct.
  63. Expectation: ValueError is raised.
  64. """
  65. net = Net()
  66. with pytest.raises(TypeError):
  67. ASGD(net.get_parameters(), alpha=1, learning_rate=0.1)
  68. def test_asgd_t0():
  69. """
  70. Feature: Test ASGD optimizer.
  71. Description: Test if error is raised when the type of t0 is not correct.
  72. Expectation: ValueError is raised.
  73. """
  74. net = Net()
  75. with pytest.raises(TypeError):
  76. ASGD(net.get_parameters(), t0=1, learning_rate=0.1)
  77. def test_asgd_mindspore_with_empty_params():
  78. """
  79. Feature: Test ASGD optimizer.
  80. Description: Test if error is raised when there is no trainable_params.
  81. Expectation: ValueError is raised.
  82. """
  83. net = nn.Flatten()
  84. with pytest.raises(ValueError, match=r"For 'Optimizer', the argument parameters must not be empty"):
  85. ASGD(net.get_parameters())