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_gpu.py 4.0 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. import numpy as np
  16. import mindspore.context as context
  17. from .optimizer_utils import build_network, loss_default_rprop, loss_group_rprop, loss_not_default_rprop
  18. def test_default_rprop_pynative():
  19. """
  20. Feature: Test Rprop optimizer
  21. Description: Test Rprop in Pynative mode with default parameter
  22. Expectation: Loss values and parameters conform to preset values.
  23. """
  24. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  25. config = {'name': 'Rprop', 'lr': 0.01, 'etas': (0.5, 1.2), 'step_sizes': (1e-6, 50.), 'weight_decay': 0.0}
  26. loss = build_network(config)
  27. assert np.allclose(loss_default_rprop, loss, atol=1.e-5)
  28. def test_default_rprop_graph():
  29. """
  30. Feature: Test Rprop optimizer
  31. Description: Test Rprop in Graph mode with default parameter
  32. Expectation: Loss values and parameters conform to preset values.
  33. """
  34. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  35. config = {'name': 'Rprop', 'lr': 0.01, 'etas': (0.5, 1.2), 'step_sizes': (1e-6, 50.), 'weight_decay': 0.0}
  36. loss = build_network(config)
  37. assert np.allclose(loss_default_rprop, loss, atol=1.e-5)
  38. def test_no_default_rprop_pynative():
  39. """
  40. Feature: Test Rprop optimizer
  41. Description: Test Rprop in Pynative mode with another set of parameter
  42. Expectation: Loss values and parameters conform to preset values.
  43. """
  44. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  45. config = {'name': 'Rprop', 'lr': 0.001, 'etas': (0.6, 1.9), 'step_sizes': (1e-3, 20.), 'weight_decay': 0.0}
  46. loss = build_network(config)
  47. assert np.allclose(loss_not_default_rprop, loss, atol=1.e-5)
  48. def test_no_default_rprop_graph():
  49. """
  50. Feature: Test Rprop optimizer
  51. Description: Test Rprop in Graph mode with another set of parameter
  52. Expectation: Loss values and parameters conform to preset values.
  53. """
  54. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  55. config = {'name': 'Rprop', 'lr': 0.001, 'etas': (0.6, 1.9), 'step_sizes': (1e-3, 20.), 'weight_decay': 0.0}
  56. loss = build_network(config)
  57. assert np.allclose(loss_not_default_rprop, loss, atol=1.e-5)
  58. def test_default_rprop_group_pynative():
  59. """
  60. Feature: Test Rprop optimizer
  61. Description: Test Rprop in Pynative mode with parameter grouping
  62. Expectation: Loss values and parameters conform to preset values.
  63. """
  64. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  65. config = {'name': 'Rprop', 'lr': 0.001, 'etas': (0.6, 1.9), 'step_sizes': (1e-2, 10.), 'weight_decay': 0.0}
  66. loss = build_network(config, is_group=True)
  67. assert np.allclose(loss_group_rprop, loss, atol=1.e-5)
  68. def test_default_rprop_group_graph():
  69. """
  70. Feature: Test Rprop optimizer
  71. Description: Test Rprop in Graph mode with parameter grouping
  72. Expectation: Loss values and parameters conform to preset values.
  73. """
  74. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  75. config = {'name': 'Rprop', 'lr': 0.001, 'etas': (0.6, 1.9), 'step_sizes': (1e-2, 10.), 'weight_decay': 0.0}
  76. loss = build_network(config, is_group=True)
  77. assert np.allclose(loss_group_rprop, loss, atol=1.e-5)