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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.common import dtype as mstype
  20. from mindspore.common.initializer import initializer
  21. from mindspore._checkparam import _check_str_by_regular
  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. np_arr_int16 = np.ones([1,1], dtype=np.int16)
  58. np_arr_int32 = np.ones([1,1], dtype=np.int32)
  59. np_arr_float16 = np.ones([1,1], dtype=np.float16)
  60. np_arr_float32 = np.ones([1,1], dtype=np.float32)
  61. # with pytest.raises(ValueError):
  62. # Parameter(np_arr_int16[0][0], name=data_str)
  63. Parameter(np_arr_int32[0], name=data_str)
  64. Parameter(np_arr_float16[0], name=data_str)
  65. Parameter(np_arr_float32[0], name=data_str)
  66. Parameter(np_arr_float32, name=data_str)
  67. Parameter(tensor, name=data_str)
  68. Parameter(data_int, name=data_str)
  69. Parameter(dat, name=data_str)
  70. with pytest.raises(ValueError):
  71. Parameter(data_none, name=data_str)
  72. with pytest.raises(ValueError):
  73. Parameter(data_bool, name=data_str)
  74. with pytest.raises(ValueError):
  75. Parameter(data_str, name=data_str)
  76. Parameter(data_list, name=data_str)
  77. with pytest.raises(ValueError):
  78. Parameter(data_tuple, name=data_str)
  79. Parameter(tensor, name=data_str)
  80. Parameter(tensor, name=data_none)
  81. with pytest.raises(ValueError):
  82. Parameter(tensor, name=dat)
  83. with pytest.raises(ValueError):
  84. Parameter(tensor, name=tensor)
  85. with pytest.raises(ValueError):
  86. Parameter(tensor, name=data_bool)
  87. with pytest.raises(ValueError):
  88. Parameter(tensor, name=data_int)
  89. with pytest.raises(ValueError):
  90. Parameter(tensor, name=data_list)
  91. with pytest.raises(ValueError):
  92. Parameter(tensor, name=data_tuple)
  93. Parameter(tensor, name=data_str, requires_grad=data_bool)
  94. with pytest.raises(TypeError):
  95. Parameter(tensor, name=data_str, requires_grad=data_none)
  96. with pytest.raises(TypeError):
  97. Parameter(tensor, name=data_str, requires_grad=dat)
  98. with pytest.raises(TypeError):
  99. Parameter(tensor, name=data_str, requires_grad=tensor)
  100. with pytest.raises(TypeError):
  101. Parameter(tensor, name=data_str, requires_grad=data_str)
  102. with pytest.raises(TypeError):
  103. Parameter(tensor, name=data_str, requires_grad=data_int)
  104. with pytest.raises(TypeError):
  105. Parameter(tensor, name=data_str, requires_grad=data_list)
  106. with pytest.raises(TypeError):
  107. Parameter(tensor, name=data_str, requires_grad=data_tuple)
  108. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=data_bool)
  109. with pytest.raises(TypeError):
  110. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=dat)
  111. with pytest.raises(TypeError):
  112. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=tensor)
  113. with pytest.raises(TypeError):
  114. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=data_none)
  115. with pytest.raises(TypeError):
  116. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=data_str)
  117. with pytest.raises(TypeError):
  118. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=data_int)
  119. with pytest.raises(TypeError):
  120. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=data_list)
  121. with pytest.raises(TypeError):
  122. Parameter(tensor, name=data_str, requires_grad=data_bool,layerwise_parallel=data_tuple)
  123. def test_check_str_by_regular():
  124. str1 = "12_sf.asdf_"
  125. str2 = "x12_sf.asdf."
  126. str3 = "_x12_sf.asdf"
  127. str4 = ".12_sf.asdf"
  128. str5 = "12_sf.a$sdf."
  129. str6 = "12+sf.asdf"
  130. _check_str_by_regular(str1)
  131. _check_str_by_regular(str2)
  132. _check_str_by_regular(str3)
  133. with pytest.raises(ValueError):
  134. _check_str_by_regular(str4)
  135. with pytest.raises(ValueError):
  136. _check_str_by_regular(str5)
  137. with pytest.raises(ValueError):
  138. _check_str_by_regular(str6)

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.