MCPcopy Create free account
hub / github.com/FreeformRobotics/OTS / WeightedRandomSampler

Class WeightedRandomSampler

lib/utils/data/sampler.py:73–93  ·  view source on GitHub ↗

Samples elements from [0,..,len(weights)-1] with given probabilities (weights). Arguments: weights (list) : a list of weights, not necessary summing up to one num_samples (int): number of samples to draw replacement (bool): if ``True``, samples are drawn with replaceme

Source from the content-addressed store, hash-verified

71
72
73class WeightedRandomSampler(Sampler):
74 """Samples elements from [0,..,len(weights)-1] with given probabilities (weights).
75
76 Arguments:
77 weights (list) : a list of weights, not necessary summing up to one
78 num_samples (int): number of samples to draw
79 replacement (bool): if ``True``, samples are drawn with replacement.
80 If not, they are drawn without replacement, which means that when a
81 sample index is drawn for a row, it cannot be drawn again for that row.
82 """
83
84 def __init__(self, weights, num_samples, replacement=True):
85 self.weights = torch.DoubleTensor(weights)
86 self.num_samples = num_samples
87 self.replacement = replacement
88
89 def __iter__(self):
90 return iter(torch.multinomial(self.weights, self.num_samples, self.replacement))
91
92 def __len__(self):
93 return self.num_samples
94
95
96class BatchSampler(object):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected