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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 implicit conversion """
  16. import numpy as np
  17. from mindspore import Tensor
  18. def test_float_tensor_and_int_add():
  19. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  20. y = 2
  21. ret_actual = x + y
  22. ret_expect = Tensor(np.array([[2.1, 2.2, 2.3], [2.4, 2.5, 2.6]], dtype=np.float32))
  23. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  24. def test_bool_tensor_and_float_add():
  25. x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  26. y = 3.3
  27. ret_actual = x + y
  28. ret_expect = Tensor(np.array([[4.3, 3.3], [3.3, 4.3]], dtype=np.float32))
  29. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  30. def test_bool_tensor_and_int_add():
  31. x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  32. y = 3
  33. ret_actual = x + y
  34. ret_expect = Tensor(np.array([[4, 3], [3, 4]], dtype=np.int32))
  35. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  36. def test_bool_and_int_tensor_add():
  37. x = True
  38. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  39. ret_actual = x + y
  40. ret_expect = Tensor(np.array([[2, 3, 4], [5, 6, 7]], dtype=np.int32))
  41. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  42. def test_float_tensor_and_int_tensor_add():
  43. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  44. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  45. ret_actual = x + y
  46. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32))
  47. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  48. def test_float_tensor_and_float_tensor_add():
  49. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64))
  50. y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32))
  51. ret_actual = x + y
  52. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float64))
  53. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  54. def test_int_tensor_and_int_tensor_add():
  55. x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16))
  56. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  57. ret_actual = x + y
  58. ret_expect = Tensor(np.array([[2, 4, 6], [8, 10, 12]], dtype=np.int32))
  59. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  60. def test_float_tensor_and_bool_tensors_add():
  61. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  62. y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
  63. ret_actual = x + y
  64. ret_expect = Tensor(np.array([[1.1, 1.2, 1.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  65. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()