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_l1_regularizer.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2020 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 L1Regularizer """
  16. import math
  17. import numpy as np
  18. import mindspore.nn as nn
  19. import mindspore.context as context
  20. from mindspore import Tensor, ms_function
  21. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  22. class Net_l1_regularizer(nn.Cell):
  23. def __init__(self, scale):
  24. super(Net_l1_regularizer, self).__init__()
  25. self.l1_regularizer = nn.L1Regularizer(scale)
  26. @ms_function
  27. def construct(self, weights):
  28. return self.l1_regularizer(weights)
  29. def test_l1_regularizer02():
  30. scale = 0.0
  31. weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
  32. try:
  33. l1_regularizer = Net_l1_regularizer(scale)
  34. l1_regularizer(weights)
  35. except ValueError:
  36. assert True
  37. def test_l1_regularizer03():
  38. scale = -0.5
  39. weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
  40. try:
  41. l1_regularizer = Net_l1_regularizer(scale)
  42. l1_regularizer(weights)
  43. except ValueError:
  44. assert True
  45. def test_l1_regularizer04():
  46. scale = math.inf
  47. weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
  48. try:
  49. l1_regularizer = Net_l1_regularizer(scale)
  50. l1_regularizer(weights)
  51. except ValueError:
  52. assert True
  53. def test_l1_regularizer05():
  54. scale = math.nan
  55. weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
  56. try:
  57. l1_regularizer = Net_l1_regularizer(scale)
  58. l1_regularizer(weights)
  59. except ValueError:
  60. assert True
  61. def test_l1_regularizer06():
  62. scale = 0.5
  63. weights = "sss"
  64. try:
  65. l1_regularizer = Net_l1_regularizer(scale)
  66. l1_regularizer(weights)
  67. except TypeError:
  68. assert True
  69. def test_l1_regularizer07():
  70. scale = 0.5
  71. try:
  72. l1_regularizer = Net_l1_regularizer(scale)
  73. l1_regularizer()
  74. except TypeError:
  75. assert True
  76. def test_l1_regularizer09():
  77. scale = 0.5
  78. weights = Tensor([[False, False], [False, False]])
  79. try:
  80. net = nn.L1Regularizer(scale)
  81. net(weights)
  82. except TypeError:
  83. assert True