-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVI.py
More file actions
168 lines (98 loc) · 3.93 KB
/
VI.py
File metadata and controls
168 lines (98 loc) · 3.93 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import numpy as np
import theano
import theano.tensor as T
import math
from MALA import RWM
# This only works with 1D data
class Variational_inference(object):
def __init__(self,fn, mu_init, var_init, learning_rate, no_of_sample,no_of_epochs):
self.f = fn
self.mu_init = mu_init
self.var_init = var_init
self.learning_rate = learning_rate
self.n = no_of_sample
self.epoch = no_of_epochs
def grad(self, mu, var):
w = T.vector('w')
x = T.scalar('x')
log_density = -T.log(T.sqrt(2*math.pi*w[1])) - ((x-w[0])**2)/(2*w[1])
log_density_grad = T.grad(log_density, w)
sum =0
for i in range(self.n):
sample = mu+ var**.5* np.random.randn()
sum =sum+np.log(self.f(sample))*log_density_grad.eval({w:[mu, var],x:sample})
mc_avg = sum/self.n
mu_grad = -mc_avg[0]
var_grad = -(1/(2*var)) - mc_avg[1]
return mu_grad,var_grad
def get_param(self):
mu = self.mu_init
var = self.var_init
mu_arr = []
var_arr = []
for i in range(self.epoch):
mu -=self.learning_rate*self.grad(mu, var)[0]
var -=self.learning_rate*self.grad(mu, var)[1]
mu_arr.append(mu)
var_arr.append(var)
return mu_arr, var_arr
#This is after reparametrization of variance i.e. taking \sigma^2 = log(1+e^s)
#Also added the momentum support
class Variational_inference_new(object):
"""
This estimates a density with N(mu, sigma^2) using variational inference
"""
def __init__(self,fn, mu_init, var_init, learning_rate, no_of_sample,no_of_epochs, momentum=.9):
self.f = fn
self.mu_init = mu_init
self.var_init = var_init
self.learning_rate = learning_rate
self.n = no_of_sample
self.epoch = no_of_epochs
self.momentum = momentum
def grad(self, mu, s):
w = T.vector('w')
var = T.log(1 + T.exp(w[1]))
x = T.scalar('x')
log_density = -T.log(T.sqrt(2*math.pi*var)) - ((x-w[0])**2)/(2*var)
log_density_grad = T.grad(log_density, w)
sum =0
for i in range(self.n):
sample = mu+ (math.log(1+math.exp(s))**.5)* np.random.randn()
sum =sum+np.log(self.f(sample))*log_density_grad.eval({w:[mu,s], x:sample})
mc_avg = sum/self.n
mu_grad = -mc_avg[0]
s_grad = (-.5* math.exp(s)) /(math.log(1+math.exp(s))*(1+math.exp(s))) - mc_avg[1]
return mu_grad,s_grad
def get_param(self):
mu = self.mu_init
s = math.log(math.exp(self.var_init)-1)
mu_arr = []
var_arr = []
mu_update = [0]
s_update = [0]
for i in range(self.epoch):
update1 = self.momentum*mu_update[i] + (1-self.momentum)*self.grad(mu, s)[0]
update2 = self.momentum * mu_update[i] + (1 - self.momentum) * self.grad(mu, s)[1]
mu = mu - self.learning_rate * update1
s = s - self.learning_rate * update2
mu_update.append(update1)
s_update.append(update2)
# mu = mu - self.learning_rate*self.grad(mu, s)[0]
# s = s - self.learning_rate*self.grad(mu, s)[1]
mu_arr.append(mu)
var_arr.append(math.log(1+math.exp(s)))
#We want to print the loss after every 5 epochs
if i%5 ==0:
sum = 0
for k in range(self.n):
sample = mu + (math.log(1 + math.exp(s)) ** .5) * np.random.randn()
sum = sum + np.log(self.f(sample))
loss = sum/self.n
print("Loss for epoch:{} is \t {:.4f}".format(i, loss))
return mu_arr, var_arr
if __name__ == '__main__':
fn1 = lambda x: np.exp(-x ** 2 / (1.1 + np.sin(x)))
vi1 = Variational_inference_new(fn1, 1, .1, .01, 1000, 200)
rwm = RWM(fn1,10000,0,.1)
mu, var = vi1.get_param()