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.

mindspore.dataset.GeneratorDataset.rst 8.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. mindspore.dataset.GeneratorDataset
  2. ===================================
  3. .. py:class:: mindspore.dataset.GeneratorDataset(source, column_names=None, column_types=None, schema=None, num_samples=None, num_parallel_workers=1, shuffle=None, sampler=None, num_shards=None, shard_id=None, python_multiprocessing=True, max_rowsize=6)
  4. 通过调用Python数据源从Python中生成数据作为源数据集。生成的数据集的列名和列类型取决于用户定义的Python数据源。
  5. **参数:**
  6. - **source** (Union[Callable, Iterable, Random Accessible]) -
  7. 一个Python的可调用对象,可以是一个可迭代的Python对象,或支持随机访问的Python对象。
  8. 要求传入的可调用对象,可以通过 `source().next()` 的方式返回一个由NumPy数组构成的元组。
  9. 要求传入的可迭代对象,可以通过 `iter(source).next()` 的方式返回一个由NumPy数组构成的元组。
  10. 要求传入的支持随机访问对象,可以通过 `source[idx]` 的方式返回一个由NumPy数组构成的元组。
  11. - **column_names** (Union[str, list[str]],可选) - 指定数据集生成的列名(默认值为None),用户必须提供此参数或通过参数 `schema` 指定列名。
  12. - **column_types** ((list[mindspore.dtype],可选) - 指定生成数据集各个数据列的数据类型(默认为None)。
  13. 如果未指定该参数,则自动推断类型;如果指定了该参数,将在数据输出时做类型匹配检查。
  14. - **schema** (Union[Schema, str],可选) - 读取模式策略,用于指定读取数据列的数据类型、数据维度等信息,支持传入JSON文件或 `schema` 对象的路径。
  15. 对于数据集生成的列名,用户需要提供 `column_names` 或 `schema` 进行指定,如果同时指定两者,则将优先从 `schema` 获取列名信息。
  16. - **num_samples** (int,可选) - 指定从数据集中读取的样本数(默认为None)。
  17. - **num_parallel_workers** (int,可选) - 指定读取数据的工作线程数(默认值为1)。
  18. - **shuffle** (bool,可选) - 是否混洗数据集。只有输入的 `source` 参数带有可随机访问属性(__getitem__)时,才可以指定该参数。(默认值为None,下表中会展示不同配置的预期行为)。
  19. - **sampler** (Union[Sampler, Iterable],可选) - 指定从数据集中选取样本的采样器。只有输入的 `source` 参数带有可随机访问属性(__getitem__)时,才可以指定该参数(默认值为None,下表中会展示不同配置的预期行为)。
  20. - **num_shards** (int, 可选): 分布式训练时,将数据集划分成指定的分片数(默认值None)。指定此参数后,`num_samples` 表示每个分片的最大样本数。需要输入 `data` 支持可随机访问才能指定该参数。
  21. - **shard_id** (int, 可选): 分布式训练时,指定使用的分片ID号(默认值None)。只有当指定了 `num_shards` 时才能指定此参数。
  22. - **python_multiprocessing** (bool,可选) - 启用Python多进程模式加速运算(默认为True)。当传入Python对象的计算量很大时,开启此选项可能会有较好效果。
  23. - **max_rowsize** (int,可选) - 指定在多进程之间复制数据时,共享内存分配的最大空间(数量级为MB,默认为6MB),仅当参数 `python_multiprocessing` 设为True时,此参数才会生效。
  24. **异常:**
  25. - **RuntimeError** - Python对象 `source` 在执行期间引发异常。
  26. - **RuntimeError** - 参数 `column_names` 指定的列名数量与 `source` 的输出数据数量不匹配。
  27. - **RuntimeError** - 参数 `num_parallel_workers` 超过最大线程数。
  28. - **RuntimeError** - 同时指定了 `sampler` 和 `shuffle` 。
  29. - **RuntimeError** - 同时指定了 `sampler` 和 `num_shards` 。
  30. - **RuntimeError** - 指定了 `num_shards` 参数,但是未指定 `shard_id` 参数。
  31. - **RuntimeError** - 指定了 `shard_id` 参数,但是未指定 `num_shards` 参数。
  32. - **ValueError** - `shard_id` 参数错误(小于0或者大于等于 `num_shards` )。
  33. .. note::
  34. - `source` 参数接收用户自定义的Python函数(PyFuncs),不要将 `mindspore.nn` 和 `mindspore.ops` 目录下或其他的网络计算算子添加
  35. 到 `source` 中。
  36. - 此数据集可以指定 `sampler` 参数,但 `sampler` 和 `shuffle` 是互斥的。下表展示了几种合法的输入参数及预期的行为。
  37. .. list-table:: 配置 `sampler` 和 `shuffle` 的不同组合得到的预期排序结果
  38. :widths: 25 25 50
  39. :header-rows: 1
  40. * - 参数 `sampler`
  41. - 参数 `shuffle`
  42. - 预期数据顺序
  43. * - None
  44. - None
  45. - 随机排列
  46. * - None
  47. - True
  48. - 随机排列
  49. * - None
  50. - False
  51. - 顺序排列
  52. * - 参数 `sampler`
  53. - None
  54. - 由 `sampler` 行为定义的顺序
  55. * - 参数 `sampler`
  56. - True
  57. - 不允许
  58. * - 参数 `sampler`
  59. - False
  60. - 不允许
  61. **样例:**
  62. >>> import numpy as np
  63. >>>
  64. >>> # 1)定义一个Python生成器作为GeneratorDataset的可调用对象。
  65. >>> def generator_multidimensional():
  66. ... for i in range(64):
  67. ... yield (np.array([[i, i + 1], [i + 2, i + 3]]),)
  68. >>>
  69. >>> dataset = ds.GeneratorDataset(source=generator_multidimensional, column_names=["multi_dimensional_data"])
  70. >>>
  71. >>> # 2)定义一个Python生成器返回多列数据,作为GeneratorDataset的可调用对象。
  72. >>> def generator_multi_column():
  73. ... for i in range(64):
  74. ... yield np.array([i]), np.array([[i, i + 1], [i + 2, i + 3]])
  75. >>>
  76. >>> dataset = ds.GeneratorDataset(source=generator_multi_column, column_names=["col1", "col2"])
  77. >>>
  78. >>> # 3)定义一个可迭代数据集对象,作为GeneratorDataset的可调用对象。
  79. >>> class MyIterable:
  80. ... def __init__(self):
  81. ... self._index = 0
  82. ... self._data = np.random.sample((5, 2))
  83. ... self._label = np.random.sample((5, 1))
  84. ...
  85. ... def __next__(self):
  86. ... if self._index >= len(self._data):
  87. ... raise StopIteration
  88. ... else:
  89. ... item = (self._data[self._index], self._label[self._index])
  90. ... self._index += 1
  91. ... return item
  92. ...
  93. ... def __iter__(self):
  94. ... self._index = 0
  95. ... return self
  96. ...
  97. ... def __len__(self):
  98. ... return len(self._data)
  99. >>>
  100. >>> dataset = ds.GeneratorDataset(source=MyIterable(), column_names=["data", "label"])
  101. >>>
  102. >>> # 4)定义一个支持随机访问数据集对象,作为GeneratorDataset的可调用对象。
  103. >>> class MyAccessible:
  104. ... def __init__(self):
  105. ... self._data = np.random.sample((5, 2))
  106. ... self._label = np.random.sample((5, 1))
  107. ...
  108. ... def __getitem__(self, index):
  109. ... return self._data[index], self._label[index]
  110. ...
  111. ... def __len__(self):
  112. ... return len(self._data)
  113. >>>
  114. >>> dataset = ds.GeneratorDataset(source=MyAccessible(), column_names=["data", "label"])
  115. >>>
  116. >>> # 注意,Python的list、dict、tuple也是支持随机可访问的,同样可以作为GeneratorDataset的输入
  117. >>> dataset = ds.GeneratorDataset(source=[(np.array(0),), (np.array(1),), (np.array(2),)], column_names=["col"])
  118. .. include:: mindspore.dataset.Dataset.add_sampler.rst
  119. .. include:: mindspore.dataset.Dataset.rst
  120. .. include:: mindspore.dataset.Dataset.use_sampler.rst
  121. .. include:: mindspore.dataset.Dataset.zip.rst