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 4.1 kB

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