-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayesian_nn.py
70 lines (44 loc) · 2.2 KB
/
bayesian_nn.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class BayesianLayer(nn.Module):
def __init__(self,in_features,out_features):
super(BayesianLayer, self).__init__()
self.mu = nn.Parameter(torch.zeros(in_features, out_features).uniform_(-0.6, 0.6))
self.rho = nn.Parameter(torch.zeros(in_features,out_features)-3)
self.bias_mu = nn.Parameter(torch.zeros(out_features).uniform_(-0.6, 0.6))
self.bias_rho = nn.Parameter(torch.zeros(out_features)-3)
def forward(self,x):
epsilon = torch.normal(mean = 0.0, std = 1.0, size = self.mu.shape)
w = self.mu + F.softplus(self.rho) * epsilon
bias_epsilon = torch.normal(mean = 0.0, std = 1.0, size = self.bias_mu.shape)
b = self.bias_mu + F.softplus(self.bias_rho) * bias_epsilon
return torch.matmul(x,w) + b
# total kl loss for the weights in this layer
def compute_layer_kl_loss(self):
layer_kl_loss = torch.sum(self._kl_loss(self.mu,self.rho)) + torch.sum(self._kl_loss(self.bias_mu,self.bias_rho))
return layer_kl_loss
# kl loss between one weight's posterior and unit Gaussian prior (closed form complexity cost)
def _kl_loss(self,temp_mu,temp_rho):
sigma_squared = F.softplus(temp_rho) ** 2
return -0.5 * (1 + torch.log(sigma_squared) - temp_mu ** 2 - sigma_squared)
class BayesianNet(nn.Module):
def __init__(self, input_size, num_hidden_layers, layer_width):
super(BayesianNet, self).__init__()
layers = []
layers.append(BayesianLayer(input_size,layer_width))
layers.append(nn.ReLU())
for _ in range(num_hidden_layers-1):
layers.append(BayesianLayer(layer_width,layer_width))
layers.append(nn.ReLU())
layers.append(BayesianLayer(layer_width,1))
self.net = nn.Sequential(*layers)
def forward(self,x):
return self.net(x)
def compute_total_kl_loss(self):
total_kl_loss = 0
for i in self.children():
for j in i.children():
if isinstance(j,BayesianLayer):
total_kl_loss += j.compute_layer_kl_loss()
return total_kl_loss