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_dictionary.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_dictionary """
  16. import numpy as np
  17. from mindspore import Tensor, context
  18. from mindspore.nn import Cell
  19. context.set_context(mode=context.GRAPH_MODE)
  20. class Net1(Cell):
  21. def __init__(self):
  22. super().__init__()
  23. def construct(self, x):
  24. dic = {'x': 0, 'y': 1}
  25. output = []
  26. for i in dic.keys():
  27. output.append(i)
  28. for j in dic.values():
  29. output.append(j)
  30. return output
  31. class Net2(Cell):
  32. def __init__(self):
  33. super().__init__()
  34. def construct(self, x):
  35. dic = {'x': x, 'y': 1}
  36. output = []
  37. for i in dic.keys():
  38. output.append(i)
  39. for j in dic.values():
  40. output.append(j)
  41. return output
  42. class Net3(Cell):
  43. def __init__(self):
  44. super().__init__()
  45. def construct(self, x):
  46. dic = {'x': 0}
  47. dic['y'] = (0, 1)
  48. output = []
  49. for i in dic.keys():
  50. output.append(i)
  51. for j in dic.values():
  52. output.append(j)
  53. return output
  54. def test_dict1():
  55. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  56. input_me = Tensor(input_np)
  57. net = Net1()
  58. out_me = net(input_me)
  59. assert out_me == ('x', 'y', 0, 1)
  60. def test_dict2():
  61. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  62. input_me = Tensor(input_np)
  63. net = Net2()
  64. net(input_me)
  65. def test_dict3():
  66. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  67. input_me = Tensor(input_np)
  68. net = Net3()
  69. out_me = net(input_me)
  70. assert out_me == ('x', 'y', 0, (0, 1))
  71. def test_dict4():
  72. class Net(Cell):
  73. def __init__(self):
  74. super().__init__()
  75. def construct(self, tuple_x):
  76. output = tuple_x + tuple_x
  77. return output
  78. x = (1, Tensor([1, 2, 3]), {"a": Tensor([1, 2, 3]), "b": 1})
  79. net = Net()
  80. out_me = net(x)
  81. assert out_me == x + x