Wraps another sampler to yield an infinite stream of indices.
| 190 | # ============================================================================ |
| 191 | |
| 192 | class InfiniteSampler(Sampler): |
| 193 | """Wraps another sampler to yield an infinite stream of indices.""" |
| 194 | |
| 195 | def __init__(self, sampler: Sampler, shuffle: bool = True, seed: int = 0): |
| 196 | self.sampler = sampler |
| 197 | self.shuffle = shuffle |
| 198 | self.seed = seed |
| 199 | self.epoch = 0 |
| 200 | |
| 201 | def __iter__(self): |
| 202 | while True: |
| 203 | if hasattr(self.sampler, 'set_epoch'): |
| 204 | self.sampler.set_epoch(self.epoch) |
| 205 | yield from iter(self.sampler) |
| 206 | self.epoch += 1 |
| 207 | |
| 208 | def __len__(self): |
| 209 | return int(1e18) # Effectively infinite |
| 210 | |
| 211 | |
| 212 | # ============================================================================ |
no outgoing calls