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_cpu.py 3.9 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_graph():
  19. """
  20. Feature: Test ASGD optimizer
  21. Description: Test ASGD in Graph 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.GRAPH_MODE, device_target='CPU')
  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_no_default_asgd_graph():
  35. """
  36. Feature: Test ASGD optimizer
  37. Description: Test ASGD in Graph mode with another set of parameter
  38. Expectation: Loss values and parameters conform to preset values.
  39. """
  40. from .optimizer_utils import no_default_fc1_weight_asgd, \
  41. no_default_fc1_bias_asgd, no_default_fc2_weight_asgd, no_default_fc2_bias_asgd
  42. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  43. config = {'name': 'ASGD', 'lr': 0.001, 'lambd': 1e-3, 'alpha': 0.8, 't0': 50., 'weight_decay': 0.001}
  44. loss, cells = build_network(config)
  45. assert np.allclose(cells.ax[0].asnumpy(), no_default_fc1_weight_asgd, atol=1.e-5)
  46. assert np.allclose(cells.ax[1].asnumpy(), no_default_fc1_bias_asgd, atol=1.e-5)
  47. assert np.allclose(cells.ax[2].asnumpy(), no_default_fc2_weight_asgd, atol=1.e-5)
  48. assert np.allclose(cells.ax[3].asnumpy(), no_default_fc2_bias_asgd, atol=1.e-5)
  49. assert np.allclose(loss_not_default_asgd, loss, atol=1.e-5, rtol=1e-3)
  50. def test_default_asgd_group_graph():
  51. """
  52. Feature: Test ASGD optimizer
  53. Description: Test ASGD in Graph mode with parameter grouping
  54. Expectation: Loss values and parameters conform to preset values.
  55. """
  56. from .optimizer_utils import no_default_group_fc1_weight_asgd, no_default_group_fc1_bias_asgd, \
  57. no_default_group_fc2_weight_asgd, no_default_group_fc2_bias_asgd
  58. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  59. config = {'name': 'ASGD', 'lr': 0.1, 'lambd': 1e-3, 'alpha': 0.8, 't0': 50., 'weight_decay': 0.001}
  60. loss, cells = build_network(config, is_group=True)
  61. assert np.allclose(cells.ax[0].asnumpy(), no_default_group_fc1_weight_asgd, atol=1.e-5)
  62. assert np.allclose(cells.ax[1].asnumpy(), no_default_group_fc1_bias_asgd, atol=1.e-5)
  63. assert np.allclose(cells.ax[2].asnumpy(), no_default_group_fc2_weight_asgd, atol=1.e-5)
  64. assert np.allclose(cells.ax[3].asnumpy(), no_default_group_fc2_bias_asgd, atol=1.e-5)
  65. assert np.allclose(loss_group_asgd, loss, atol=1.e-5, rtol=1e-3)