-
Notifications
You must be signed in to change notification settings - Fork 15
/
gtg.py
72 lines (60 loc) · 2.54 KB
/
gtg.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import torch.nn as nn
import torch
import dynamics
import torch.nn.functional as F
class GTG(nn.Module):
def __init__(self, total_classes, tol=-1., max_iter=5, mode='replicator', device='cuda:0'):
super(GTG, self).__init__()
self.m = total_classes
self.tol = tol
self.max_iter = max_iter
self.mode = mode
self.device = device
def _init_probs_uniform(self, labs, L, U):
""" Initialized the probabilities of GTG from uniform distribution """
n = len(L) + len(U)
ps = torch.zeros(n, self.m).to(self.device)
ps[U, :] = 1. / self.m
ps[L, labs] = 1.
# check if probs sum up to 1.
assert torch.allclose(ps.sum(dim=1), torch.ones(n).cuda())
return ps
def _init_probs_prior(self, probs, labs, L, U):
""" Initiallized probabilities from the softmax layer of the CNN """
n = len(L) + len(U)
ps = torch.zeros(n, self.m).to(self.device)
ps[U, :] = probs[U, :]
ps[L, labs] = 1.
# check if probs sum up to 1.
assert torch.allclose(ps.sum(dim=1), torch.ones(n).cuda())
return ps
def _init_probs_prior_only_classes(self, probs, labs, L, U, classes_to_use):
""" Different version of the previous version when it considers only classes in the minibatch,
might need tuning in order to reach the same performance as _init_probs_prior """
n = len(L) + len(U)
ps = torch.zeros(n, self.m).to(self.device)
ps[U, :] = probs[torch.meshgrid(torch.tensor(U), torch.from_numpy(classes_to_use))]
ps[L, labs] = 1.
ps /= ps.sum(dim=ps.dim() - 1).unsqueeze(ps.dim() - 1)
return ps
def set_negative_to_zero(self, W):
return F.relu(W)
def _get_W(self, x):
x = (x - x.mean(dim=1).unsqueeze(1))
norms = x.norm(dim=1)
W = torch.mm(x, x.t()) / torch.ger(norms, norms)
W = self.set_negative_to_zero(W.cuda())
return W
def forward(self, fc7, num_points, labs, L, U, probs=None, classes_to_use=None):
W = self._get_W(fc7)
if type(probs) is type(None):
ps = self._init_probs_uniform(labs, L, U)
else:
if type(classes_to_use) is type(None):
ps = probs
ps = self._init_probs_prior(ps, labs, L, U)
else:
ps = probs
ps = self._init_probs_prior_only_classes(ps, labs, L, U, classes_to_use)
ps = dynamics.dynamics(W, ps, self.tol, self.max_iter, self.mode)
return ps, W