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_empty.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 mindspore.dataset as ds
  17. def test_tensor_empty():
  18. def gen():
  19. for _ in range(4):
  20. (yield np.array([], dtype=np.int64), np.array([], dtype='S').reshape([0, 4]), np.array([1],
  21. dtype=np.float64))
  22. data = ds.GeneratorDataset(gen, column_names=["col1", "col2", "col3"])
  23. for d in data.create_tuple_iterator(num_epochs=1, output_numpy=True):
  24. np.testing.assert_array_equal(np.array([], dtype=np.int64), d[0])
  25. np.testing.assert_array_equal(np.array([], dtype='S').reshape([0, 4]), d[1])
  26. np.testing.assert_array_equal(np.array([1], dtype=np.float64), d[2])
  27. def test_tensor_empty_map():
  28. def gen():
  29. for _ in range(4):
  30. (yield np.array([], dtype=np.int64), np.array([], dtype='S'), np.array([1], dtype=np.float64))
  31. data = ds.GeneratorDataset(gen, column_names=["col1", "col2", "col3"])
  32. def func(x, y, z):
  33. x = np.array([1], dtype=np.int64)
  34. y = np.array(["Hi"], dtype='S')
  35. z = np.array([], dtype=np.float64)
  36. return x, y, z
  37. data = data.map(operations=func, input_columns=["col1", "col2", "col3"])
  38. for d in data.create_tuple_iterator(num_epochs=1, output_numpy=True):
  39. np.testing.assert_array_equal(np.array([1], dtype=np.int64), d[0])
  40. np.testing.assert_array_equal(np.array(["Hi"], dtype='S'), d[1])
  41. np.testing.assert_array_equal(np.array([], dtype=np.float64), d[2])
  42. def test_tensor_empty_batch():
  43. def gen():
  44. for _ in range(4):
  45. (yield np.array([], dtype=np.int64), np.array([], dtype='S').reshape([0, 4]), np.array([1],
  46. dtype=np.float64))
  47. data = ds.GeneratorDataset(gen, column_names=["col1", "col2", "col3"]).batch(2)
  48. for d in data.create_tuple_iterator(num_epochs=1, output_numpy=True):
  49. np.testing.assert_array_equal(np.array([], dtype=np.int64).reshape([2, 0]), d[0])
  50. np.testing.assert_array_equal(np.array([], dtype='S').reshape([2, 0, 4]), d[1])
  51. np.testing.assert_array_equal(np.array([[1], [1]], dtype=np.float64), d[2])
  52. if __name__ == '__main__':
  53. test_tensor_empty()
  54. test_tensor_empty_map()
  55. test_tensor_empty_batch()