MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / RandomSampler

Class RandomSampler

imperative/python/megengine/data/sampler.py:218–256  ·  view source on GitHub ↗

r"""Sample elements randomly without replacement. Args: dataset: dataset to sample from. batch_size: batch size for batch method. drop_last: set ``True`` to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If ``False`` a

Source from the content-addressed store, hash-verified

216
217
218class RandomSampler(MapSampler):
219 r"""Sample elements randomly without replacement.
220
221 Args:
222 dataset: dataset to sample from.
223 batch_size: batch size for batch method.
224 drop_last: set ``True`` to drop the last incomplete batch,
225 if the dataset size is not divisible by the batch size. If ``False`` and
226 the size of dataset is not divisible by the batch_size, then the last batch will
227 be smaller. Default: False
228 indices: indice of samples.
229 world_size: number of ranks.
230 rank: rank id, non-negative interger within 0 and ``world_size``.
231 seed: seed for random operators.
232 """
233
234 def __init__(
235 self,
236 dataset,
237 batch_size=1,
238 drop_last=False,
239 indices=None,
240 world_size=None,
241 rank=None,
242 seed=None,
243 ):
244 super().__init__(dataset, batch_size, drop_last, None, world_size, rank, seed)
245 if indices is not None and not isinstance(indices, collections.abc.Sequence):
246 raise ValueError(
247 "indices should be None or a sequence, "
248 "but got indices={}".format(indices)
249 )
250 self.indices = indices
251
252 def sample(self) -> List:
253 if self.indices is None:
254 return self.rng.permutation(len(self.dataset)).tolist()
255 else:
256 return self.rng.permutation(self.indices).tolist()
257
258
259class ReplacementSampler(MapSampler):

Calls

no outgoing calls