r"""Samples elements randomly from a given list of indices, without replacement. Arguments: indices (sequence): a sequence of indices
| 11 | import torch |
| 12 | |
| 13 | class SubsetRandomSampler(torch.utils.data.Sampler): |
| 14 | r"""Samples elements randomly from a given list of indices, without replacement. |
| 15 | |
| 16 | Arguments: |
| 17 | indices (sequence): a sequence of indices |
| 18 | """ |
| 19 | |
| 20 | def __init__(self, indices): |
| 21 | self.epoch = 0 |
| 22 | self.indices = indices |
| 23 | |
| 24 | def __iter__(self): |
| 25 | return (self.indices[i] for i in torch.randperm(len(self.indices))) |
| 26 | |
| 27 | def __len__(self): |
| 28 | return len(self.indices) |
| 29 | |
| 30 | def set_epoch(self, epoch): |
| 31 | self.epoch = epoch |