-
Notifications
You must be signed in to change notification settings - Fork 0
/
grad_cbo_2d.py
398 lines (305 loc) · 13.4 KB
/
grad_cbo_2d.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# -*- coding: utf-8 -*-
"""
Created on Tue May 3 21:31:16 2022
@author: Philipp
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import logsumexp
from scipy.sparse.linalg import lsmr
from math import sqrt, pi, e, exp
import time
from grad_inference import *
from matplotlib.animation import FuncAnimation, PillowWriter
np.random.seed(1)
# Himmelblau
fnc = lambda x, y: (x**2+y-11)**2 + (x+y**2-7)**2
Dfnc = lambda x, y: np.array([2*(x**2+y-11)*2*x + 2*(x+y**2-7), 2*(x**2+y-11) + 2*(x+y**2-7)*2*y])
# Rastrigin
fnc = lambda x, y: 10*2 + x**2 - 10*np.cos(2*pi*x) + y**2 - 10*np.cos(2*pi*y)
Dfnc = lambda x, y: np.array([2*x + 2*pi*10*np.sin(2*pi*x), 2*y + 2*pi*10*np.sin(2*pi*y)])
D2fnc = lambda x,y:np.array([[2 + 4*pi**2*10*np.cos(2*pi*x), 0], [0, 2 + 4*pi**2*10*np.cos(2*pi*y)]])
# Ackley
#fnc = lambda x, y: -20*np.exp(-0.2*np.sqrt(0.5*(x**2+y**2)))-np.exp(0.5*np.cos(2*pi*x)+0.5*np.cos(2*pi*y)) + e + 20
Phi = lambda z: fnc(z[0], z[1])# just for safety
DPhi = lambda z: Dfnc(z[0], z[1])
# square_exact = lambda x: fnc(wmean[0],wmean[1]) + Dfnc(wmean[0],wmean[1]).flatten()@(x-wmean) + 0.5*(x-wmean)@(D2fnc(wmean[0][0],wmean[1][0])@(x-wmean))
# make full Monte Carlo simulation?
MC_test = False
N_MC = 20
# global switch: gradient-based yes or no?
use_grad = True
onlymean = True # compute gradient only in mean?
use_truegrad = False # use true gradient instead of inferred Bayesian gradient?
component_noise = True
orthogonal_noise = False
xmin = -4.5
xmax = 1.5
ymin = -4.5
ymax = 1.5
xs_plot = np.linspace(xmin,xmax,50)
ys_plot = np.linspace(ymin,ymax,50)
XX, YY = np.meshgrid(xs_plot, ys_plot)
ZZ = fnc(XX, YY)
d = 2
T = 20.0
tau = 0.01
N = int(T/tau)
deltas = [tau for k in range(1,N)]
ts = np.arange(0,T,tau)
# new timing scheme
# r = 0.25
# delta0 = 0.01
# N = 2000
# deltas = [delta0*k**(-r) for k in range(1,N)]
# ts = np.cumsum(deltas)
alpha = 100.0 # weight exponent of cost function exp(-alpha*Phi)
lam = 1.5 # coefficient of contraction towards weighted mean
sig = 0.7 # coefficient of noise
kappa = 0.5 # coefficient of gradient drift term
N_ens = 2 # size of ensemble
J = N_ens
avar = 0.1
# inferGradientAndHess(xs, vs, hessian = True, ind=0, retOnlyMatrix=False)
# initial ensemble
us0_global = np.random.uniform(-4,-1,(2,J)) # hard for functions with minimum at (0,0)
us0_global = np.array([[-4,-1.5],[-4,-2.5]])
#us0_global = np.random.uniform(-2,2,(2,J)) # hard for Himmelblau
# list of ensemble particles for each iteration
us_list = np.zeros(( N, d, J))
us_list[0, :, :] = us0_global
#%%
# plot initial config
plt.figure(figsize=(5,5))
plt.contourf(XX, YY, ZZ, 30)
plt.plot(us_list[0, 0, :], us_list[0, 1, :], '.k')
w_mean = np.zeros((N,len(us0_global)))
w_mean[0, :] = weighted_mean(us0_global.T, lambda u: -alpha*Phi(u))
plt.plot(w_mean[0, 0], w_mean[0,1], 'rx')
# method for computing all gradients at once
def compute_gradients(points):
vs = np.zeros_like(points)
H = np.zeros((points.shape[0],points.shape[1],points.shape[1]))
for i in range(points.shape[0]):
return_dict= inferGradientAndHess(points.T, Phi(points.T).flatten(), hessian = True, ind=i, additionalvariance=avar)
vs[i, :], H[i,:,:] = return_dict['grad'], return_dict['H']
return vs, H
# one step of gradient-augmented CBO
def step_gradHess(us, wmean, onlymean=True, tau=1.0):
#m = weighted_mean(us_list.reshape((-1,1)), lambda u: -alpha*Phi(u))
if onlymean:
ensembleplusmean = np.concatenate((wmean,us),axis=1)
values = np.concatenate((Phi(wmean),Phi(us)))
return_dict= inferGradientAndHess(ensembleplusmean, values, hessian=True, ind=0, additionalvariance=avar)
vs, H = return_dict['grad'], return_dict['H']
#vs = np.linalg.solve(H + np.eye(d), vs) # experimental to avoid oscillations
vs = vs[np.newaxis, :]
else:
vs, H = compute_gradients(us.T)
if component_noise:
diff = sig*(us-m)*np.random.normal(0,sqrt(tau),(d,J))
else:
if orthogonal_noise:
diff0 = sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
us_step = us + 0.5*diff0
m_step = weighted_mean(us_step.T, lambda u: -alpha*Phi(u)).reshape((-1,1))
term1 = (np.einsum("ij,ij->j", (us_step-m_step), diff0))
term2 = (np.einsum("ij,ij->j", (us_step-m_step), (us_step-m_step)))
division = np.divide(term1, term2, where=term2 > 0)
diff_orth = diff0 - division*(us_step-m_step)
return wmean + (us-wmean + diff_orth - tau*kappa*vs.T)*exp(-tau*lam)
else:
diff = sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
drift = -tau*kappa*vs.T - lam*tau*(us-wmean)
us_new = us +drift + diff#sig*np.linalg.norm(us-wmean, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
return us_new
# one step of vanilla CBO
def step(us, wmean):
drift = - lam*tau*(us-wmean)
if component_noise:
noise = sig*(us-m)*np.random.normal(0,sqrt(tau),(d,J))
else:
noise = sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
if orthogonal_noise:
diff0 = sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
us_step = us + 0.5*diff0
m_step = weighted_mean(us_step.T, lambda u: -alpha*Phi(u)).reshape((-1,1))
term1 = (np.einsum("ij,ij->j", (us_step-m_step), diff0))
term2 = (np.einsum("ij,ij->j", (us_step-m_step), (us_step-m_step)))
division = np.divide(term1, term2, where=term2 > 0)
diff_orth = diff0 - division*(us_step-m_step)
return wmean + (us-wmean + diff_orth)*exp(-tau*lam)
else:
diff = sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
return us +drift/(1+0.001*np.linalg.norm(drift)) + noise
def step_truegrad(us, wmean, onlymean=True, tau=1.0 ):
if onlymean:
drift = -tau*kappa*DPhi(wmean)- lam*tau*(us-wmean)
return us +drift/(1+0.001*np.linalg.norm(drift)) + sig*np.linalg.norm(us-wmean, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
else:
drift = -tau*kappa*DPhi(us)- lam*tau*(us-wmean)
return us +drift/(1+0.001*np.linalg.norm(drift)) + sig*np.linalg.norm(us-wmean, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
#%% prepare animation
if MC_test:
t1 = time.time()
optimizer = np.zeros((N_MC,2))
for n_MC in range(N_MC):
# initial ensemble
# initial ensemble
us0 = us0_global
# list of ensemble particles for each iteration
us_list = np.zeros(( N, d, J))
us_list[0, :, :] = us0
w_mean = np.zeros((N,d))
w_mean[0, :] = weighted_mean(us0.T, lambda u: -alpha*Phi(u))
for i in range(N-1):
#us_EKI[i+1, :] = EKI_step(us_EKI[i, :])
m = weighted_mean(us_list[i,:, :].T, lambda u: -alpha*Phi(u)).reshape((-1,1))
w_mean[i,:] = m.flatten()
if use_grad:
if use_truegrad:
us_list[i+1, :] = step_truegrad(us_list[i, :, :],m,onlymean=onlymean, tau=deltas[i])
else:
us_list[i+1, :] = step_gradHess(us_list[i, :, :],m,onlymean=onlymean, tau=deltas[i])
else:
us_list[i+1, :] = step(us_list[i, :, :],m)
m = weighted_mean(us_list[N-1,:, :].T, lambda u: -alpha*Phi(u)).reshape((-1,1))
w_mean[N-1,:] = m.flatten()
optimizer[n_MC, :] = w_mean[-1, :]
t2=time.time()
print(f"elapsed time = {t2-t1}")
fig, ax1 = plt.subplots(1,1, figsize=(5,5))
xdata, ydata = [], []
ln1, = ax1.plot([], [], 'ko')
ln2, = ax1.plot([], [], 'rs')
#plt.plot(xs, ys)
#plt.title("target function")
def init():
ax1.set_xlim(xmin, xmax)
ax1.set_ylim(ymin, ymax)
ax1.contourf(XX, YY, ZZ, 30)
return ln1, ln2,
def update(frame_num): # expects frame to contain the new position of the ensemble
xdata1 = us_list[frame_num, 0, :]
ydata1 = us_list[frame_num, 1, :]
ln1.set_data(xdata1, ydata1)
ln2.set_data(w_mean[frame_num, 0], w_mean[frame_num, 1])
ax1.set_title(f"it={frame_num}")
return ln1,
ani = FuncAnimation(fig, update, frames = range(N), init_func=init, blit=False, interval=20)
# new transparency-augmented color map for plotting histogram on top of utility function
import matplotlib.pylab as pl
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
# Choose colormap
cmap = pl.cm.plasma
# Get the colormap colors
my_cmap = cmap(np.arange(cmap.N))
# Set alpha
my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
# Create new colormap
my_cmap = ListedColormap(my_cmap)
fig2, ax2 = plt.subplots(1,1, figsize=(5,5))
ax2.set_xlim(xmin, xmax)
ax2.set_ylim(ymin, ymax)
ax2.contourf(XX, YY, ZZ, 30)
ax2.plot(optimizer[:,0], optimizer[:, 1], 'k.')
H, xedges, yedges =np.histogram2d(optimizer[:,0],optimizer[:,1], range=[[-4.25,4.25],[-4.25,4.25]], bins=17)
ax2.pcolormesh(xedges, yedges, H.T, cmap=my_cmap)
print(f"success rate: {np.sum(np.linalg.norm(optimizer, axis=1) <= 1e-2)/N_MC}")
else:
fig, ax1 = plt.subplots(1,1, figsize=(5,5))
xdata, ydata = [], []
ln1, = ax1.plot([], [], 'ko')
ln2, = ax1.plot([], [], 'rs')
#plt.plot(xs, ys)
#plt.title("target function")
def init():
ax1.set_xlim(xmin, xmax)
ax1.set_ylim(ymin, ymax)
ax1.contourf(XX, YY, ZZ, 30)
return ln1, ln2,
def update(frame_num): # expects frame to contain the new position of the ensemble
xdata1 = us_list[frame_num, 0, :]
ydata1 = us_list[frame_num, 1, :]
ln1.set_data(xdata1, ydata1)
ln2.set_data(w_mean[frame_num, 0], w_mean[frame_num, 1])
ax1.set_title(f"t={frame_num*T/N:.2f}")
return ln1,
for i in range(N-1):
#us_EKI[i+1, :] = EKI_step(us_EKI[i, :])
m = weighted_mean(us_list[i,:, :].T, lambda u: -alpha*Phi(u)).reshape((-1,1))
w_mean[i,:] = m.flatten()
if use_grad:
if use_truegrad:
us_list[i+1, :] = step_truegrad(us_list[i, :, :],m,onlymean=onlymean, tau=deltas[i])
else:
us_list[i+1, :] = step_gradHess(us_list[i, :, :],m,onlymean=onlymean, tau=deltas[i])
else:
us_list[i+1, :] = step(us_list[i, :, :],m)
m = weighted_mean(us_list[N-1,:, :].T, lambda u: -alpha*Phi(u)).reshape((-1,1))
w_mean[N-1,:] = m.flatten()
ani = FuncAnimation(fig, update, frames = range(N), init_func=init, blit=False, interval=2)
# f = r"P://Philipp/Erlangen/Forschung/Bayesian_CBO/animation.gif"
# writergif = PillowWriter(fps=30)
# anim.save(f, writer=writergif)
plt.figure()
plt.subplot(2,1,1)
plt.title("weighted mean")
it = np.linspace(0.0, T, N)
ns = np.arange(N)
ts = np.array([it[n] for n in ns])
plt.semilogy(it, [Phi(w_mean[n, :]) for n in range(N)], label="val of weighted mean")
vals = np.log(np.array([Phi(w_mean[n, :]) for n in ns]))
Phi_mat = np.stack((ts, np.ones_like(ns))).T
coeff = np.linalg.lstsq(Phi_mat, vals, rcond=None)[0] #np.linalg.solve(Phi_mat.T@Phi_mat, Phi_mat.T@vals)
plt.semilogy(ts, np.exp(coeff[1] + coeff[0]*ts), label=f"$\exp({coeff[1]:.2f} {coeff[0]:.2f}\cdot t)$")
plt.legend()
plt.subplot(2,1,2)
plt.loglog(it, [Phi(w_mean[n, :]) for n in range(N)], label="val of weighted mean")
plt.plot(it, np.exp(coeff[1] + coeff[0]*ts), label=f"$\exp({coeff[1]:.2f} {coeff[0]:.2f}\cdot t)$")
plt.legend()
plt.figure(figsize=(5,4))
plt.subplot(2,1,1)
plt.title("weighted mean")
# it = np.linspace(0.0, T, N)
ns = np.arange(N)
#ts = np.array([it[n] for n in ns])
plt.semilogy(ts, [Phi(w_mean[n, :]) for n in range(N)], label="val of weighted mean")
plt.subplot(2,1,2)
for p in range(J):
plt.semilogy(ts, Phi(us_list[:,:,p].T))
plt.tight_layout()
plt.figure()
plt.subplot(2,1,1)
plt.plot(ts, us_list[:,0,:])
plt.subplot(2,1,2)
plt.plot(ts, us_list[:,1,:])
plt.figure()
plt.semilogy(np.linalg.norm(us_list-w_mean[:,:,np.newaxis],axis=1))
plt.semilogy(np.mean(np.linalg.norm(us_list-w_mean[:,:,np.newaxis],axis=1),axis=-1), 'k', linewidth=3)
plt.title("collapse")
# code for plot of gradients
plt.figure()
plt.contourf(XX, YY, ZZ, 30)
xs = us_list[-1, :, :]
vs = Phi(xs)
plt.plot(xs[0,:], xs[1,:], '.')
grads = []
HS = []
if onlymean:
m = weighted_mean(xs.T, lambda u: -alpha*Phi(u)).reshape((-1,1))
wmean = m.flatten()
ensembleplusmean = np.concatenate((wmean[:,np.newaxis],
xs),axis=1)
values = np.concatenate(([Phi(wmean)],vs))
return_dict= inferGradientAndHess(ensembleplusmean, values, hessian=True, ind=0)
grads, H = return_dict['grad'], return_dict['H']
for k in range(J):
if onlymean:
plt.quiver(xs[0,k], xs[1,k], grads[0], grads[1], color="blue", label="max likelihood")
else:
ret = inferGradientAndHess(xs, vs, hessian=True,ind=k,penalizedistance=True)
gk, Hk = ret['grad'], ret['H']
grads = [*grads, gk]
HS = [*HS, Hk]
plt.quiver(xs[0,k], xs[1,k], grads[k][0], grads[k][1], color="blue", label="max likelihood")