-
Notifications
You must be signed in to change notification settings - Fork 0
/
regular_sampler.py
33 lines (30 loc) · 1.13 KB
/
regular_sampler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
import random
class RegularSampler(torch.utils.data.Sampler):
def __init__(self, data_source:torch.utils.data.Dataset,train_indexes,clustering_indexes,warmup_epochs = 1):
self.ds = data_source
self.train_indexes = train_indexes
self.clustering_indexes = clustering_indexes
self.warmup_epochs =warmup_epochs
self._clustering_flag = "training"
self.counter = 0
def get_clustering_flag(self):
return self._clustering_flag
def __iter__(self):
if self.counter < self.warmup_epochs+1:
self.counter+=1
indexes = self.train_indexes
random.shuffle(indexes)
for idx in indexes:
yield idx
if self.counter == self.warmup_epochs+1:
self._clustering_flag = "clustering"
indexes = self.clustering_indexes
random.shuffle(indexes)
for idx in indexes:
yield idx
self._clustering_flag = "done"
for i in range(8):
yield random.choice(self.clustering_indexes)
def __len__(self):
return len(self.ds)