-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.py
377 lines (296 loc) · 9.91 KB
/
final.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
# %% [markdown]
"""
## Gaussian Process from scratch with Tensorflow
This is a naive implementation of Gaussian Process in Tensorflow.
"""
# %% [markdown]
"""
### Import
"""
# %%
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import numpy as np
import tensorflow as tf
from IPython.display import HTML
import csv
jitter = 1e-7
# %% [markdown]
"""
### Utils
"""
# %%
# Help function for computing the Cholesky decomposition with added jitter for stability
@tf.function
def cholesky(X):
"""Cholesky with added jitter on the diagonal for stability
Args:
X (tf.Tensor): Matrix to be decomposed
Returns:
L (tf.Tensor): Lower matrix L where X = LL^T
"""
return tf.linalg.cholesky(X + tf.eye(X.shape[0], dtype=tf.float64) * jitter)
@tf.function
def KL(m0, L0, m1, L1):
"""KL divergence between two Gausisans N0(m0, S0), N1(m1, S1)
Args:
m0 (tf.Tensor): mean vector of N0 (N x 1)
L0 (tf.Tensor): Cholesky decomposition of S0 (N x N)
m1 (tf.Tensor): mean vector of N1 (N x 1)
L1 (tf.Tensor): Cholesky decomposition of S1 (N x N)
Returns:
KL divergence (scalar)
"""
N = tf.cast(m0.shape[0], tf.float64)
# Ensure triangularity
L0 = tf.linalg.band_part(L0, -1, 0)
L1 = tf.linalg.band_part(L1, -1, 0)
#
A = tf.reduce_sum(tf.math.pow(
tf.linalg.triangular_solve(L1, L0), 2)) # Compute L1_inv @ L0
B_1 = tf.linalg.triangular_solve(L1, (m1 - m0))
B = tf.reduce_sum(tf.transpose(B_1) @ B_1) - N
C = 2 * tf.reduce_sum(
tf.math.log(tf.math.abs(tf.linalg.tensor_diag_part(L0)))
- tf.math.log(tf.math.abs(tf.linalg.tensor_diag_part(L1)))
)
return 1 / 2 * (A + B + C)
@tf.function
def ncdf(x):
"""Cumulative distribution function for the standard (multivariate) normal distribution.
(Used for squashing)
Args:
x (tf.Tensor): evaluation points
Returns:
(tf.Tensor): cumulative distribution function evaluated at x
"""
return (1 + tf.math.erf(x / np.sqrt(2))) / 2
@tf.function
def classif_log_likelihood(f, y):
"""log p(y|f) for classification using the normal cdf
log p(y|f) = log phi(y * f)
"""
return tf.math.log(ncdf(y * f) + 1e-7)
# %% [markdown]
"""
### Kernel function
"""
# %%
class RBF:
def __init__(self, alpha=1, scale=1) -> None:
"""Squared exponential kernel between two data matrix A and B
Args:
alpha, scale (float|tf.Variable): hyperparameters of the kernel
"""
self.alpha = tf.Variable(alpha, dtype=tf.float64)
self.scale = tf.Variable(scale, dtype=tf.float64)
self.parameters = [self.alpha, self.scale]
def __call__(self, A, B):
"""
Args:
A (tf:Tensor): data matrix A (N x D)
B (tf:Tensor): data matrix B (M x D)
Returns:
(scalar)
"""
AA = tf.reshape(tf.reduce_sum(A * A, 1), (-1, 1))
BB = tf.reshape(tf.reduce_sum(B * B, 1), (-1, 1))
D = AA - 2 * A @ tf.transpose(B) + tf.transpose(BB)
return self.alpha * tf.exp(-D / (2 * tf.pow(self.scale, 2)))
# %% [markdown]
# ## Sparse Variational Gaussian Process (SVGP) model
# %%
class SVGP:
def __init__(
self,
data,
kernel,
likelihood,
ns=10,
lr=0.0005,
n_gh=10,
X_s_init=None,
silent=False,
):
self.X, self.y = data
self.llh = likelihood
self.kernel = kernel
self.ns = ns
self.optimizer = tf.optimizers.Adam(learning_rate=lr)
self.num_steps = num_steps
self.variables = []
self.silent = silent
# for Gaussian-Hermite quadrature approximation
GQ_t, GQ_w = np.polynomial.hermite.hermgauss(n_gh)
self.GQ_w = tf.reshape(tf.constant(
GQ_w, dtype=tf.float64), (1, -1)) # 1 x m
self.GQ_t = tf.reshape(tf.constant(
GQ_t, dtype=tf.float64), (1, -1)) # 1 x m
# initialize inducing points
if X_s_init is not None:
ns = X_s_init.shape[0]
else:
X_s_init = X[np.random.choice(X.shape[0], ns), :]
self.X_s = tf.Variable(X_s_init, dtype=tf.float64)
# reparameterization vector u ~ N(m_u, Sigma_u)
self.m_u = tf.Variable(np.zeros((ns, 1)), dtype=tf.float64)
self.L_u = tf.Variable(np.eye(ns), dtype=tf.float64)
self.variables = [self.X_s, self.m_u,
self.L_u, *self.kernel.parameters]
def expected_log_likelihood(self, means, mvars, y):
stds = tf.reshape(tf.sqrt(mvars), (-1, 1)) # 1 x N
f = means + np.sqrt(2) * stds @ self.GQ_t # N x m
aprox_terms = self.GQ_w / np.sqrt(2) * self.llh(f, y) # N x m
return tf.reduce_sum(aprox_terms, axis=1) # N x 1
def _predict_f(self, X_new, X, m_q, L_q):
# ensure triangularity
L_q = tf.linalg.band_part(L_q, -1, 0)
K_fsf = self.kernel(X_new, X)
K_ff = self.kernel(X, X)
K_fsfs = self.kernel(X_new, X_new)
inv = tf.linalg.inv(K_ff)
pos_m = K_fsf @ inv @ m_q
pos_var = tf.linalg.tensor_diag_part(
K_fsfs
- K_fsf @ inv @ (K_ff - L_q @ tf.transpose(L_q)
) @ inv @ tf.transpose(K_fsf)
)
return pos_m, tf.reshape(pos_var, (-1, 1))
def posterior(self):
L_ps = cholesky(self.kernel(self.X_s, self.X_s))
m_qs = L_ps @ self.m_u
V = L_ps @ self.L_u
L_qs = V @ tf.transpose(V)
return m_qs, L_qs @ tf.transpose(L_qs)
def predict_f(self, X_new):
return self._predict_f(X_new, self.X_s, *self.posterior())
def elbo(self):
# prior
m_ps = tf.constant(np.zeros((self.ns, 1)))
L_ps = cholesky(self.kernel(self.X_s, self.X_s))
# whitening
m_qs = L_ps @ self.m_u
V = L_ps @ self.L_u
L_qs = V @ tf.transpose(V)
#
q_means, q_vars = self._predict_f(self.X, self.X_s, m_qs, L_qs)
ve = tf.reduce_sum(
self.expected_log_likelihood(q_means, q_vars, self.y))
kl = KL(m_qs, L_qs, m_ps, L_ps)
return ve - kl
def loss(self):
return -self.elbo()
@tf.function
def train_step(self):
with tf.GradientTape() as tape:
tape.watch(self.variables)
loss_value = self.loss()
gradients = tape.gradient(loss_value, self.variables)
self.optimizer.apply_gradients(zip(gradients, self.variables))
def train(self, num_steps):
losses = []
inducing_points = []
posteriors = []
for step in range(num_steps):
self.train_step()
if step % 500 == 0:
l = tf.reduce_mean(self.loss()).numpy()
losses.append(l)
inducing_points.append(self.X_s.numpy())
posteriors.append(self.posterior())
if not self.silent:
print(step, "avg_loss: ", l)
return losses, inducing_points, posteriors
# %% [markdown]
"""
### Training
`banana.csv` dataset is used.
"""
# %%
# Data
XY = []
with open("banana.csv") as csvfile:
# change contents to floats
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)
for row in reader: # each row is a list
XY.append(row)
XY = np.array(XY)
# Here we select a subset of the data. (remember, computation scales as N^3)
np.random.shuffle(XY)
N = 1000
X, Y = XY[:N, :-1], XY[:N, -1:]
Y = (Y - 1.5) * 2 # to be in {-1, 1}
N = X.shape[0]
# Plotting the data
plt.scatter(X[:, 0], X[:, 1], c=Y)
plt.xlabel("$x_1$", fontsize=15)
plt.ylabel("$x_2$", fontsize=15)
plt.title("Classification data", fontsize=20)
# %%
# Model
num_steps = 15000
model = SVGP((X, Y), RBF(), classif_log_likelihood)
losses, inducing_points, posteriors = model.train(num_steps)
# %% [markdown]
# ### Visualize
# %%
def plot_losses(losses, num_steps):
plt.plot(np.arange(0, num_steps, 500), -np.array(losses))
plt.title("ELBO values during training")
plt.show()
def plot_prediction(model):
# create new input points on grid
n_grid = 100
x = np.linspace(X.min(), X.max(), n_grid)
X1new, X2new = np.meshgrid(x, x)
Xnew = np.hstack(
[X1new.reshape(-1, 1), X2new.reshape(-1, 1)]
) # size : n_grid * n_grid x 2
plt.figure(figsize=(12, 6))
pos_m, pos_v = model.predict_f(Xnew)
pdf = pos_m.numpy() / np.sqrt(1 + pos_v.numpy())
plt.subplot(1, 2, 1)
plt.contourf(X1new, X2new, pdf.reshape((n_grid, n_grid)), cmap="jet")
plt.scatter(model.X_s[:, 0], model.X_s[:, 1],
c="red", label="trained inducing.p")
plt.legend()
plt.subplot(1, 2, 2)
plt.contourf(X1new, X2new, pdf.reshape((n_grid, n_grid)), cmap="jet")
for g, c in zip([-1, 1], ["blue", "yellow"]):
ix = np.where(Y == g)
plt.scatter(model.X[ix, 0], model.X[ix, 1], color=c, label=str(g))
plt.legend()
plt.show()
print(model.kernel.alpha, model.kernel.scale)
plot_losses(losses, num_steps)
plot_prediction(model)
# %% [markdown]
# ### Animate
# %%
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
# create new input points on grid
n_grid = 100
x = np.linspace(X.min(), X.max(), n_grid)
X1new, X2new = np.meshgrid(x, x)
Xnew = np.hstack(
[X1new.reshape(-1, 1), X2new.reshape(-1, 1)]
) # size : n_grid * n_grid x 2
def plot_prediction_animation(model, X_s, posterior, ax):
pos_m, pos_v = model._predict_f(Xnew, X_s, *posterior)
pdf = pos_m.numpy() / np.sqrt(1 + pos_v.numpy())
ax.contourf(X1new, X2new, pdf.reshape((n_grid, n_grid)), cmap="jet")
ax.scatter(X_s[:, 0], X_s[:, 1], c="red", label="trained inducing.p")
ax.legend()
def animate(i):
ax.cla() # clear the previous image
plot_prediction_animation(
model,
inducing_points[i],
posteriors[i],
ax,
)
anim = ani.FuncAnimation(fig, animate, frames=len(
losses), interval=1, blit=False)
HTML(anim.to_jshtml())
# %%
anim.save('animation.gif', writer='imagemagick')