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_sequence_assign.py 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright 2020-2022 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 enumerate"""
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore import context
  20. from mindspore.ops import operations as P
  21. from mindspore.ops import composite as C
  22. context.set_context(mode=context.GRAPH_MODE)
  23. def test_list_index_1D():
  24. class Net(nn.Cell):
  25. def construct(self):
  26. list_ = [[1], [2, 2], [3, 3, 3]]
  27. list_[0] = [100]
  28. return list_
  29. net = Net()
  30. out = net()
  31. assert list(out[0]) == [100]
  32. assert list(out[1]) == [2, 2]
  33. assert list(out[2]) == [3, 3, 3]
  34. def test_list_neg_index_1D():
  35. class Net(nn.Cell):
  36. def construct(self):
  37. list_ = [[1], [2, 2], [3, 3, 3]]
  38. list_[-3] = [100]
  39. return list_
  40. net = Net()
  41. out = net()
  42. assert list(out[0]) == [100]
  43. assert list(out[1]) == [2, 2]
  44. assert list(out[2]) == [3, 3, 3]
  45. def test_list_index_2D():
  46. class Net(nn.Cell):
  47. def construct(self):
  48. list_ = [[1], [2, 2], [3, 3, 3]]
  49. list_[1][0] = 200
  50. list_[1][1] = 201
  51. return list_
  52. net = Net()
  53. out = net()
  54. assert list(out[0]) == [1]
  55. assert list(out[1]) == [200, 201]
  56. assert list(out[2]) == [3, 3, 3]
  57. def test_list_neg_index_2D():
  58. class Net(nn.Cell):
  59. def construct(self):
  60. list_ = [[1], [2, 2], [3, 3, 3]]
  61. list_[1][-2] = 200
  62. list_[1][-1] = 201
  63. return list_
  64. net = Net()
  65. out = net()
  66. assert list(out[0]) == [1]
  67. assert list(out[1]) == [200, 201]
  68. assert list(out[2]) == [3, 3, 3]
  69. def test_list_index_3D():
  70. class Net(nn.Cell):
  71. def construct(self):
  72. list_ = [[1], [2, 2], [[3, 3, 3]]]
  73. list_[2][0][0] = 300
  74. list_[2][0][1] = 301
  75. list_[2][0][2] = 302
  76. return list_
  77. net = Net()
  78. out = net()
  79. assert list(out[0]) == [1]
  80. assert list(out[1]) == [2, 2]
  81. assert list(out[2][0]) == [300, 301, 302]
  82. def test_list_neg_index_3D():
  83. class Net(nn.Cell):
  84. def construct(self):
  85. list_ = [[1], [2, 2], [[3, 3, 3]]]
  86. list_[2][0][-3] = 300
  87. list_[2][0][-2] = 301
  88. list_[2][0][-1] = 302
  89. return list_
  90. net = Net()
  91. out = net()
  92. assert list(out[0]) == [1]
  93. assert list(out[1]) == [2, 2]
  94. assert list(out[2][0]) == [300, 301, 302]
  95. def test_list_index_1D_parameter():
  96. class Net(nn.Cell):
  97. def construct(self, x):
  98. list_ = [x]
  99. list_[0] = 100
  100. return list_
  101. net = Net()
  102. net(Tensor(0))
  103. def test_list_index_2D_parameter():
  104. class Net(nn.Cell):
  105. def construct(self, x):
  106. list_ = [[x, x]]
  107. list_[0][0] = 100
  108. return list_
  109. net = Net()
  110. net(Tensor(0))
  111. def test_list_index_3D_parameter():
  112. class Net(nn.Cell):
  113. def construct(self, x):
  114. list_ = [[[x, x]]]
  115. list_[0][0][0] = 100
  116. return list_
  117. net = Net()
  118. net(Tensor(0))
  119. def test_const_list_index_3D_bprop():
  120. class Net(nn.Cell):
  121. def __init__(self):
  122. super(Net, self).__init__()
  123. self.value = [[1], [2, 2], [[3, 3], [3, 3]]]
  124. self.relu = P.ReLU()
  125. def construct(self, input_x):
  126. list_x = self.value
  127. list_x[2][0][1] = input_x
  128. return self.relu(list_x[2][0][1])
  129. class GradNet(nn.Cell):
  130. def __init__(self, net):
  131. super(GradNet, self).__init__()
  132. self.net = net
  133. self.grad_all_with_sens = C.GradOperation(get_all=True, sens_param=True)
  134. def construct(self, x, sens):
  135. return self.grad_all_with_sens(self.net)(x, sens)
  136. net = Net()
  137. grad_net = GradNet(net)
  138. x = Tensor(np.arange(2 * 3).reshape(2, 3))
  139. sens = Tensor(np.arange(2 * 3).reshape(2, 3))
  140. grad_net(x, sens)
  141. def test_parameter_list_index_3D_bprop():
  142. class Net(nn.Cell):
  143. def __init__(self):
  144. super(Net, self).__init__()
  145. self.value = [[1], [2, 2], [[3, 3], [3, 3]]]
  146. self.relu = P.ReLU()
  147. def construct(self, x, value):
  148. list_value = [[x], [x, x], [[x, x], [x, x]]]
  149. list_value[2][0][1] = value
  150. return self.relu(list_value[2][0][1])
  151. class GradNet(nn.Cell):
  152. def __init__(self, net):
  153. super(GradNet, self).__init__()
  154. self.net = net
  155. self.grad_all_with_sens = C.GradOperation(get_all=True, sens_param=True)
  156. def construct(self, x, value, sens):
  157. return self.grad_all_with_sens(self.net)(x, value, sens)
  158. net = Net()
  159. grad_net = GradNet(net)
  160. x = Tensor(np.arange(2 * 3).reshape(2, 3))
  161. value = Tensor(np.ones((2, 3), np.int64))
  162. sens = Tensor(np.arange(2 * 3).reshape(2, 3))
  163. grad_net(x, value, sens)