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
| 71 | |
| 72 | |
| 73 | class 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 | |
| 96 | class BatchSampler(object): |
nothing calls this directly
no outgoing calls
no test coverage detected