-
Notifications
You must be signed in to change notification settings - Fork 51
/
sampler.py
36 lines (25 loc) · 1006 Bytes
/
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
34
35
36
import torch
import numpy as np
class AdversarySampler:
def __init__(self, budget):
self.budget = budget
def sample(self, vae, discriminator, data, cuda):
all_preds = []
all_indices = []
for images, _, indices in data:
if cuda:
images = images.cuda()
with torch.no_grad():
_, _, mu, _ = vae(images)
preds = discriminator(mu)
preds = preds.cpu().data
all_preds.extend(preds)
all_indices.extend(indices)
all_preds = torch.stack(all_preds)
all_preds = all_preds.view(-1)
# need to multiply by -1 to be able to use torch.topk
all_preds *= -1
# select the points which the discriminator things are the most likely to be unlabeled
_, querry_indices = torch.topk(all_preds, int(self.budget))
querry_pool_indices = np.asarray(all_indices)[querry_indices]
return querry_pool_indices