mindspore/docs/api/api_python/dataset/mindspore.dataset.WeightedR...

32 lines
1.4 KiB
ReStructuredText
Raw Normal View History

2021-11-27 16:09:05 +08:00
mindspore.dataset.WeightedRandomSampler
=======================================
.. py:class:: mindspore.dataset.WeightedRandomSampler(weights, num_samples=None, replacement=True)
2021-11-23 15:00:48 +08:00
2022-01-05 16:22:27 +08:00
给定样本的权重列表,根据权重决定样本的采样概率,随机采样[0len(weights) - 1]中的样本。
2021-11-23 15:00:48 +08:00
2021-11-27 16:09:05 +08:00
**参数:**
2021-12-04 20:36:47 +08:00
- **weights** (list[float, int]) - 权重序列总和不一定为1。
2022-01-05 16:22:27 +08:00
- **num_samples** (int, 可选) - 获取的样本数可用于部分获取采样得到的样本。默认值None获取采样到的所有样本。
- **replacement** (bool) - 是否将样本ID放回下一次采样默认值True有放回采样。
**异常:**
- **TypeError** `weights` 元素的类型不是数字。
- **TypeError** `num_samples` 不是整数值。
- **TypeError** `replacement` 不是布尔值。
- **RuntimeError** `weights` 为空或全为零。
- **ValueError** `num_samples` 为负值。
2021-11-27 16:09:05 +08:00
**样例:**
>>> weights = [0.9, 0.01, 0.4, 0.8, 0.1, 0.1, 0.3]
>>>
2021-11-29 11:48:18 +08:00
>>> # 创建一个WeightedRandomSampler将对4个元素进行有放回采样
2021-11-27 16:09:05 +08:00
>>> sampler = ds.WeightedRandomSampler(weights, 4)
>>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir,
... num_parallel_workers=8,
... sampler=sampler)
2021-11-23 15:00:48 +08:00
.. include:: mindspore.dataset.BuiltinSampler.rst