-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBBB.py
142 lines (103 loc) · 4.31 KB
/
BBB.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import numpy as np
import torch
import torch.nn as nn
from tqdm import tqdm
from base import Wrapper, get_bayesian_network, cross_entropy_loss, log_gaussian_loss, Network
from priors import Gaussian
from bayesian_layers import BayesianCNNLayer, BayesianLinearLayer
class BBB(Network):
def __init__(self, sample, classes, topology=None, prior=None, mu_init=None, rho_init=None,
local_trick=False, regression=False, posterior_type='weights', **kwargs):
super().__init__(classes=classes, regression=regression)
if topology is None:
topology = [400, 400]
if prior is None:
prior = Gaussian(0, 10)
self.calculate_kl = True
self._prior = prior
self.features = get_bayesian_network(topology, sample, classes,
mu_init, rho_init, prior, 'kl', local_trick, bias=True,
posterior_type=posterior_type)
def _forward(self, x):
tot_prior = 0
tot_post = 0
for j, i in enumerate(self.features):
if not isinstance(i, (BayesianLinearLayer, BayesianCNNLayer)):
x = i(x)
else:
x, prior, post = i(x, self.calculate_kl)
tot_post += post
tot_prior += prior
return x, tot_prior, tot_post
def forward(self, x, samples=1):
o = []
log_priors = []
log_posts = []
for i in range(samples):
op, prior, post = self._forward(x)
o.append(op)
log_priors.append(prior)
log_posts.append(post)
o = torch.stack(o)
log_priors = torch.stack(log_priors)
log_posts = torch.stack(log_posts)
log_prior = log_priors.mean()
log_post = log_posts.mean()
return o, log_prior, log_post
def eval_forward(self, x, samples=1):
o, _, _ = self(x, samples=samples)
return o
class Trainer(Wrapper):
def __init__(self, model: nn.Module, train_data, test_data, optimizer, **kwargs):
super().__init__(model, train_data, test_data, optimizer)
self.regression = model.regression
if model.regression:
self.loss = log_gaussian_loss(model.classes)
else:
self.loss = cross_entropy_loss('sum')
def train_epoch(self, samples=1, **kwargs):
losses = []
M = len(self.train_data)
a = np.asarray([2 ** (M - i - 1) for i in range(M + 1)])
b = 2 ** M - 1
pi = a / b
self.model.train()
progress_bar = tqdm(enumerate(self.train_data), total=len(self.train_data), disable=False, leave=False)
progress_bar.set_postfix(ce_loss='', kl_loss='')
train_true = []
train_pred = []
self.model.calculate_kl = True
for batch, (x, y) in progress_bar:
train_true.extend(y.tolist())
y = y.to(self.device)
x = x.to(self.device)
self.optimizer.zero_grad()
out, prior, post = self.model(x, samples=samples)
logloss = (post - prior) * pi[batch] #/ x.shape[0]
if pi[batch] == 0:
self.model.calculate_kl = False
if self.regression:
out = out.mean(0)
logloss = logloss/x.shape[0]
if self.model.classes == 1:
noise = self.model.noise.exp()
x = out
loss = self.loss_function(x, y, noise)
else:
loss = self.loss_function(out[:, :1], y, out[:, 1:].exp())
loss = loss/x.shape[0]
else:
loss = self.loss_function(out, y)
out = torch.softmax(out, -1).mean(0)
out = out.argmax(dim=-1)
progress_bar.set_postfix(ce_loss=loss.item(), kl_loss=logloss.item())
loss += logloss
losses.append(loss.item())
loss.backward()
self.optimizer.step()
train_pred.extend(out.tolist())
return losses, (train_true, train_pred)
def train_step(self, train_samples=1, test_samples=1, **kwargs):
losses, train_res = self.train_epoch(samples=train_samples)
test_res = self.test_evaluation(samples=test_samples)
return losses, train_res, test_res