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_initializer.py 20 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 mindspore
  16. from mindspore.common.initializer import initializer, Identity, Dirac, Sparse, VarianceScaling, Orthogonal
  17. import numpy as np
  18. def test_sparse():
  19. """
  20. Feature: Test sparse initializer.
  21. Description: Initialize a 2 dimension sparse matrix to fill the input tensor.
  22. Expectation: The Tensor is initialized with a 2 dimension sparse matrix.
  23. """
  24. tensor1 = initializer(Sparse(sparsity=0.1, sigma=0.01), [5, 8], mindspore.float32)
  25. tensor1.init_data()
  26. def test_orthogonal():
  27. """
  28. Feature: Test orthogonal initializer.
  29. Description: Initialize a (semi) orthogonal matrix to fill the input tensor.
  30. Expectation: The Tensor is initialized with values from orthogonal matrix.
  31. """
  32. tensor1 = initializer(Orthogonal(gain=2.), [2, 3, 4], mindspore.float32)
  33. tensor2 = initializer('orthogonal', [2, 3, 4], mindspore.float32)
  34. tensor1.init_data()
  35. tensor2.init_data()
  36. def test_variancescaling():
  37. """
  38. Feature: Test varianceScaling initializer.
  39. Description: Randomly initialize an array with scaling to fill the input tensor.
  40. Expectation: The Tensor is initialized successfully.
  41. """
  42. tensor1 = initializer('varianceScaling', [2, 3], mindspore.float32)
  43. tensor2 = initializer(VarianceScaling(scale=1.0, mode='fan_out', distribution='untruncated_normal'), [2, 3],
  44. mindspore.float32)
  45. tensor3 = initializer(VarianceScaling(scale=2.0, mode='fan_in', distribution='truncated_normal'), [2, 3],
  46. mindspore.float32)
  47. tensor4 = initializer(VarianceScaling(scale=3.0, mode='fan_avg', distribution='uniform'), [2, 3],
  48. mindspore.float32)
  49. tensor1.init_data()
  50. tensor2.init_data()
  51. tensor3.init_data()
  52. tensor4.init_data()
  53. def test_identity():
  54. """
  55. Feature: Test identity initializer.
  56. Description: Initialize an identity matrix to fill a Tensor.
  57. Expectation: The Tensor is initialized with identity matrix.
  58. """
  59. tensor1 = initializer(Identity(), [3, 3], mindspore.float32)
  60. tensor2 = initializer('identity', [3, 4], mindspore.float32)
  61. tensor3 = initializer('identity', [4, 3], mindspore.float32)
  62. expect1 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float32)
  63. expect2 = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]], dtype=np.float32)
  64. expect3 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]], dtype=np.float32)
  65. assert (tensor1.asnumpy() == expect1).all()
  66. assert (tensor2.asnumpy() == expect2).all()
  67. assert (tensor3.asnumpy() == expect3).all()
  68. def test_dirac():
  69. """
  70. Feature: Test dirac initializer.
  71. Description: Initialize input tensor with the Dirac delta function.
  72. Expectation: The Tensor is correctly initialized.
  73. """
  74. tensor3_1 = initializer(Dirac(groups=1), [6, 2, 3], mindspore.float32)
  75. tensor3_2 = initializer(Dirac(groups=2), [6, 2, 3], mindspore.float32)
  76. tensor3_3 = initializer(Dirac(groups=3), [6, 2, 3], mindspore.float32)
  77. tensor4_1 = initializer(Dirac(groups=1), [6, 4, 3, 3], mindspore.float32)
  78. tensor4_2 = initializer(Dirac(groups=2), [6, 4, 3, 3], mindspore.float32)
  79. tensor4_3 = initializer(Dirac(groups=3), [6, 4, 3, 3], mindspore.float32)
  80. tensor5_1 = initializer(Dirac(groups=1), [6, 2, 3, 3, 3], mindspore.float32)
  81. tensor5_2 = initializer(Dirac(groups=2), [6, 2, 3, 3, 3], mindspore.float32)
  82. tensor5_3 = initializer(Dirac(groups=3), [6, 2, 3, 3, 3], mindspore.float32)
  83. expectation3_1 = np.array([[[0., 1., 0.], [0., 0., 0.]],
  84. [[0., 0., 0.], [0., 1., 0.]],
  85. [[0., 0., 0.], [0., 0., 0.]],
  86. [[0., 0., 0.], [0., 0., 0.]],
  87. [[0., 0., 0.], [0., 0., 0.]],
  88. [[0., 0., 0.], [0., 0., 0.]]], dtype=np.float32)
  89. expectation3_2 = np.array([[[0., 1., 0.], [0., 0., 0.]],
  90. [[0., 0., 0.], [0., 1., 0.]],
  91. [[0., 0., 0.], [0., 0., 0.]],
  92. [[0., 1., 0.], [0., 0., 0.]],
  93. [[0., 0., 0.], [0., 1., 0.]],
  94. [[0., 0., 0.], [0., 0., 0.]]], dtype=np.float32)
  95. expectation3_3 = np.array([[[0., 1., 0.], [0., 0., 0.]],
  96. [[0., 0., 0.], [0., 1., 0.]],
  97. [[0., 1., 0.], [0., 0., 0.]],
  98. [[0., 0., 0.], [0., 1., 0.]],
  99. [[0., 1., 0.], [0., 0., 0.]],
  100. [[0., 0., 0.], [0., 1., 0.]]], dtype=np.float32)
  101. expectation4_1 = np.array([[[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  102. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  103. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  104. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  105. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  106. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  107. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  108. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  109. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  110. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  111. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  112. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  113. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  114. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  115. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  116. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]]],
  117. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  118. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  119. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  120. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  121. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  122. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  123. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  124. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]], dtype=np.float32)
  125. expectation4_2 = np.array([[[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  126. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  127. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  128. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  129. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  130. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  131. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  132. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  133. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  134. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  135. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  136. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  137. [[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  138. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  139. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  140. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  141. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  142. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  143. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  144. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  145. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  146. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  147. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  148. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]], dtype=np.float32)
  149. expectation4_3 = np.array([[[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  150. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  151. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  152. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  153. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  154. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  155. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  156. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  157. [[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  158. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  159. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  160. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  161. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  162. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  163. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  164. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  165. [[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  166. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  167. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  168. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  169. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  170. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  171. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  172. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]], dtype=np.float32)
  173. expectation5_1 = np.array([[[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  174. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  175. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  176. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  177. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  178. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  179. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  180. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  181. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  182. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  183. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  184. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  185. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  186. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  187. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  188. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  189. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  190. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  191. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  192. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  193. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  194. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  195. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  196. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  197. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  198. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  199. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  200. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  201. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  202. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  203. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  204. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  205. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  206. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  207. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  208. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]]], dtype=np.float32)
  209. expectation5_2 = np.array([[[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  210. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  211. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  212. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  213. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  214. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  215. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  216. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  217. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  218. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  219. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  220. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  221. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  222. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  223. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  224. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  225. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  226. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  227. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  228. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  229. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  230. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  231. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  232. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  233. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  234. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  235. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  236. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  237. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  238. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  239. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  240. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  241. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  242. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  243. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  244. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]]], dtype=np.float32)
  245. expectation5_3 = np.array([[[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  246. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  247. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  248. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  249. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  250. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  251. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  252. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  253. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  254. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  255. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  256. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  257. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  258. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  259. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  260. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  261. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  262. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  263. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  264. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  265. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  266. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  267. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  268. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  269. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  270. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  271. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  272. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  273. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  274. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
  275. [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  276. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  277. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
  278. [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
  279. [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
  280. [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]]], dtype=np.float32)
  281. assert (tensor3_1.asnumpy() == expectation3_1).all()
  282. assert (tensor3_2.asnumpy() == expectation3_2).all()
  283. assert (tensor3_3.asnumpy() == expectation3_3).all()
  284. assert (tensor4_1.asnumpy() == expectation4_1).all()
  285. assert (tensor4_2.asnumpy() == expectation4_2).all()
  286. assert (tensor4_3.asnumpy() == expectation4_3).all()
  287. assert (tensor5_1.asnumpy() == expectation5_1).all()
  288. assert (tensor5_2.asnumpy() == expectation5_2).all()
  289. assert (tensor5_3.asnumpy() == expectation5_3).all()