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

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. import numpy as np
  16. import mindspore.context as context
  17. from .optimizer_utils import build_network, loss_not_default_asgd, loss_default_asgd, loss_group_asgd
  18. def test_default_asgd_pynative():
  19. """
  20. Feature: Test ASGD optimizer
  21. Description: Test ASGD in Pynative mode with default parameter
  22. Expectation: Loss values and parameters conform to preset values.
  23. """
  24. from .optimizer_utils import default_fc1_weight_asgd, \
  25. default_fc1_bias_asgd, default_fc2_weight_asgd, default_fc2_bias_asgd
  26. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  27. config = {'name': 'ASGD', 'lr': 0.01, 'lambd': 1e-4, 'alpha': 0.75, 't0': 1e6, 'weight_decay': 0.0}
  28. loss, cells = build_network(config)
  29. assert np.allclose(cells.ax[0].asnumpy(), default_fc1_weight_asgd, atol=1.e-5)
  30. assert np.allclose(cells.ax[1].asnumpy(), default_fc1_bias_asgd, atol=1.e-5)
  31. assert np.allclose(cells.ax[2].asnumpy(), default_fc2_weight_asgd, atol=1.e-5)
  32. assert np.allclose(cells.ax[3].asnumpy(), default_fc2_bias_asgd, atol=1.e-5)
  33. assert np.allclose(loss_default_asgd, loss, atol=1.e-5)
  34. def test_default_asgd_graph():
  35. """
  36. Feature: Test ASGD optimizer
  37. Description: Test ASGD in Graph mode with default parameter
  38. Expectation: Loss values and parameters conform to preset values.
  39. """
  40. from .optimizer_utils import default_fc1_weight_asgd, \
  41. default_fc1_bias_asgd, default_fc2_weight_asgd, default_fc2_bias_asgd
  42. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  43. config = {'name': 'ASGD', 'lr': 0.01, 'lambd': 1e-4, 'alpha': 0.75, 't0': 1e6, 'weight_decay': 0.0}
  44. loss, cells = build_network(config)
  45. assert np.allclose(cells.ax[0].asnumpy(), default_fc1_weight_asgd, atol=1.e-5)
  46. assert np.allclose(cells.ax[1].asnumpy(), default_fc1_bias_asgd, atol=1.e-5)
  47. assert np.allclose(cells.ax[2].asnumpy(), default_fc2_weight_asgd, atol=1.e-5)
  48. assert np.allclose(cells.ax[3].asnumpy(), default_fc2_bias_asgd, atol=1.e-5)
  49. assert np.allclose(loss_default_asgd, loss, atol=1.e-5)
  50. def test_no_default_asgd_pynative():
  51. """
  52. Feature: Test ASGD optimizer
  53. Description: Test ASGD in Pynative mode with another set of parameter
  54. Expectation: Loss values and parameters conform to preset values.
  55. """
  56. from .optimizer_utils import no_default_fc1_weight_asgd, \
  57. no_default_fc1_bias_asgd, no_default_fc2_weight_asgd, no_default_fc2_bias_asgd
  58. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  59. config = {'name': 'ASGD', 'lr': 0.001, 'lambd': 1e-3, 'alpha': 0.8, 't0': 50., 'weight_decay': 0.001}
  60. loss, cells = build_network(config)
  61. assert np.allclose(cells.ax[0].asnumpy(), no_default_fc1_weight_asgd, atol=1.e-5)
  62. assert np.allclose(cells.ax[1].asnumpy(), no_default_fc1_bias_asgd, atol=1.e-5)
  63. assert np.allclose(cells.ax[2].asnumpy(), no_default_fc2_weight_asgd, atol=1.e-5)
  64. assert np.allclose(cells.ax[3].asnumpy(), no_default_fc2_bias_asgd, atol=1.e-5)
  65. assert np.allclose(loss_not_default_asgd, loss, atol=1.e-5, rtol=1e-3)
  66. def test_no_default_asgd_graph():
  67. """
  68. Feature: Test ASGD optimizer
  69. Description: Test ASGD in Graph mode with another set of parameter
  70. Expectation: Loss values and parameters conform to preset values.
  71. """
  72. from .optimizer_utils import no_default_fc1_weight_asgd, \
  73. no_default_fc1_bias_asgd, no_default_fc2_weight_asgd, no_default_fc2_bias_asgd
  74. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  75. config = {'name': 'ASGD', 'lr': 0.001, 'lambd': 1e-3, 'alpha': 0.8, 't0': 50., 'weight_decay': 0.001}
  76. loss, cells = build_network(config)
  77. assert np.allclose(cells.ax[0].asnumpy(), no_default_fc1_weight_asgd, atol=1.e-5)
  78. assert np.allclose(cells.ax[1].asnumpy(), no_default_fc1_bias_asgd, atol=1.e-5)
  79. assert np.allclose(cells.ax[2].asnumpy(), no_default_fc2_weight_asgd, atol=1.e-5)
  80. assert np.allclose(cells.ax[3].asnumpy(), no_default_fc2_bias_asgd, atol=1.e-5)
  81. assert np.allclose(loss_not_default_asgd, loss, atol=1.e-5, rtol=1e-3)
  82. def test_default_asgd_group_pynative():
  83. """
  84. Feature: Test ASGD optimizer
  85. Description: Test ASGD in Pynative mode with parameter grouping
  86. Expectation: Loss values and parameters conform to preset values.
  87. """
  88. from .optimizer_utils import no_default_group_fc1_weight_asgd, no_default_group_fc1_bias_asgd, \
  89. no_default_group_fc2_weight_asgd, no_default_group_fc2_bias_asgd
  90. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  91. config = {'name': 'ASGD', 'lr': 0.1, 'lambd': 1e-3, 'alpha': 0.8, 't0': 50., 'weight_decay': 0.001}
  92. loss, cells = build_network(config, is_group=True)
  93. assert np.allclose(cells.ax[0].asnumpy(), no_default_group_fc1_weight_asgd, atol=1.e-5)
  94. assert np.allclose(cells.ax[1].asnumpy(), no_default_group_fc1_bias_asgd, atol=1.e-5)
  95. assert np.allclose(cells.ax[2].asnumpy(), no_default_group_fc2_weight_asgd, atol=1.e-5)
  96. assert np.allclose(cells.ax[3].asnumpy(), no_default_group_fc2_bias_asgd, atol=1.e-5)
  97. assert np.allclose(loss_group_asgd, loss, atol=1.e-5, rtol=1e-3)
  98. def test_default_asgd_group_graph():
  99. """
  100. Feature: Test ASGD optimizer
  101. Description: Test ASGD in Graph mode with parameter grouping
  102. Expectation: Loss values and parameters conform to preset values.
  103. """
  104. from .optimizer_utils import no_default_group_fc1_weight_asgd, no_default_group_fc1_bias_asgd, \
  105. no_default_group_fc2_weight_asgd, no_default_group_fc2_bias_asgd
  106. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  107. config = {'name': 'ASGD', 'lr': 0.1, 'lambd': 1e-3, 'alpha': 0.8, 't0': 50., 'weight_decay': 0.001}
  108. loss, cells = build_network(config, is_group=True)
  109. assert np.allclose(cells.ax[0].asnumpy(), no_default_group_fc1_weight_asgd, atol=1.e-5)
  110. assert np.allclose(cells.ax[1].asnumpy(), no_default_group_fc1_bias_asgd, atol=1.e-5)
  111. assert np.allclose(cells.ax[2].asnumpy(), no_default_group_fc2_weight_asgd, atol=1.e-5)
  112. assert np.allclose(cells.ax[3].asnumpy(), no_default_group_fc2_bias_asgd, atol=1.e-5)
  113. assert np.allclose(loss_group_asgd, loss, atol=1.e-5, rtol=1e-3)