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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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
  18. from mindspore.nn import Cell
  19. class Net1(Cell):
  20. def __init__(self):
  21. super().__init__()
  22. def construct(self, x):
  23. dic = {'x': 0, 'y': 1}
  24. output = []
  25. for i in dic.keys():
  26. output.append(i)
  27. for j in dic.values():
  28. output.append(j)
  29. return output
  30. class Net2(Cell):
  31. def __init__(self):
  32. super().__init__()
  33. def construct(self, x):
  34. dic = {'x': x, 'y': 1}
  35. output = []
  36. for i in dic.keys():
  37. output.append(i)
  38. for j in dic.values():
  39. output.append(j)
  40. return output
  41. class Net3(Cell):
  42. def __init__(self):
  43. super().__init__()
  44. def construct(self, x):
  45. dic = {'x': 0}
  46. dic['y'] = (0, 1)
  47. output = []
  48. for i in dic.keys():
  49. output.append(i)
  50. for j in dic.values():
  51. output.append(j)
  52. return output
  53. def test_dict1():
  54. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  55. input_me = Tensor(input_np)
  56. net = Net1()
  57. out_me = net(input_me)
  58. assert out_me == ('x', 'y', 0, 1)
  59. def test_dict2():
  60. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  61. input_me = Tensor(input_np)
  62. net = Net2()
  63. net(input_me)
  64. def test_dict3():
  65. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  66. input_me = Tensor(input_np)
  67. net = Net3()
  68. out_me = net(input_me)
  69. assert out_me == ('x', 'y', 0, (0, 1))