-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNGVI_MEF.py
137 lines (106 loc) · 5.42 KB
/
NGVI_MEF.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
import torch
import torch.nn as nn
from distributions import Gaussian, GMM, NegLogGMM, gmm, gaussian
from utils import nth_derivative
from plotters import plot_posteriors, generate_animation
from torch.distributions import Categorical
from distributions import log_gaussian, log_gmm
from sampling import gmm_sample
class H(nn.Module):
def __init__(self):
super().__init__()
# self.p = Gaussian(mean=0, sigma= 10) # prior
# self.q = GMM(num_components=2, means=[-2., 2.], variances=[.5, .5], pais=[.3, .7])
# self.neg_log_ll = NegLogGMM(num_components=2, means=[-4., 4.], variances=[.2, .2], pais=[.7, .3])
self.softplus = torch.nn.Softplus()
self.softmax = torch.nn.Softmax(dim=0)
self.prior_m = torch.tensor(0.)
self.prior_s = torch.tensor(10.)
self.prior_fn = lambda t: log_gaussian(t, self.prior_m, self.prior_s)
self.likelihood_s = torch.tensor([.2, .2])
self.likelihood_log_pais = torch.log(torch.tensor([.3, .7]))
self.likelihood_mean = torch.tensor([-4., 4.])
self.likelihood_fn = lambda t: log_gmm(t, self.likelihood_mean,
self.likelihood_s,
self.likelihood_log_pais)
self.q_means = torch.tensor([-2., 2.])
self.q_stds = torch.tensor([.4, .4])
self.q_log_pais = torch.log(torch.tensor([.5, .5]))
self.q_fn = lambda t: log_gmm(t, self.q_means,
self.q_stds,
self.q_log_pais)
def forward(self, x):
h = self.q_fn(x) - self.prior_fn(x) - self.likelihood_fn(x)
return h
def fit_gmm(num_components, beta, num_step=20, num_samples=1):
objective = H()
all_deltas = torch.zeros(num_components)
grads = torch.zeros(num_components)
hess = torch.zeros(num_components)
all_sampled_z = torch.zeros(num_components)
rho = torch.zeros(num_components)
all_posteriors = []
all_normalized_posteriors = []
for i in range(num_step):
# mixture_comps = Categorical(torch.exp(objective.q_log_pais))
# component = mixture_comps.sample()
# sampled_z = objective.q_means[component] + torch.randn(1) * objective.q_stds[component]
sampled_z = gmm_sample(objective.q_means, objective.q_stds,
objective.q_log_pais, num_samples)
sampled_z = torch.nn.Parameter(sampled_z)
dx = nth_derivative(objective(sampled_z), sampled_z, 1)
d2xx = nth_derivative(objective(sampled_z), sampled_z, 2)
for c in range(num_components):
delta = gaussian(sampled_z, objective.q_means[c],
objective.q_stds[c]) / torch.exp(
objective.q_fn(sampled_z))
all_deltas[c] = delta
# update mean and variances and ratios
q_var = objective.q_stds[c] ** 2
q_var = 1 / (1 / q_var + beta * delta * d2xx)
objective.q_stds[c] = torch.sqrt(q_var)
objective.q_means[c] = objective.q_means[
c] - beta * delta * q_var * dx
# objective.q_stds[c] = torch.sqrt(torch.max(q_var, torch.FloatTensor([1e-6])))
if i % 10 == 0:
print("STEP {} ".format(i))
print("rhos: {}".format(rho.data.numpy()))
print("q_pais: {}".format(
torch.exp(objective.q_log_pais).data.numpy()))
print("q.means: {}".format(objective.q_means))
print("q.stds: {}".format(objective.q_stds))
print("*" * 30)
beta = beta * .95
for c in range(num_components):
# Update ratios
rho[c] = objective.q_log_pais[c] - objective.q_log_pais[-1]
rho[c] = rho[c] - beta * 0.1 * (
all_deltas[c] - all_deltas[-1]) * objective(sampled_z)
# rho = rho - torch.max(rho)
objective.q_log_pais = rho - torch.logsumexp(rho, axis=0,
keepdims=False)
# torch.log(torch.softmax(rho, dim=0))
# grads[c] = dx
# hess[c] = d2xx
x_axis = torch.arange(-5, 5, 0.0001)
posterior = gmm(x_axis, objective.q_means, objective.q_stds,
torch.exp(objective.q_log_pais))
normalized_posterior = posterior / torch.sum(posterior)
log_true_posterior = objective.likelihood_fn(
x_axis) + objective.prior_fn(x_axis)
true_posterior = torch.exp(log_true_posterior)
normalized_true_posterior = true_posterior / torch.sum(true_posterior)
# ax1 = fig.add_subplot(121)
# plt.plot(x_axis.detach(), normalized_posterior.detach(), '-b')
# plt.plot(x_axis.detach(), true_posterior, '-r')
# plt.show()
all_normalized_posteriors.append(normalized_posterior.detach())
all_posteriors.append(posterior.detach())
return all_normalized_posteriors, normalized_true_posterior, x_axis
if __name__ == '__main__':
all_normalized_posteriors, normalized_true_posterior, x_axis = fit_gmm(
num_step=501, num_components=2, beta=0.1, num_samples=1)
plot_posteriors(all_normalized_posteriors, normalized_true_posterior,
x_axis)
generate_animation(all_normalized_posteriors, normalized_true_posterior,
x_axis, path='./figs/gmm_3.mp4')