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_parameter.py 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 parameter """
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor, Parameter, ParameterTuple
  19. from mindspore._checkparam import _check_str_by_regular
  20. from mindspore.common import dtype as mstype
  21. from mindspore.common.initializer import initializer
  22. def test_parameter_init():
  23. dat = np.array([[1, 2, 3], [2, 3, 4]])
  24. tensor = Tensor(dat)
  25. Parameter(tensor, name="testParameter", requires_grad=True, layerwise_parallel=False)
  26. def test_parameter_tuple_illegal():
  27. p1 = Parameter(initializer(0, [1], mstype.int32), name="global_step1")
  28. p2 = Parameter(initializer(0, [1], mstype.int32), name="global_step2")
  29. plist = [p1, p2]
  30. plist2 = [p1, "str"]
  31. ptuple = (p1, p2)
  32. ptuple_str = ("2", "1")
  33. pstr = "[2,3]"
  34. pnum = 3
  35. ParameterTuple(plist)
  36. ParameterTuple(ptuple)
  37. with pytest.raises(TypeError):
  38. ParameterTuple(p1)
  39. with pytest.raises(ValueError):
  40. ParameterTuple(plist2)
  41. with pytest.raises(ValueError):
  42. ParameterTuple(ptuple_str)
  43. with pytest.raises(ValueError):
  44. ParameterTuple(pstr)
  45. with pytest.raises(TypeError):
  46. ParameterTuple(pnum)
  47. def test_parameter_init_illegal():
  48. import numpy as np
  49. dat = np.array([[1, 2, 3], [2, 3, 4]])
  50. tensor = Tensor(dat)
  51. data_none = None
  52. data_bool = True
  53. data_str = "nicai"
  54. data_int = 3
  55. data_list = [1, "2", True]
  56. data_tuple = (1, 2, 3)
  57. # test data
  58. Parameter(tensor, name=data_str)
  59. Parameter(data_int, name=data_str)
  60. Parameter(dat, name=data_str)
  61. with pytest.raises(ValueError):
  62. Parameter(data_bool, name=data_str)
  63. # test name
  64. Parameter(tensor, name=data_none)
  65. with pytest.raises(ValueError):
  66. Parameter(tensor, name=dat)
  67. with pytest.raises(ValueError):
  68. Parameter(tensor, name=tensor)
  69. with pytest.raises(ValueError):
  70. Parameter(tensor, name=data_bool)
  71. with pytest.raises(ValueError):
  72. Parameter(tensor, name=data_int)
  73. with pytest.raises(ValueError):
  74. Parameter(tensor, name=data_list)
  75. with pytest.raises(ValueError):
  76. Parameter(tensor, name=data_tuple)
  77. Parameter(tensor, name=data_str, requires_grad=data_bool)
  78. with pytest.raises(TypeError):
  79. Parameter(tensor, name=data_str, requires_grad=data_none)
  80. with pytest.raises(TypeError):
  81. Parameter(tensor, name=data_str, requires_grad=dat)
  82. with pytest.raises(TypeError):
  83. Parameter(tensor, name=data_str, requires_grad=tensor)
  84. with pytest.raises(TypeError):
  85. Parameter(tensor, name=data_str, requires_grad=data_str)
  86. with pytest.raises(TypeError):
  87. Parameter(tensor, name=data_str, requires_grad=data_int)
  88. with pytest.raises(TypeError):
  89. Parameter(tensor, name=data_str, requires_grad=data_list)
  90. with pytest.raises(TypeError):
  91. Parameter(tensor, name=data_str, requires_grad=data_tuple)
  92. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=data_bool)
  93. with pytest.raises(TypeError):
  94. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=dat)
  95. with pytest.raises(TypeError):
  96. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=tensor)
  97. with pytest.raises(TypeError):
  98. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=data_none)
  99. with pytest.raises(TypeError):
  100. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=data_str)
  101. with pytest.raises(TypeError):
  102. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=data_int)
  103. with pytest.raises(TypeError):
  104. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=data_list)
  105. with pytest.raises(TypeError):
  106. Parameter(tensor, name=data_str, requires_grad=data_bool, layerwise_parallel=data_tuple)
  107. def test_check_str_by_regular():
  108. str1 = "12_sf.asdf_"
  109. str2 = "x12_sf.asdf."
  110. str3 = "_x12_sf.asdf"
  111. str4 = ".12_sf.asdf"
  112. str5 = "12_sf.a$sdf."
  113. str6 = "12+sf.asdf"
  114. _check_str_by_regular(str1)
  115. _check_str_by_regular(str2)
  116. _check_str_by_regular(str3)
  117. with pytest.raises(ValueError):
  118. _check_str_by_regular(str4)
  119. with pytest.raises(ValueError):
  120. _check_str_by_regular(str5)
  121. with pytest.raises(ValueError):
  122. _check_str_by_regular(str6)