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 6.9 kB

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