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_tensor_py.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 tensor py"""
  16. import numpy as np
  17. import mindspore as ms
  18. from mindspore.common.api import _executor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. from ..ut_filter import non_graph_engine
  22. def _attribute(tensor, shape_, size_, dtype_):
  23. result = (tensor.shape == shape_) and \
  24. (tensor.size() == size_) and \
  25. (tensor.dtype == dtype_)
  26. return result
  27. def test_tensor_init():
  28. nparray = np.ones([2, 2], np.float32)
  29. ms.Tensor(nparray)
  30. ms.Tensor(nparray, dtype=ms.float32)
  31. @non_graph_engine
  32. def test_tensor_add():
  33. a = ms.Tensor(np.ones([3, 3], np.float32))
  34. b = ms.Tensor(np.ones([3, 3], np.float32))
  35. a += b
  36. @non_graph_engine
  37. def test_tensor_sub():
  38. a = ms.Tensor(np.ones([2, 3]))
  39. b = ms.Tensor(np.ones([2, 3]))
  40. b -= a
  41. @non_graph_engine
  42. def test_tensor_mul():
  43. a = ms.Tensor(np.ones([3, 3]))
  44. b = ms.Tensor(np.ones([3, 3]))
  45. a *= b
  46. def test_tensor_dim():
  47. arr = np.ones((1, 6))
  48. b = ms.Tensor(arr)
  49. assert b.dim() == 2
  50. def test_tensor_size():
  51. arr = np.ones((1, 6))
  52. b = ms.Tensor(arr)
  53. assert arr.size == b.size()
  54. def test_dtype():
  55. a = ms.Tensor(np.ones((2, 3), dtype=np.int32))
  56. assert a.dtype == ms.int32
  57. def test_asnumpy():
  58. npd = np.ones((2, 3))
  59. a = ms.Tensor(npd)
  60. a.set_dtype(ms.int32)
  61. assert a.asnumpy().all() == npd.all()
  62. def test_print():
  63. a = ms.Tensor(np.ones((2, 3)))
  64. a.set_dtype(ms.int32)
  65. print(a)
  66. def test_float():
  67. a = ms.Tensor(np.ones((2, 3)), ms.float16)
  68. assert a.dtype == ms.float16
  69. def test_tensor_method_sub():
  70. """test_tensor_method_sub"""
  71. class Net(Cell):
  72. def __init__(self):
  73. super(Net, self).__init__()
  74. self.sub = P.Sub()
  75. def construct(self, x, y):
  76. out = x - y
  77. return out.transpose()
  78. net = Net()
  79. x = ms.Tensor(np.ones([5, 3], np.float32))
  80. y = ms.Tensor(np.ones([8, 5, 3], np.float32))
  81. _executor.compile(net, x, y)
  82. def test_tensor_method_mul():
  83. """test_tensor_method_mul"""
  84. class Net(Cell):
  85. def __init__(self):
  86. super(Net, self).__init__()
  87. self.sub = P.Sub()
  88. def construct(self, x, y):
  89. out = x * (-y)
  90. return out.transpose()
  91. net = Net()
  92. x = ms.Tensor(np.ones([5, 3], np.float32))
  93. y = ms.Tensor(np.ones([8, 5, 3], np.float32))
  94. _executor.compile(net, x, y)
  95. def test_tensor_method_div():
  96. """test_tensor_method_div"""
  97. class Net(Cell):
  98. def __init__(self):
  99. super(Net, self).__init__()
  100. self.sub = P.Sub()
  101. def construct(self, x, y):
  102. out = x / y
  103. return out.transpose()
  104. net = Net()
  105. x = ms.Tensor(np.ones([5, 3], np.float32))
  106. y = ms.Tensor(np.ones([8, 5, 3], np.float32))
  107. _executor.compile(net, x, y)