-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.py
355 lines (291 loc) · 10.7 KB
/
demo.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 4 14:16:52 2016
@author: hrs13
"""
import numpy as np
import pickle
import matplotlib.animation as manimation
from utils import generate_parameters, generate_samples_correlated, generate_samples_correlated_new
from plotting import double_panel_demo, single_panel_demo
from K_means import update_K_means_Z, K_means_objective, update_K_means_mus
from EM_GMM import loglikelihood, E_step, M_step
from variational_GMM import variational_E_step, variational_M_step, variational_LB
def K_means_demo(X, K, num_its):
plt.set_new_lims(X, num_its)
mus = generate_parameters(K)[1]
# these initial means are an independent draw from the prior
objective = []
plt.cla('ax1')
plt.cla('ax2')
plt.plot_points_black(X)
plt.draw()
writer.grab_frame()
plt.cla('ax1')
plt.plot_points_black(X)
plt.plot_means_as_crosses(mus)
plt.draw()
writer.grab_frame()
for i in range(num_its):
# update Z
Z = update_K_means_Z(X, mus)
objective.append(K_means_objective(X, Z, mus))
plt.cla('ax1')
plt.plot_means_as_crosses(mus)
plt.plot_points_black(X)
plt.plot_K_means_objective(objective)
plt.plot_regions(Z, mus)
plt.draw()
writer.grab_frame()
#show colours
plt.cla('ax1')
plt.plot_regions(Z, mus)
plt.plot_means_as_crosses(mus)
plt.plot_data_coloured(X, Z)
plt.draw()
writer.grab_frame()
# update means
new_mus = update_K_means_mus(X, Z)
objective.append(K_means_objective(X, Z, new_mus))
plt.cla('ax1')
plt.plot_means_as_crosses(new_mus)
plt.plot_data_coloured(X, Z)
plt.plot_K_means_objective(objective)
plt.draw()
writer.grab_frame()
mus = new_mus
#plot with black points
plt.cla('ax1')
plt.plot_points_black(X)
plt.plot_means_as_crosses(mus)
plt.draw()
writer.grab_frame()
#move regions
plt.cla('ax1')
plt.plot_regions(Z, mus)
plt.plot_means_as_crosses(mus)
plt.plot_points_black(X)
plt.draw()
writer.grab_frame()
def EM_make_demo(X, K, num_its, num_trails):
N = X.shape[0]
objective = []
Sigmas = np.empty((num_iterations*num_trails, K, 2, 2))
mus = np.empty((num_iterations*num_trails, K, 2))
pis = np.empty((num_iterations*num_trails, K))
rs = np.empty((num_iterations*num_trails, N, 3))
for j in range(num_trails):
params = generate_parameters(K)
for i in range(num_its):
n = j*num_its + i
print n
r = E_step(X, params)
rs[n, :, :] = r
pis[n, :] = params[0]
mus[n, :, :] = params[1]
Sigmas[n, :, :, :] = params[2]
params = M_step(X, r)
objective.append(loglikelihood(X, params))
# objectives.append(objective)
def EM_animate(i):
n = i
plt.cla('ax1')
plt.cla('ax2')
params = pis[n, :], mus[n, :, :], Sigmas[n, :, :, :]
plt.plot_parameters(params)
plt.plot_data_coloured(X, rs[n, :, :])
plt.plot_GMM_objective(objective[:n], num_iterations)
plt.draw()
return EM_animate
def variational_make_demo_new(X, K, num_iterations, num_samples):
N = X.shape[0]
objective = []
rand = np.reshape(np.random.rand(N*K), (N, K))
r = (rand.T/np.sum(rand, axis=1)).T
Sigmas = np.empty((num_iterations, num_samples, K, 2, 2))
mus = np.empty((num_iterations, num_samples, K, 2))
pis = np.empty((num_iterations, num_samples, K))
rs = np.empty((num_iterations, N, K))
out = None
for i in range(num_iterations):
a_k, b_k, m_k, W_k, v_k = variational_M_step(X, r)
r = variational_E_step(X, a_k, b_k, m_k, W_k, v_k)
rs[i, :, :] = r
objective.append(variational_LB(X, r, a_k, b_k, m_k, W_k, v_k))
pis_, mus_, Sigmas_, out = generate_samples_correlated_new(num_samples,
a_k, b_k, m_k, W_k, v_k, out)
pis[i, :, :] = pis_
mus[i, :, :, :] = mus_
Sigmas[i, :, :, :, :] = Sigmas_
def variational_animate(n):
i = n/num_samples
j = n%num_samples
print n, i, j
plt.cla('ax1')
params = pis[i, j, :], mus[i, j, :, :], Sigmas[i, j, :, :, :]
print pis[i, j, :]
plt.plot_parameters(params)
plt.plot_data_coloured(X, rs[i, :, :])
if j==0:
plt.cla('ax2')
plt.plot_GMM_objective(objective[:i])
plt.draw()
return variational_animate
#def variational_make_demo(X, K, num_iterations, num_samples, num_interpolation_steps):
# N = X.shape[0]
# objective = []
#
# rand = np.reshape(np.random.rand(N*K), (N, K))
# r = (rand.T/np.sum(rand, axis=1)).T
#
# Sigmas = np.empty((num_iterations, num_samples, num_interpolation_steps, K, 2, 2))
# mus = np.empty((num_iterations, num_samples, num_interpolation_steps, K, 2))
# pis = np.empty((num_iterations, num_samples, num_interpolation_steps, K))
# rs = np.empty((num_iterations, N, K))
#
# for i in range(num_iterations):
# a_k, b_k, m_k, W_k, v_k = variational_M_step(X, r)
# r = variational_E_step(X, a_k, b_k, m_k, W_k, v_k)
# rs[i, :, :] = r
# objective.append(variational_LB(X, r, a_k, b_k, m_k, W_k, v_k))
# pis_, mus_, Sigmas_ = generate_samples_correlated(num_samples,
# num_interpolation_steps,
# a_k, b_k, m_k, W_k, v_k)
# pis[i, :, :, :] = pis_
# mus[i, :, :, :, :] = mus_
# Sigmas[i, :, :, :, :] = Sigmas_
## return pis, mus, Sigmas, rs, objective
# def variational_animate(n):
# i = n/(num_samples*num_interpolation_steps)
# j = (n%(num_samples*num_interpolation_steps))/num_interpolation_steps
# k = (n%(num_samples*num_interpolation_steps))%num_interpolation_steps
# print n, i, j, k
# plt.cla('ax1')
# params = np.abs(pis[i, j, k, :]), mus[i, j, k, :, :], Sigmas[i, j, k, :, :, :]
# print pis[i, j , k :]
# plt.plot_parameters(params)
# plt.plot_data_coloured(X, rs[i])
# if k==0:
# plt.cla('ax2')
# plt.plot_GMM_objective(objective[:i])
# plt.draw()
# return variational_animate
#
FFMpegWriter = manimation.writers['ffmpeg']
#metadata = dict(title='K means 1', artist='hughsalimbeni',
# comment='Shows K means working well')
#writer = FFMpegWriter(fps=15, metadata=metadata)
#data_1, data_2 = pickle.load( open( "data.p", "rb" ))
#
#plt = double_panel_demo(3)
#
#num_iterations = 30
#num_trials = 8
#X = data_1
#
#fps = 5
#writer = FFMpegWriter(fps=fps, bitrate=fps*100)
#
#plt.set_new_lims(X, num_iterations)
#frames = num_iterations*num_trials
#EM_animate = EM_make_demo(X, 3, num_iterations, num_trials)
#anim = manimation.FuncAnimation(plt.fig, EM_animate, frames=frames)
#
#anim.save("EM_low.mp4", writer=writer)
#
#
#data_1, data_2 = pickle.load( open( "data.p", "rb" ))
#plt = double_panel_demo(3)
#X = data_1
#
#num_iterations = 30
#num_samples = 20
#var_animate = variational_make_demo_new(X, 3,
# num_iterations,
# num_samples)
#fps=30
#plt.set_new_lims(X, num_iterations)
#
#writer = FFMpegWriter(fps=fps, bitrate=100*fps)
#frames = num_iterations * num_samples
#anim = manimation.FuncAnimation(plt.fig, var_animate, frames=frames)
#anim.save("var_high.mp4", writer=writer)
#
#
def prior_make_demo(K, num_samples):
Sigmas = np.empty((num_samples, K, 2, 2))
mus = np.empty((num_samples, K, 2))
pis = np.empty((num_samples, K))
out = None
a_k = 1000.*np.ones(K)
b_k = 1.*np.ones(K)
v_k = 25*np.ones(K)
m_k = np.empty((K, 2))
W_k = np.empty((K, 2, 2))
for k in range(K):
m_k[k, :] = np.zeros(2)
W_k[k, :, :] = np.eye(2)/20
pis, mus, Sigmas, out = generate_samples_correlated_new(num_samples,
a_k, b_k, m_k, W_k, v_k, out)
def prior_animate(n):
print n
plt.cla()
params = pis[n, :], mus[n, :, :], Sigmas[n, :, :, :]
plt.plot_parameters(params)
plt.draw()
return prior_animate
plt = single_panel_demo(3)
num_samples = 200
fps = 15
writer = FFMpegWriter(fps=fps, bitrate=100*fps)
frames = num_samples
prior_animate = prior_make_demo(3, num_samples)
anim = manimation.FuncAnimation(plt.fig, prior_animate, frames=frames)
anim.save("prior2.mp4", writer=writer)
#writer = FFMpegWriter(fps=1)
#num_its = 10
#frames = (2 + num_its*5)
#with writer.saving(plt.fig, "K_means_low_1.mp4", frames):
# K_means_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "K_means_low_2.mp4", frames):
# K_means_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "K_means_low_3.mp4", frames):
# K_means_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "K_means_low_1.mp4", frames):
# K_means_demo(plt, data_2, 3, num_its)
#with writer.saving(plt.fig, "K_means_low_2.mp4", frames):
# K_means_demo(plt, data_2, 3, num_its)
#with writer.saving(plt.fig, "K_means_low_3.mp4", frames):
# K_means_demo(plt, data_2, 3, num_its)
#
#writer = FFMpegWriter(fps=5)
#num_its = 30
#frames = 2 + num_its*2
#with writer.saving(plt.fig, "EM_low_1.mp4", frames):
# EM_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "EM_low_2.mp4", frames):
# EM_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "EM_low_3.mp4", frames):
# EM_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "EM_high_1.mp4", frames):
# EM_demo(plt, data_2, 3, num_its)
#with writer.saving(plt.fig, "EM_high_2.mp4", frames):
# EM_demo(plt, data_2, 3, num_its)
#with writer.saving(plt.fig, "EM_high_3.mp4", frames):
# EM_demo(plt, data_2, 3, num_its)
#
#
#
#
#with writer.saving(plt.fig, "var_low_2.mp4", frames):
# variational_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "var_low_3.mp4", frames):
# variational_demo(plt, data_1, 3, num_its)
#with writer.saving(plt.fig, "var_high_1.mp4", frames):
# variational_demo(plt, data_2, 3, num_its)
#with writer.saving(plt.fig, "var_high_2.mp4", frames):
# variational_demo(plt, data_2, 3, num_its)
#with writer.saving(plt.fig, "var_high_3.mp4", frames):
# variational_demo(plt, data_2, 3, num_its)
#
#