Skip to content

Commit 5e7f5db

Browse files
felixgwusoumith
authored andcommitted
add subset samplers (#888)
1 parent b5f7592 commit 5e7f5db

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

torch/utils/data/sampler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,20 @@ def __iter__(self):
5151

5252
def __len__(self):
5353
return self.num_samples
54+
55+
56+
class SubsetRandomSampler(Sampler):
57+
"""Samples elements randomly from a given list of indices, without replacement.
58+
59+
Arguments:
60+
indices (list): a list of indices
61+
"""
62+
63+
def __init__(self, indices):
64+
self.indices = indices
65+
66+
def __iter__(self):
67+
return (self.indices[i] for i in torch.randperm(len(self.indices)))
68+
69+
def __len__(self):
70+
return len(self.indices)

0 commit comments

Comments
 (0)