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_rprop.py 4.1 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Rprop """
  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 Rprop
  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_rpropwithoutparam():
  41. """
  42. Feature: Test Rprop 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. Rprop(net.trainable_params(), learning_rate=0.1)
  50. def test_rprop_tuple():
  51. """
  52. Feature: Test Rprop optimizer.
  53. Description: Test if error is raised when the type of etas and step_sizes is not correct.
  54. Expectation: TypeError is raised.
  55. """
  56. net = Net()
  57. with pytest.raises(TypeError):
  58. Rprop(net.get_parameters(), etas=[0.5, 1.2], learning_rate=0.1)
  59. with pytest.raises(TypeError):
  60. Rprop(net.get_parameters(), step_sizes=[1e-6, 50.], learning_rate=0.1)
  61. def test_rprop_size():
  62. """
  63. Feature: Test Rprop optimizer.
  64. Description: Test if error is raised when the size of etas and step_sizes is not correct.
  65. Expectation: ValueError is raised.
  66. """
  67. net = Net()
  68. with pytest.raises(ValueError):
  69. Rprop(net.get_parameters(), etas=(0.5, 1.2, 1.3), learning_rate=0.1)
  70. with pytest.raises(ValueError):
  71. Rprop(net.get_parameters(), step_sizes=(1e-6, 50., 60.), learning_rate=0.1)
  72. def test_rprop_stepsize():
  73. """
  74. Feature: Test Rprop optimizer.
  75. Description: Test if error is raised when the value of step_sizes is not correct.
  76. Expectation: ValueError is raised.
  77. """
  78. net = Net()
  79. with pytest.raises(ValueError):
  80. Rprop(net.get_parameters(), step_sizes=(50., 1e-6), learning_rate=0.1)
  81. def test_rprop_etas():
  82. """
  83. Feature: Test Rprop optimizer.
  84. Description: Test if error is raised when the value range of etas is not correct.
  85. Expectation: ValueError is raised.
  86. """
  87. net = Net()
  88. with pytest.raises(ValueError):
  89. Rprop(net.get_parameters(), etas=(0.5, 0.9), learning_rate=0.1)
  90. with pytest.raises(ValueError):
  91. Rprop(net.get_parameters(), etas=(1., 1.2), learning_rate=0.1)
  92. with pytest.raises(ValueError):
  93. Rprop(net.get_parameters(), etas=(-0.1, 1.2), learning_rate=0.1)
  94. def test_rprop_mindspore_with_empty_params():
  95. """
  96. Feature: Test Rprop optimizer.
  97. Description: Test if error is raised when there is no trainable_params.
  98. Expectation: ValueError is raised.
  99. """
  100. net = nn.Flatten()
  101. with pytest.raises(ValueError, match=r"For 'Optimizer', the argument parameters must not be empty"):
  102. Rprop(net.get_parameters())