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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. """ test enumerate"""
  16. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore import context
  19. context.set_context(mode=context.GRAPH_MODE)
  20. def test_list_index_1D():
  21. class Net(nn.Cell):
  22. def __init__(self):
  23. super(Net, self).__init__()
  24. def construct(self):
  25. list_ = [[1], [2, 2], [3, 3, 3]]
  26. list_[0] = [100]
  27. return list_
  28. net = Net()
  29. out = net()
  30. assert out[0] == [100]
  31. assert out[1] == [2, 2]
  32. assert out[2] == [3, 3, 3]
  33. def test_list_neg_index_1D():
  34. class Net(nn.Cell):
  35. def __init__(self):
  36. super(Net, self).__init__()
  37. def construct(self):
  38. list_ = [[1], [2, 2], [3, 3, 3]]
  39. list_[-3] = [100]
  40. return list_
  41. net = Net()
  42. out = net()
  43. assert out[0] == [100]
  44. assert out[1] == [2, 2]
  45. assert out[2] == [3, 3, 3]
  46. def test_list_index_2D():
  47. class Net(nn.Cell):
  48. def __init__(self):
  49. super(Net, self).__init__()
  50. def construct(self):
  51. list_ = [[1], [2, 2], [3, 3, 3]]
  52. list_[1][0] = 200
  53. list_[1][1] = 201
  54. return list_
  55. net = Net()
  56. out = net()
  57. assert out[0] == [1]
  58. assert out[1] == [200, 201]
  59. assert out[2] == [3, 3, 3]
  60. def test_list_neg_index_2D():
  61. class Net(nn.Cell):
  62. def __init__(self):
  63. super(Net, self).__init__()
  64. def construct(self):
  65. list_ = [[1], [2, 2], [3, 3, 3]]
  66. list_[1][-2] = 200
  67. list_[1][-1] = 201
  68. return list_
  69. net = Net()
  70. out = net()
  71. assert out[0] == [1]
  72. assert out[1] == [200, 201]
  73. assert out[2] == [3, 3, 3]
  74. def test_list_index_3D():
  75. class Net(nn.Cell):
  76. def __init__(self):
  77. super(Net, self).__init__()
  78. def construct(self):
  79. list_ = [[1], [2, 2], [[3, 3, 3]]]
  80. list_[2][0][0] = 300
  81. list_[2][0][1] = 301
  82. list_[2][0][2] = 302
  83. return list_
  84. net = Net()
  85. out = net()
  86. assert out[0] == [1]
  87. assert out[1] == [2, 2]
  88. assert out[2] == [[300, 301, 302]]
  89. def test_list_neg_index_3D():
  90. class Net(nn.Cell):
  91. def __init__(self):
  92. super(Net, self).__init__()
  93. def construct(self):
  94. list_ = [[1], [2, 2], [[3, 3, 3]]]
  95. list_[2][0][-3] = 300
  96. list_[2][0][-2] = 301
  97. list_[2][0][-1] = 302
  98. return list_
  99. net = Net()
  100. out = net()
  101. assert out[0] == [1]
  102. assert out[1] == [2, 2]
  103. assert out[2] == [[300, 301, 302]]
  104. def test_list_index_1D_parameter():
  105. class Net(nn.Cell):
  106. def __init__(self):
  107. super(Net, self).__init__()
  108. def construct(self, x):
  109. list_ = [x]
  110. list_[0] = 100
  111. return list_
  112. net = Net()
  113. net(Tensor(0))
  114. def test_list_index_2D_parameter():
  115. class Net(nn.Cell):
  116. def __init__(self):
  117. super(Net, self).__init__()
  118. def construct(self, x):
  119. list_ = [[x, x]]
  120. list_[0][0] = 100
  121. return list_
  122. net = Net()
  123. net(Tensor(0))
  124. def test_list_index_3D_parameter():
  125. class Net(nn.Cell):
  126. def __init__(self):
  127. super(Net, self).__init__()
  128. def construct(self, x):
  129. list_ = [[[x, x]]]
  130. list_[0][0][0] = 100
  131. return list_
  132. net = Net()
  133. net(Tensor(0))