-
Notifications
You must be signed in to change notification settings - Fork 4
/
learning.py
266 lines (186 loc) · 8.61 KB
/
learning.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
#
# This file is part of the RGP distribution (https://github.com/smidmatej/RGP).
# Copyright (c) 2023 Smid Matej.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Core
import numpy as np
from RGP import RBF, RGP
# Plotting
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib import gridspec
from matplotlib.collections import PolyCollection
# Cosmetic
import seaborn as sns
from tqdm import tqdm
def main():
# ----- The basis vectors -----
X_ = np.arange(-10,10,1).reshape(-1,1)
y_ = np.random.normal(0, 0, size=X_.shape)
# ----- The true function we are trying to approximate -----
X_query = np.arange(-10,10,0.2).reshape(-1,1)
y_true = np.sin(X_query)
# ----- The training data -----
n_training = 50
X_t = np.random.uniform(-10,10, size=(n_training,1))
X_t = np.sort(X_t, axis=0)
# Put half of the data at the end in reverse order
X_t = np.concatenate((X_t[::2], np.flip(X_t[::2])), axis=0)
y_t = np.sin(X_t) + np.random.normal(0, 0.1, size=X_t.shape)
rgp = RGP(X_, y_)
mean_training = [None] * (X_t.shape[0]+1)
cov_training = [None] * (X_t.shape[0]+1)
g_ = [None] * (X_t.shape[0]+1)
eta = [None] * (X_t.shape[0]+1)
mean_training[0], cov_training[0] = rgp.predict(X_query, cov=True)
g_[0] = rgp.mu_g_t # Current estimate of g at X
eta[0] = rgp.mu_eta_t
print('Training model recursively...')
pbar = tqdm(total=X_t.shape[0])
for t in range(X_t.shape[0]):
#rgp.regress(X_t[t,:], y_t[t,:])
rgp.learn(X_t[t,:], y_t[t,:])
mean_training[t+1], cov_training[t+1] = rgp.predict(X_query, cov=True)
g_[t+1] = rgp.mu_g_t # Current estimate of g at X
eta[t+1] = rgp.mu_eta_t # Current estimate of eta at X
pbar.update()
pbar.close()
mean_posterior, cov_posterior = rgp.predict(X_query, cov=True)
std_posterior = np.sqrt(np.diag(cov_posterior))
cs = [[x/256 for x in (205, 70, 49)], \
[x/256 for x in (105, 220, 158)], \
[x/256 for x in (102, 16, 242)], \
[x/256 for x in (7, 59, 58)]]
# Color scheme convert from [0,255] to [0,1]
cs = [[x/256 for x in (8, 65, 92)], \
[x/256 for x in (204, 41, 54)], \
[x/256 for x in (118, 148, 159)], \
[x/256 for x in (232, 197, 71)]]
# ----------------- ANIMATION -----------------
print('Rendering animation...')
def animate(i):
line_mean.set_data(X_query, mean_training[i])
scat_basis_vectors.set_offsets(np.array([X_.ravel(), g_[i].ravel()]).T)
scat_training_points.set_offsets(np.array([X_t[:i,:].ravel(), y_t[:i,:].ravel()]).T)
global fill_between
fill_between.remove()
fill_between = ax.fill_between(X_query.reshape(-1),
mean_training[i].reshape(-1) - 2*np.sqrt(np.diag(cov_training[i])),
mean_training[i].reshape(-1) + 2*np.sqrt(np.diag(cov_training[i])), color=cs[1], alpha=0.2)
#line_eta[0].set_data([0,1],)
for k in range(len(line_eta)):
#breakpoint()
if i >= 1:
#[x[k] for x in eta[:i]]
#line_eta[k].set_data(np.arange(i), [np.exp(x[k]) for x in eta[:i]])
line_eta[k].set_data(np.arange(i), [x[k] for x in eta[:i]])
#print(f"Eta: {eta[i]}")
#pass
pbar.update()
animation.writer = animation.writers['ffmpeg']
plt.ioff() # Turn off interactive mode to hide rendering animations
plt.style.use('fast')
sns.set_style("whitegrid")
gs = gridspec.GridSpec(2, 3)
fig = plt.figure(figsize=(10,10), dpi=100)
ax = fig.add_subplot(gs[0,:])
line_mean, = ax.plot([], [], '--', color=cs[0], label='E[g(x)]')
scat_training_points = ax.scatter([], [], marker='+', color=cs[1], label='Training samples')
scat_basis_vectors = ax.scatter([], [], marker='o', color=cs[2], label='Basis Vectors')
# Hack to be able to change the fill at each step
global fill_between
fill_between = ax.fill_between([],
[],
[], color=cs[3], alpha=0.2)
ax.plot(X_query, y_true, color='gray')
ax.set_xlim((min((min(X_t), min(X_query), min(X_))) , max((max(X_t), max(X_query), max(X_)))))
#ax.set_ylim((min((min(y_), min(y_t), min(y_true))) , max((max(y_), max(y_t), max(y_true)))))
ax.set_ylim((-2,2))
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Recursive Gaussian Process')
ax.legend()
# Eta plot
ax_eta = []
line_eta = []
for i in range(3):
ax_eta.append(fig.add_subplot(gs[1,i]))
line, = ax_eta[i].plot([],[])
line_eta.append(line)
ax_eta[i].set_xlim((0.0, len(eta)))
ax_eta[i].set_ylim((0.0, max([np.exp(x[i]) for x in eta])))
ax_eta[i].set_ylim((0.0, max([x[i] for x in eta])))
ax_eta[i].set_xlabel('Iteration')
pbar = tqdm(total=X_t.shape[0])
ani = animation.FuncAnimation(fig, animate, frames=len(mean_training), interval=500)
#ani.save('animation.mp4', writer='ffmpeg', fps=10, dpi=100)
ani.save('animation.gif', writer='imagemagick', fps=10, dpi=100)
pbar.close()
plt.show()
# ----------------- PLOT -----------------
plt.style.use('fast')
sns.set_style("whitegrid")
fig = plt.figure(figsize=(10,10), dpi=100)
gs = gridspec.GridSpec(2, 2, width_ratios=[1, 1], height_ratios=[1, 1])
ax = [None]*4
ax[0] = fig.add_subplot(gs[0, 0])
ax[1] = fig.add_subplot(gs[0, 1])
ax[2] = fig.add_subplot(gs[1, 0])
ax[3] = fig.add_subplot(gs[1, 1])
ax[0].plot(X_query, y_true, color=cs[1], label='True function')
ax[0].plot(X_query, mean_training[0], '--', color=cs[0], label='E[g(x)]')
ax[0].scatter(X_, y_, marker='o', s=20, color=cs[0], label='Basis Vectors')
ax[0].fill_between(X_query.reshape(-1),
mean_training[0].reshape(-1) - 2*np.sqrt(np.diag(cov_training[0])),
mean_training[0].reshape(-1) + 2*np.sqrt(np.diag(cov_training[0])), color=cs[3], alpha=0.1)
ax[0].set_xlim((min((min(X_t), min(X_query), min(X_))) , max((max(X_t), max(X_query), max(X_)))))
#plt.set_ylim((min((min(y_), min(y_t), min(y_true))) , max((max(y_), max(y_t), max(y_true)))))
ax[0].set_ylim((-2,2))
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
ax[0].legend()
ax[0].set_title('Prior with no training data')
ax[1].plot(X_query, y_true, color=cs[1], label='True function')
ax[1].scatter(X_, g_[-1], marker='o', s=20, color=cs[0], label='Basis Vectors')
ax[1].scatter(X_t, y_t, marker='+', color=cs[2], label='Training samples')
ax[1].plot(X_query, mean_training[-1], '--', color=cs[0], label='E[g(x)]')
ax[1].fill_between(X_query.reshape(-1),
mean_training[-1].reshape(-1) - 2*np.sqrt(np.diag(cov_training[-1])),
mean_training[-1].reshape(-1) + 2*np.sqrt(np.diag(cov_training[-1])), color=cs[3], alpha=0.1)
ax[1].set_xlim((min((min(X_t), min(X_query), min(X_))) , max((max(X_t), max(X_query), max(X_)))))
#plt.set_ylim((min((min(y_), min(y_t), min(y_true))) , max((max(y_), max(y_t), max(y_true)))))
ax[1].set_ylim((-2,2))
ax[1].set_xlabel('x')
ax[1].set_ylabel('y')
ax[1].legend()
ax[1].set_title('Posterior after training')
ax[2].plot(X_query, y_true - mean_training[0], color=cs[0])
ax[2].set_xlim((min((min(X_t), min(X_query), min(X_))) , max((max(X_t), max(X_query), max(X_)))))
#plt.set_ylim((min((min(y_), min(y_t), min(y_true))) , max((max(y_), max(y_t), max(y_true)))))
ax[2].set_ylim((-2,2))
ax[2].set_xlabel('x')
ax[2].set_ylabel('y')
ax[2].set_title('Difference (true - prior)')
ax[3].plot(X_query, y_true - mean_training[-1], color=cs[0])
ax[3].set_xlim((min((min(X_t), min(X_query), min(X_))) , max((max(X_t), max(X_query), max(X_)))))
#plt.set_ylim((min((min(y_), min(y_t), min(y_true))) , max((max(y_), max(y_t), max(y_true)))))
ax[3].set_ylim((-2,2))
ax[3].set_xlabel('x')
ax[3].set_ylabel('y')
ax[3].set_title('Difference (true - prior)')
plt.savefig('before_after_difference.png')
plt.show()
if __name__ == '__main__':
main()