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_properties.py 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 tensor properties in graph mode"""
  16. import numpy as np
  17. import mindspore.nn as nn
  18. import mindspore.common.dtype as mstype
  19. from mindspore import Tensor
  20. from mindspore import context
  21. context.set_context(mode=context.GRAPH_MODE)
  22. def test_ndim():
  23. class Net(nn.Cell):
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.value = Tensor(np.random.random(
  27. (2, 3, 4, 5)), dtype=mstype.float32)
  28. def construct(self):
  29. return self.value.ndim
  30. net = Net()
  31. res = net()
  32. assert res == 4
  33. def test_nbytes():
  34. class Net(nn.Cell):
  35. def __init__(self):
  36. super(Net, self).__init__()
  37. self.value = Tensor(np.random.random(
  38. (2, 3, 4, 5)), dtype=mstype.float32)
  39. def construct(self):
  40. return self.value.nbytes
  41. net = Net()
  42. res = net()
  43. assert res == 480
  44. def test_size():
  45. class Net(nn.Cell):
  46. def __init__(self):
  47. super(Net, self).__init__()
  48. self.value = Tensor(np.random.random(
  49. (2, 3, 4, 5)), dtype=mstype.float32)
  50. def construct(self):
  51. return self.value.size
  52. net = Net()
  53. res = net()
  54. assert res == 120
  55. def test_strides():
  56. class Net(nn.Cell):
  57. def __init__(self):
  58. super(Net, self).__init__()
  59. self.value = Tensor(np.random.random(
  60. (2, 3, 4, 5)), dtype=mstype.float32)
  61. def construct(self):
  62. return self.value.strides
  63. net = Net()
  64. res = net()
  65. assert res == (240, 80, 20, 4)
  66. def test_itemsize():
  67. class Net(nn.Cell):
  68. def __init__(self):
  69. super(Net, self).__init__()
  70. self.value1 = Tensor(np.random.random(
  71. (2, 3, 4, 5)), dtype=mstype.float64)
  72. self.value2 = Tensor(np.random.random(
  73. (2, 3, 4, 5)), dtype=mstype.int32)
  74. self.value3 = Tensor(np.random.random(
  75. (2, 3, 4, 5)), dtype=mstype.bool_)
  76. def construct(self):
  77. return (self.value1.itemsize, self.value2.itemsize, self.value3.itemsize)
  78. net = Net()
  79. res = net()
  80. assert res == (8, 4, 1)