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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. return x
  72. x = (1, 2, 3)
  73. net = Net()
  74. assert net(x) == x
  75. def test_output_parameter_list():
  76. class Net(Cell):
  77. def __init__(self):
  78. super(Net, self).__init__()
  79. def construct(self, x):
  80. return x
  81. x = [1, 2, 3]
  82. net = Net()
  83. assert net(x) == x
  84. def test_output_parameter_int():
  85. class Net(Cell):
  86. def __init__(self):
  87. super(Net, self).__init__()
  88. def construct(self, x):
  89. return x
  90. x = 88
  91. net = Net()
  92. assert net(x) == x
  93. def test_output_parameter_str():
  94. class Net(Cell):
  95. def __init__(self):
  96. super(Net, self).__init__()
  97. def construct(self, x):
  98. return x
  99. x = "hello world"
  100. net = Net()
  101. assert net(x) == x
  102. def test_tuple_tuple_0():
  103. class Net(Cell):
  104. def __init__(self):
  105. super(Net, self).__init__()
  106. self.add = P.TensorAdd()
  107. self.sub = P.Sub()
  108. def construct(self, x, y):
  109. xx = self.add(x, x)
  110. yy = self.add(y, y)
  111. xxx = self.sub(x, x)
  112. yyy = self.sub(y, y)
  113. ret = ((xx, yy), (xxx, yyy))
  114. ret = (ret, ret)
  115. return ret
  116. net = Net()
  117. x = Tensor(np.ones([2], np.int32))
  118. y = Tensor(np.zeros([3], np.int32))
  119. net(x, y)
  120. def test_tuple_tuple_1():
  121. class Net(Cell):
  122. def __init__(self):
  123. super(Net, self).__init__()
  124. self.add = P.TensorAdd()
  125. self.sub = P.Sub()
  126. def construct(self, x, y):
  127. xx = self.add(x, x)
  128. yy = self.add(y, y)
  129. ret = ((xx, yy), x)
  130. ret = (ret, ret)
  131. return ret
  132. net = Net()
  133. x = Tensor(np.ones([2], np.int32))
  134. y = Tensor(np.zeros([3], np.int32))
  135. net(x, y)
  136. def test_tuple_tuple_2():
  137. class Net(Cell):
  138. def __init__(self):
  139. super(Net, self).__init__()
  140. self.add = P.TensorAdd()
  141. self.sub = P.Sub()
  142. self.relu = P.ReLU()
  143. self.depend = depend
  144. def construct(self, x, y):
  145. xx = self.add(x, x)
  146. yy = self.add(y, y)
  147. xxx = self.sub(x, x)
  148. yyy = self.sub(y, y)
  149. z = self.relu(x)
  150. ret = ((xx, yy), (xxx, yyy))
  151. ret = (ret, ret)
  152. ret = self.depend(ret, z)
  153. return ret
  154. net = Net()
  155. x = Tensor(np.ones([2], np.int32))
  156. y = Tensor(np.zeros([3], np.int32))
  157. net(x, y)
  158. def test_tuple_tuple_3():
  159. class Net(Cell):
  160. def __init__(self):
  161. super(Net, self).__init__()
  162. self.add = P.TensorAdd()
  163. self.sub = P.Sub()
  164. self.relu = P.ReLU()
  165. self.depend = depend
  166. def construct(self, x, y):
  167. xx = self.add(x, x)
  168. yy = self.add(y, y)
  169. z = self.relu(x)
  170. ret = ((xx, yy), x)
  171. ret = (ret, ret)
  172. ret = self.depend(ret, z)
  173. return ret
  174. net = Net()
  175. x = Tensor(np.ones([2], np.int32))
  176. y = Tensor(np.zeros([3], np.int32))
  177. net(x, y)
  178. def test_soft():
  179. class SoftmaxCrossEntropyWithLogitsNet(Cell):
  180. def __init__(self):
  181. super(SoftmaxCrossEntropyWithLogitsNet, self).__init__()
  182. self.soft = P.SoftmaxCrossEntropyWithLogits()
  183. self.value = (Tensor(np.zeros((2, 2)).astype(np.float32)), Tensor(np.ones((2, 2)).astype(np.float32)))
  184. def construct(self, x, y, z):
  185. xx = x + y
  186. yy = x - y
  187. ret = self.soft(xx, yy)
  188. ret = (ret, z)
  189. ret = (ret, self.value)
  190. return ret
  191. input1 = Tensor(np.zeros((2, 2)).astype(np.float32))
  192. input2 = Tensor(np.ones((2, 2)).astype(np.float32))
  193. input3 = Tensor((np.ones((2, 2)) + np.ones((2, 2))).astype(np.float32))
  194. net = SoftmaxCrossEntropyWithLogitsNet()
  195. net(input1, input2, input3)
  196. def test_const_depend():
  197. class ConstDepend(Cell):
  198. def __init__(self):
  199. super(ConstDepend, self).__init__()
  200. self.value = (Tensor(np.zeros((2, 3)).astype(np.float32)), Tensor(np.ones((2, 3)).astype(np.float32)))
  201. self.soft = P.SoftmaxCrossEntropyWithLogits()
  202. self.depend = depend
  203. def construct(self, x, y, z):
  204. ret = x + y
  205. ret = ret * z
  206. ret = self.depend(self.value, ret)
  207. ret = (ret, self.soft(x, y))
  208. return ret
  209. input1 = Tensor(np.zeros((2, 2)).astype(np.float32))
  210. input2 = Tensor(np.ones((2, 2)).astype(np.float32))
  211. input3 = Tensor((np.ones((2, 2)) + np.ones((2, 2))).astype(np.float32))
  212. net = ConstDepend()
  213. net(input1, input2, input3)