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_equal_op.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.common.initializer import initializer
  21. from mindspore.common.parameter import Parameter
  22. from mindspore.ops import operations as P
  23. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  24. class NetEqualBool(nn.Cell):
  25. def __init__(self):
  26. super(NetEqualBool, self).__init__()
  27. self.equal = P.Equal()
  28. x = Tensor(np.array([True, True, False]).astype(np.bool))
  29. y = Tensor(np.array([True, False, True]).astype(np.bool))
  30. self.x = Parameter(initializer(x, x.shape), name="x")
  31. self.y = Parameter(initializer(y, y.shape), name="y")
  32. def construct(self):
  33. return self.equal(self.x, self.y)
  34. @pytest.mark.level0
  35. @pytest.mark.platform_x86_cpu
  36. @pytest.mark.env_onecard
  37. def test_equal_bool():
  38. Equal = NetEqualBool()
  39. output = Equal()
  40. print("================================")
  41. expect = np.array([True, False, False]).astype(np.bool)
  42. print(output)
  43. assert (output.asnumpy() == expect).all()
  44. class NetEqualInt(nn.Cell):
  45. def __init__(self):
  46. super(NetEqualInt, self).__init__()
  47. self.equal = P.Equal()
  48. x = Tensor(np.array([1, 20, 5]).astype(np.int32))
  49. y = Tensor(np.array([2, 20, 5]).astype(np.int32))
  50. self.x = Parameter(initializer(x, x.shape), name="x")
  51. self.y = Parameter(initializer(y, y.shape), name="y")
  52. def construct(self):
  53. return self.equal(self.x, self.y)
  54. @pytest.mark.level0
  55. @pytest.mark.platform_x86_cpu
  56. @pytest.mark.env_onecard
  57. def test_equal_int():
  58. Equal = NetEqualInt()
  59. output = Equal()
  60. print("================================")
  61. expect = np.array([False, True, True]).astype(np.bool)
  62. print(output)
  63. assert (output.asnumpy() == expect).all()
  64. class NetEqualFloat(nn.Cell):
  65. def __init__(self):
  66. super(NetEqualFloat, self).__init__()
  67. self.equal = P.Equal()
  68. x = Tensor(np.array([1.2, 10.4, 5.5]).astype(np.float32))
  69. y = Tensor(np.array([1.2, 10.3, 5.4]).astype(np.float32))
  70. self.x = Parameter(initializer(x, x.shape), name="x")
  71. self.y = Parameter(initializer(y, y.shape), name="y")
  72. def construct(self):
  73. return self.equal(self.x, self.y)
  74. @pytest.mark.level0
  75. @pytest.mark.platform_x86_cpu
  76. @pytest.mark.env_onecard
  77. def test_equal_float():
  78. Equal = NetEqualFloat()
  79. output = Equal()
  80. print("================================")
  81. expect = np.array([True, False, False]).astype(np.bool)
  82. print(output)
  83. assert (output.asnumpy() == expect).all()