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_logic_not.py 3.1 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright 2021 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 syntax for logic expression """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore import context
  19. from mindspore.common.tensor import Tensor
  20. context.set_context(mode=context.GRAPH_MODE)
  21. class LogicNot(nn.Cell):
  22. def __init__(self):
  23. super(LogicNot, self).__init__()
  24. self.m = 1
  25. def construct(self, x):
  26. not_v = not x
  27. return not_v
  28. class LogicNotSpec(nn.Cell):
  29. def __init__(self, x):
  30. super(LogicNotSpec, self).__init__()
  31. self.x = x
  32. def construct(self, x):
  33. not_v = not self.x
  34. return not_v
  35. def test_ms_syntax_operator_logic_not_int():
  36. net = LogicNot()
  37. ret = net(1)
  38. print(ret)
  39. def test_ms_syntax_operator_logic_not_float():
  40. net = LogicNot()
  41. ret = net(1.89)
  42. print(ret)
  43. def test_ms_syntax_operator_logic_tensor_1_int():
  44. net = LogicNot()
  45. x = Tensor(np.ones([1], np.int32))
  46. ret = net(x)
  47. print(ret)
  48. def test_ms_syntax_operator_logic_not_tensor_1_float():
  49. net = LogicNot()
  50. x = Tensor(np.ones([1], np.float))
  51. ret = net(x)
  52. print(ret)
  53. def test_ms_syntax_operator_logic_not_tensor_2X2_int():
  54. net = LogicNot()
  55. x = Tensor(np.ones([2, 2], np.int32))
  56. ret = net(x)
  57. print(ret)
  58. def test_ms_syntax_operator_logic_not_tensor_2X2_float():
  59. net = LogicNot()
  60. x = Tensor(np.ones([2, 2], np.float))
  61. ret = net(x)
  62. print(ret)
  63. def test_ms_syntax_operator_logic_not_str():
  64. net = LogicNotSpec("cba")
  65. ret = net(1)
  66. print(ret)
  67. def test_ms_syntax_operator_logic_not_list_int():
  68. net = LogicNot()
  69. ret = net([1, 2, 3])
  70. print(ret)
  71. def test_ms_syntax_operator_logic_not_list_float():
  72. net = LogicNot()
  73. ret = net([1.0, 2.0, 3.0])
  74. print(ret)
  75. def test_ms_syntax_operator_logic_not_list_str():
  76. net = LogicNotSpec(["1", "2", "3"])
  77. ret = net(1)
  78. print(ret)
  79. def test_ms_syntax_operator_logic_not_list_combine():
  80. net = LogicNotSpec([1, "2", 3])
  81. ret = net(1)
  82. print(ret)
  83. def test_ms_syntax_operator_logic_not_tuple_int():
  84. net = LogicNot()
  85. ret = net((1, 2, 3))
  86. print(ret)
  87. def test_ms_syntax_operator_logic_not_tuple_float():
  88. net = LogicNot()
  89. ret = net((1.0, 2.0, 3.0))
  90. print(ret)
  91. def test_ms_syntax_operator_logic_not_tuple_str():
  92. net = LogicNotSpec(("1", "2", "3"))
  93. ret = net(1)
  94. print(ret)
  95. def test_ms_syntax_operator_logic_not_tuple_combine():
  96. net = LogicNotSpec((1, "2", 3))
  97. ret = net(1)
  98. print(ret)