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_structure_output.py 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. """
  16. test_structure_output
  17. """
  18. import numpy as np
  19. import mindspore.ops.operations as P
  20. from mindspore import Tensor, context
  21. from mindspore.nn import Cell
  22. from mindspore.ops.functional import depend
  23. context.set_context(mode=context.GRAPH_MODE)
  24. def test_output_const_tuple():
  25. class Net(Cell):
  26. def __init__(self):
  27. super(Net, self).__init__()
  28. self.tuple_1 = (1, 2, 3)
  29. self.tuple_2 = (4, 5, 6)
  30. def construct(self):
  31. ret = self.tuple_1 + self.tuple_2
  32. return ret
  33. net = Net()
  34. assert net() == (1, 2, 3, 4, 5, 6)
  35. def test_output_const_list():
  36. class Net(Cell):
  37. def __init__(self):
  38. super(Net, self).__init__()
  39. self.tuple_1 = [1, 2, 3]
  40. def construct(self):
  41. ret = self.tuple_1
  42. return ret
  43. net = Net()
  44. assert net() == (1, 2, 3)
  45. def test_output_const_int():
  46. class Net(Cell):
  47. def __init__(self):
  48. super(Net, self).__init__()
  49. self.number_1 = 2
  50. self.number_2 = 3
  51. def construct(self):
  52. ret = self.number_1 + self.number_2
  53. return ret
  54. net = Net()
  55. assert net() == 5
  56. def test_output_const_str():
  57. class Net(Cell):
  58. def __init__(self):
  59. super(Net, self).__init__()
  60. self.str = "hello world"
  61. def construct(self):
  62. ret = self.str
  63. return ret
  64. net = Net()
  65. assert net() == "hello world"
  66. def test_output_parameter_tuple():
  67. class Net(Cell):
  68. def __init__(self):
  69. super(Net, self).__init__()
  70. def construct(self, x):
  71. ret = x
  72. return ret
  73. x = (1, 2, 3)
  74. net = Net()
  75. assert net(x) == x
  76. def test_output_parameter_list():
  77. class Net(Cell):
  78. def __init__(self):
  79. super(Net, self).__init__()
  80. def construct(self, x):
  81. ret = x
  82. return ret
  83. x = [1, 2, 3]
  84. net = Net()
  85. assert net(x) == x
  86. def test_output_parameter_int():
  87. class Net(Cell):
  88. def __init__(self):
  89. super(Net, self).__init__()
  90. def construct(self, x):
  91. ret = x
  92. return ret
  93. x = 88
  94. net = Net()
  95. assert net(x) == x
  96. def test_output_parameter_str():
  97. class Net(Cell):
  98. def __init__(self):
  99. super(Net, self).__init__()
  100. def construct(self, x):
  101. ret = x
  102. return ret
  103. x = "hello world"
  104. net = Net()
  105. assert net(x) == x
  106. def test_tuple_tuple_0():
  107. class Net(Cell):
  108. def __init__(self):
  109. super(Net, self).__init__()
  110. self.add = P.TensorAdd()
  111. self.sub = P.Sub()
  112. def construct(self, x, y):
  113. xx = self.add(x, x)
  114. yy = self.add(y, y)
  115. xxx = self.sub(x, x)
  116. yyy = self.sub(y, y)
  117. ret = ((xx, yy), (xxx, yyy))
  118. ret = (ret, ret)
  119. return ret
  120. net = Net()
  121. x = Tensor(np.ones([2], np.int32))
  122. y = Tensor(np.zeros([3], np.int32))
  123. net(x, y)
  124. def test_tuple_tuple_1():
  125. class Net(Cell):
  126. def __init__(self):
  127. super(Net, self).__init__()
  128. self.add = P.TensorAdd()
  129. self.sub = P.Sub()
  130. def construct(self, x, y):
  131. xx = self.add(x, x)
  132. yy = self.add(y, y)
  133. ret = ((xx, yy), x)
  134. ret = (ret, ret)
  135. return ret
  136. net = Net()
  137. x = Tensor(np.ones([2], np.int32))
  138. y = Tensor(np.zeros([3], np.int32))
  139. net(x, y)
  140. def test_tuple_tuple_2():
  141. class Net(Cell):
  142. def __init__(self):
  143. super(Net, self).__init__()
  144. self.add = P.TensorAdd()
  145. self.sub = P.Sub()
  146. self.relu = P.ReLU()
  147. self.depend = depend
  148. def construct(self, x, y):
  149. xx = self.add(x, x)
  150. yy = self.add(y, y)
  151. xxx = self.sub(x, x)
  152. yyy = self.sub(y, y)
  153. z = self.relu(x)
  154. ret = ((xx, yy), (xxx, yyy))
  155. ret = (ret, ret)
  156. ret = self.depend(ret, z)
  157. return ret
  158. net = Net()
  159. x = Tensor(np.ones([2], np.int32))
  160. y = Tensor(np.zeros([3], np.int32))
  161. net(x, y)
  162. def test_tuple_tuple_3():
  163. class Net(Cell):
  164. def __init__(self):
  165. super(Net, self).__init__()
  166. self.add = P.TensorAdd()
  167. self.sub = P.Sub()
  168. self.relu = P.ReLU()
  169. self.depend = depend
  170. def construct(self, x, y):
  171. xx = self.add(x, x)
  172. yy = self.add(y, y)
  173. z = self.relu(x)
  174. ret = ((xx, yy), x)
  175. ret = (ret, ret)
  176. ret = self.depend(ret, z)
  177. return ret
  178. net = Net()
  179. x = Tensor(np.ones([2], np.int32))
  180. y = Tensor(np.zeros([3], np.int32))
  181. net(x, y)
  182. def test_soft():
  183. class SoftmaxCrossEntropyWithLogitsNet(Cell):
  184. def __init__(self):
  185. super(SoftmaxCrossEntropyWithLogitsNet, self).__init__()
  186. self.soft = P.SoftmaxCrossEntropyWithLogits()
  187. self.value = (Tensor(np.zeros((2, 2)).astype(np.float32)), Tensor(np.ones((2, 2)).astype(np.float32)))
  188. def construct(self, x, y, z):
  189. xx = x + y
  190. yy = x - y
  191. ret = self.soft(xx, yy)
  192. ret = (ret, z)
  193. ret = (ret, self.value)
  194. return ret
  195. input1 = Tensor(np.zeros((2, 2)).astype(np.float32))
  196. input2 = Tensor(np.ones((2, 2)).astype(np.float32))
  197. input3 = Tensor((np.ones((2, 2)) + np.ones((2, 2))).astype(np.float32))
  198. net = SoftmaxCrossEntropyWithLogitsNet()
  199. net(input1, input2, input3)
  200. def test_const_depend():
  201. class ConstDepend(Cell):
  202. def __init__(self):
  203. super(ConstDepend, self).__init__()
  204. self.value = (Tensor(np.zeros((2, 3)).astype(np.float32)), Tensor(np.ones((2, 3)).astype(np.float32)))
  205. self.soft = P.SoftmaxCrossEntropyWithLogits()
  206. self.depend = depend
  207. def construct(self, x, y, z):
  208. ret = x + y
  209. ret = ret * z
  210. ret = self.depend(self.value, ret)
  211. ret = (ret, self.soft(x, y))
  212. return ret
  213. input1 = Tensor(np.zeros((2, 2)).astype(np.float32))
  214. input2 = Tensor(np.ones((2, 2)).astype(np.float32))
  215. input3 = Tensor((np.ones((2, 2)) + np.ones((2, 2))).astype(np.float32))
  216. net = ConstDepend()
  217. net(input1, input2, input3)