-
Notifications
You must be signed in to change notification settings - Fork 0
/
grad_cbo_dd.py
436 lines (339 loc) · 13.6 KB
/
grad_cbo_dd.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# -*- 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, sqrt, ceil, exp
import time
from grad_inference import *
from matplotlib.animation import FuncAnimation, PillowWriter
np.random.seed(1)
d = 10
exponential_correction = True
# squared norm
Phi = lambda z: 0.5*np.linalg.norm(z-1, axis=0)**4# just for safety
DPhi = lambda z: z
# Rastrigin
A = 3
Phi = lambda z: A*d + np.sum(z**2 - A*np.cos(2*pi*z), axis=0)
# 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 = True
N_MC = 100
# 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
orth_noise = False
# xmin = -4.5
# xmax = +4.5
# ymin = -4.5
# ymax = +4.5
# xs_plot = np.linspace(xmin,xmax,50)
# ys_plot = np.linspace(ymin,ymax,50)
# XX, YY = np.meshgrid(xs_plot, ys_plot)
# ZZ = Phi(np.stack((XX, YY)))
# plt.figure()
# plt.contourf(XX,YY,ZZ, 30)
T = 20.0
# tau = 0.001
# N = int(T/tau)
# deltas = [tau for k in range(1,N)]
# ts = np.arange(0,T,tau)
# new timing scheme
r = 0.3
delta0 = 0.01
N = 5000
deltas = [delta0*k**(-r) for k in range(1,N+1)]
ts = np.cumsum(deltas)
tau0 = delta0
# Parameter für Rastrigin 50-d
alpha = 30.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 = 40 # size of ensemble
J = N_ens
avar = 0.0
# Parameter für quadratische Funktion 50-d
# alpha = 30.0 # weight exponent of cost function exp(-alpha*Phi)
# lam = 1.0 # coefficient of contraction towards weighted mean
# sig = 4.0 # coefficient of noise 0.2
# kappa = 0.5#2.5#1.5 # coefficient of gradient drift term
# N_ens = 10 # size of ensemble
# J = N_ens
# avar = 0.0
# inferGradientAndHess(xs, vs, hessian = True, ind=0, retOnlyMatrix=False)
# initial ensemble
us0_global = np.random.uniform(-3,3,(d,J)) # hard for functions with minimum at (0,0)
#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, m, onlymean=True, tau=1.0, it=0, coeffs0=None, memory=None):
#m = weighted_mean(us_list.reshape((-1,1)), lambda u: -alpha*Phi(u))
if onlymean:
if memory is not None:
ensembleplusmean = np.concatenate((m,us,memory),axis=1)
else:
ensembleplusmean = np.concatenate((m,us),axis=1)
values = Phi(ensembleplusmean)
#values = np.concatenate((Phi(m),Phi(us)))
return_dict= inferGradientAndHess(ensembleplusmean, values, hessian=True, ind=0, additionalvariance=avar, coeffs0=coeffs0)
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)
gradterm[it,:,:] = vs.T
contrterm[it,:,:] = us-m
#tau = max(tau/100, min(tau, tau*np.max(np.linalg.norm(vs, axis=-1))))
if exponential_correction:
temp1 = us -tau*kappa*vs.T
us = m + (temp1-m)*exp(-lam*tau)
# temp1 = m + (us-m)*exp(-lam*tau)
# us = temp1-tau*kappa*vs.T
else:
us = us -tau*kappa*vs.T - lam*tau*(us-m)
#drift = -tau*kappa*vs.T - lam*tau*(us-m)
if component_noise:
diff = sig*(us-m)*np.random.normal(0,sqrt(tau),(d,J))
else:
diff = sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
# project onto orthogonal of drift
# diff_proj = np.zeros((d,J))
# for j in range(J):
# diff_proj[:,j] = diff[:,j] - (diff[:,j]@drift[:,j])/(drift[:,j]@drift[:,j])*drift[:,j]
us_new = us + diff#diff_proj
return us_new
# one step of vanilla CBO
def step(us, m):
drift = - lam*tau*(us-m)
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 exponential_correction:
return m + (us-m)*exp(-lam*tau) + noise
else:
return us +drift/(1+0.001*np.linalg.norm(drift)) + noise
def step_truegrad(us, m, onlymean=True, tau=1.0, it=0 ):
gradterm[it,:,:] = DPhi(m)
contrterm[it,:,:] = us-m
if exponential_correction:
print("warning, not yet implemented")
if onlymean:
drift = -tau*kappa*DPhi(m)- lam*tau*(us-m)
return us +drift + sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
else:
drift = -tau*kappa*DPhi(us)- lam*tau*(us-wmean)
return us +drift + sig*np.linalg.norm(us-m, axis=0)*np.random.normal(0,sqrt(tau),(d,J))
#%% prepare animation
global gradterm
gradterm = np.zeros((N,d,J))
global contrterm
contrterm = np.zeros((N,d,J))
if MC_test:
t1 = time.time()
plt.figure()
optimizer = np.zeros((N_MC,d))
for n_MC in range(N_MC):
print(f"iteration {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))
global coeffs_previous
coeffs_previous = None
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 i > 0:
old_pos = (us_list[i-1,:])[np.newaxis, :]
if use_grad:
if use_truegrad:
us_list[i+1, :] = step_truegrad(us_list[i, :, :],m,onlymean=onlymean, tau=deltas[i])
else:
old_pos = None
us_list[i+1, :] = step_gradHess(us_list[i, :, :],m,onlymean=onlymean, tau=deltas[i], coeffs0=None, memory = old_pos)
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, :]
plt.plot(np.linalg.norm(w_mean, axis=1))
plt.figure()
plt.hist(Phi(optimizer.T), bins=10)
plt.figure()
plt.hist(np.linalg.norm(optimizer, axis=1))
sqnorm = 1/d*np.linalg.norm(optimizer, axis=1)**2
abw = np.max(np.abs(optimizer[:,:]), axis=1)
print(f"success: {np.sum(abw < 0.25)/N_MC}")
t2=time.time()
print(f"elapsed time = {t2-t1}")
else:
from time import time
t1 = time()
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], it=i)
else:
us_list[i+1, :] = step_gradHess(us_list[i, :, :],m,onlymean=onlymean, tau=deltas[i], it=i)
else:
us_list[i+1, :] = step(us_list[i, :, :],m)
print(f"time elapsed: {time()-t1}")
m = weighted_mean(us_list[N-1,:, :].T, lambda u: -alpha*Phi(u)).reshape((-1,1))
w_mean[N-1,:] = m.flatten()
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)
print(w_mean[-1])
plt.figure(figsize=(5,8))
plt.subplot(3,1,1)
plt.semilogy(ts, [Phi(w_mean[n, :]) for n in range(N)])
plt.title("Phis")
plt.subplot(3,1,2)
plt.semilogy(ts, np.sum(np.linalg.norm(us_list-w_mean[:,:,np.newaxis], axis=1), axis=1))
plt.title("ensemble collapse")
plt.tight_layout()
plt.subplot(3,1,3)
plt.plot(ts, us_list[:,0,:])
plt.tight_layout()
# plt.figure(figsize=(7.5,4))
# plt.semilogy(ts, Phis_l1d5, label="lamda=1.5")
# plt.semilogy(ts, Phis_l2d5, label="lamda=2.5")
# plt.semilogy(ts, Phis_l3d5, label="lamda=3.5")
# plt.semilogy(ts, Phis_l4d5, label="lamda=4.5")
# plt.legend()
# plt.tight_layout()
# plt.legend(loc="upper right")
# for j in range(J):
# plt.semilogy(ts, [Phi(us_list[n, :, j]) for n in range(N)], label="val of particle mean")
#plt.semilogy(ts, np.exp(-(lam+kappa-d*sig**2/2)*ts))
# plt.tight_layout()
# plt.figure()
# plt.semilogy(ts, [Phi(us_list[i,:,:]) for i in ns], 'r')
# patch_Phi = mpatches.Patch(color="red", label='Phis')
# plt.semilogy(ts, np.linalg.norm(gradterm[:,:,:],axis=1), 'k')
# patch_grad = mpatches.Patch(color="black", label='grad')
# plt.semilogy(ts, np.exp(-kappa*ts), 'k--')
# plt.semilogy(ts, np.linalg.norm(contrterm[:,:,:],axis=1), 'b')
# patch_contr = mpatches.Patch(color="blue", label='contr')
# handles=[patch_Phi,patch_grad,patch_contr]
# plt.legend(handles=[h for h in handles if h is not None])
# plt.figure()
# plt.semilogy(ts, np.linalg.norm(gradterm[:,:,0]-us_list[:,:,0], axis=1))
# plt.title("norm of error in gradient approximation")
# plt.subplot(3,1,2)
# plt.semilogy(ts, np.sum(np.linalg.norm(us_list-w_mean[:,:,np.newaxis], axis=1), axis=1))
# plt.title("ensemble collapse")
# plt.tight_layout()
# plt.figure()
# plt.semilogy(ts, np.mean(np.linalg.norm(us_list-w_mean[:,:,np.newaxis], axis=2), axis=1), label="deviation")
# plt.semilogy(ts, np.mean(np.linalg.norm(us_list-0, axis=2), axis=1), label="distance")
# plt.legend()
# angles = np.zeros((N))
# for n in range(1,N):
# g = gradterm[n,:,0]
# wm = w_mean[n,:]
# angles[n] = g@wm/sqrt(g@g*wm@wm)
# plt.figure()
# plt.plot(ts[1:], angles[1:])
# plt.figure()
# from math import sqrt
# nrow = int(d/sqrt(d)) + ceil(d % sqrt(d))
# ncol = int(d/sqrt(d))
# for dd in range(d):
# plt.subplot(nrow,ncol,dd+1)
# plt.plot(ts, us_list[:,dd,:])
#plt.semilogy(ts, np.linalg.norm(us_list-w_mean[:,:,np.newaxis],axis=1),'g')
# # plot of 1st coordinate
# # plt.figure(figsize=(5,2))
# plt.subplot(3,1,3)
# plt.plot(ts, us_list[:,0,:])
# plt.tight_layout()
# # plot of ensemble deviations
# plt.figure()
# plt.semilogy(ts, np.linalg.norm(us_list-w_mean[:,:,np.newaxis],axis=1))
# plt.title("distance from weighted mean")
# # 2d animation
# 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.plot(us_list[:,0,:], us_list[:,1,:], '.-')
# plt.plot(us_list[0,0,:], us_list[0,1,:], 'r.')
# plt.plot(us_list[int(20//tau),0,:], us_list[int(20//tau),1,:], 'gx')
# fig, ax1 = plt.subplots(1,1, figsize=(5,5))
# xdata, ydata = [], []
# ln1, = ax1.plot([], [], 'ko')
# ln2, = ax1.plot([], [], 'rs')
# ln3, = ax1.plot([],[],'b-')
# xmin = -0.1
# xmax = 0.1
# ymin = -0.1
# ymax = 0.1
# 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], 0], [w_mean[frame_num, 1], 0])
# ax1.set_title(f"t={frame_num*T/N:.2f}")
# mx = np.mean(xdata1)
# my = np.mean(ydata1)
# dx = max(xdata1)-min(xdata1)
# dy = max(ydata1)-min(ydata1)
# dd = max(dx, dy)
# center = w_mean[frame_num, :]
# factor = sqrt(center@center)
# ln3.set_data([w_mean[frame_num, 0], (1-dd)/factor*w_mean[frame_num, 0]],[w_mean[frame_num, 1], (1-dd)/factor*w_mean[frame_num, 1]])
# ax1.set_xlim((mx-dd,mx+dd))
# ax1.set_ylim((my-dd,my+dd))
# return ln1,
# ani = FuncAnimation(fig, update, frames = range(0,N,2), init_func=init, blit=False, interval=2)
# # plt.figure()
# # plt.plot(us_list[nn,0,:], us_list[nn,1,:], '.')
# # plt.quiver(us_list[nn,0,:], us_list[nn,1,:], -us_list[nn,0,:], -us_list[nn,1,:])
# # plt.plot(0,0,'rx')