forked from GabrieleSantin/VKOGA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvkoga.py
More file actions
327 lines (256 loc) · 11.9 KB
/
vkoga.py
File metadata and controls
327 lines (256 loc) · 11.9 KB
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
#!/usr/bin/env python3
from vkoga.kernels import Gaussian
import numpy as np
from sklearn.base import BaseEstimator
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from scipy.spatial import distance_matrix
# VKOGA implementation
class VKOGA(BaseEstimator):
def __init__(self, kernel=Gaussian(), kernel_par=1,
verbose=True, n_report=10,
greedy_type='p_greedy', reg_par=0, restr_par=0,
tol_f=1e-10, tol_p=1e-10):
# Set the verbosity on/off
self.verbose = verbose
# Set the frequency of report
self.n_report = n_report
# Set the params defining the method
self.kernel = kernel
self.kernel_par = kernel_par
self.greedy_type = greedy_type
self.reg_par = reg_par
self.restr_par = restr_par
self.flag_val = None
# Set the stopping values
self.tol_f = tol_f
self.tol_p = tol_p
# Some further settings
self.ctrs_ = None
self.Cut_ = None
self.c = None
# Initialize the convergence history
self.train_hist = {}
self.train_hist['n'] = []
self.train_hist['f'] = []
self.train_hist['p'] = []
self.train_hist['p selected'] = [] # list of selected power vals
self.train_hist['f val'] = []
self.train_hist['p val'] = []
def selection_rule(self, f, p):
if self.restr_par > 0:
p_ = np.max(p)
restr_idx = np.nonzero(p >= self.restr_par * p_)[0]
else:
restr_idx = np.arange(len(p))
f = np.sum(f ** 2, axis=1)
if self.greedy_type == 'f_greedy':
idx = np.argmax(f[restr_idx])
idx = restr_idx[idx]
f_max = np.max(f)
p_max = np.max(p)
elif self.greedy_type == 'fp_greedy':
idx = np.argmax(f[restr_idx] / p[restr_idx])
idx = restr_idx[idx]
f_max = np.max(f)
p_max = np.max(p)
elif self.greedy_type == 'p_greedy':
f_max = np.max(f)
idx = np.argmax(p)
p_max = p[idx]
return idx, f_max, p_max
def fit(self, X, y, X_val=None, y_val=None, maxIter=None):
# Check the datasets
X, y = check_X_y(X, y, multi_output=True)
if len(y.shape) == 1:
y = y[:, None]
if X_val is None or y_val is None:
X_val = None
y_val = None
self.flag_val = False
else:
self.flag_val = True
X_val, y_val = check_X_y(X_val, y_val, multi_output=True)
# We will assume in the following that X_val and X are disjoint
if len(y_val.shape) == 1:
y_val = y_val[:, None]
# Check whether already fitted
if self.ctrs_ is None:
self.ctrs_ = np.zeros((0, X.shape[1]))
self.Cut_ = np.zeros((0, 0))
self.c = np.zeros((0, y.shape[1]))
# Check whether "new X" and previously chosen centers overlap
list_truly_new_X = []
if self.ctrs_.shape[0] > 0:
for idx_x in range(X.shape[0]):
if min(np.linalg.norm(self.ctrs_ - X[idx_x, :], axis=1)) < 1e-10:
continue
else:
list_truly_new_X.append(idx_x)
else:
list_truly_new_X = list(range(X.shape[0]))
X = X[list_truly_new_X, :]
y = y[list_truly_new_X, :]
# Initialize the residual and update the given y values by substracting the current model
y = np.array(y)
if len(y.shape) == 1:
y = y[:, None]
y = y - self.predict(X)
if self.flag_val:
y_val = y_val - self.predict(X_val)
# Get the data dimension
N, q = y.shape
if self.flag_val:
N_val = y_val.shape[0]
# Set maxIter_continue
if maxIter is None or maxIter > N:
self.maxIter = 100
else:
self.maxIter = maxIter
# Check compatibility of restriction
if self.greedy_type == 'p_greedy':
self.restr_par = 0
if not self.reg_par == 0:
self.restr_par = 0
# Initialize list for the chosen and non-chosen indices
indI_ = []
notIndI = list(range(N))
c = np.zeros((self.maxIter, q))
# Compute the Newton basis values (related to the old centers) on the new X
Vx_new_X_old_ctrs = self.kernel.eval(X, self.ctrs_) @ self.Cut_.transpose()
if self.flag_val:
Vx_val_new_X_old_ctrs = self.kernel.eval(X_val, self.ctrs_) @ self.Cut_.transpose()
# Initialize arrays for the Newton basis values (related to the new centers) on the new X
Vx = np.zeros((N, self.maxIter))
if self.flag_val:
Vx_val = np.zeros((N_val, self.maxIter))
# Compute the powervals on X and X_val
p = self.kernel.diagonal(X) + self.reg_par
p = p - np.sum(Vx_new_X_old_ctrs ** 2, axis=1)
if self.flag_val:
p_val = self.kernel.diagonal(X_val) + self.reg_par
p_val = p_val - np.sum(Vx_val_new_X_old_ctrs ** 2, axis=1)
# Extend Cut_ matrix, i.e. continue to build on old self.Cut_ matrix
N_ctrs_so_far = self.Cut_.shape[0]
Cut_ = np.zeros((N_ctrs_so_far + self.maxIter, N_ctrs_so_far + self.maxIter))
Cut_[:N_ctrs_so_far, :N_ctrs_so_far] = self.Cut_
# Iterative selection of new points
self.print_message('begin')
for n in range(self.maxIter):
# prepare
self.train_hist['n'].append(self.ctrs_.shape[0] + n + 1)
self.train_hist['f'].append([])
self.train_hist['p'].append([])
self.train_hist['p selected'].append([])
if self.flag_val:
self.train_hist['p val'].append([])
self.train_hist['f val'].append([])
# select the current index
idx, self.train_hist['f'][-1], self.train_hist['p'][-1] = self.selection_rule(y[notIndI], p[notIndI])
self.train_hist['p selected'][-1] = p[notIndI[idx]]
if self.flag_val:
self.train_hist['p val'][-1] = np.max(p_val)
self.train_hist['f val'][-1] = np.max(np.sum(y_val ** 2, axis=1))
# add the current index
indI_.append(notIndI[idx])
# check if the tolerances are reacheded
if self.train_hist['f'][n] <= self.tol_f:
n = n - 1
self.print_message('end')
break
if self.train_hist['p'][n] <= self.tol_p:
n = n - 1
self.print_message('end')
break
# compute the nth basis (including normalization)# ToDo: Also old Vx need to be substracted here!
Vx[notIndI, n] = self.kernel.eval(X[notIndI, :], X[indI_[n], :])[:, 0]\
- Vx_new_X_old_ctrs[notIndI, :] @ Vx_new_X_old_ctrs[indI_[n], :].transpose()\
- Vx[notIndI, :n+1] @ Vx[indI_[n], 0:n+1].transpose()
Vx[indI_[n], n] += self.reg_par
Vx[notIndI, n] = Vx[notIndI, n] / np.sqrt(p[indI_[n]])
if self.flag_val:
Vx_val[:, n] = self.kernel.eval(X_val, X[indI_[n], :])[:, 0]\
- Vx_val_new_X_old_ctrs[:, :] @ Vx_new_X_old_ctrs[indI_[n], :].transpose()\
- Vx_val[:, :n+1] @ Vx[indI_[n], 0:n+1].transpose()
Vx_val[:, n] = Vx_val[:, n] / np.sqrt(p[indI_[n]])
# update the change of basis
Cut_new_row = np.ones(N_ctrs_so_far + n + 1)
Cut_new_row[:N_ctrs_so_far + n] = \
-np.concatenate((Vx_new_X_old_ctrs[indI_[n], :], Vx[indI_[n], :n])) \
@ Cut_[:N_ctrs_so_far + n, :N_ctrs_so_far + n]
Cut_[N_ctrs_so_far + n, :N_ctrs_so_far + n + 1] = Cut_new_row / Vx[indI_[n], n]
# compute the nth coefficient
c[n] = y[indI_[n]] / np.sqrt(p[indI_[n]])
# update the power function
p[notIndI] = p[notIndI] - Vx[notIndI, n] ** 2
if self.flag_val:
p_val = p_val - Vx_val[:, n] ** 2
# update the residual
y[notIndI] = y[notIndI] - Vx[notIndI, n][:, None] * c[n]
if self.flag_val:
y_val = y_val - Vx_val[:, n][:, None] * c[n]
# remove the nth index from the dictionary
notIndI.pop(idx)
# Report some data every now and then
if n % self.n_report == 0:
self.print_message('track')
else:
self.print_message('end')
# Define coefficients and centers
self.c = np.concatenate((self.c, c[:n + 1]))
self.Cut_ = Cut_[:N_ctrs_so_far + n + 1, :N_ctrs_so_far + n + 1]
self.indI_ = indI_[:n + 1] # Mind: These are only the indices of the latest points
self.coef_ = self.Cut_.transpose() @ self.c
self.ctrs_ = np.concatenate((self.ctrs_, X[self.indI_, :]), axis=0)
return self
def predict(self, X):
# Check is fit has been called
# check_is_fitted(self, 'coef_') # ToDo: Remove this one!
# Validate the input
X = check_array(X)
# Compute prediction
if self.ctrs_.shape[0] > 0:
prediction = self.kernel.eval(X, self.ctrs_) @ self.coef_
else:
prediction = np.zeros((X.shape[0], 1))
return prediction
### TODO: replace with eval prod
def print_message(self, when):
if self.verbose and when == 'begin':
print('')
print('*' * 30 + ' [VKOGA] ' + '*' * 30)
print('Training model with')
print(' |_ kernel : %s' % self.kernel)
print(' |_ regularization par. : %2.2e' % self.reg_par)
print(' |_ restriction par. : %2.2e' % self.restr_par)
print('')
if self.verbose and when == 'end':
print('Training completed with')
print(' |_ selected points : %8d / %8d' % (self.train_hist['n'][-1], self.ctrs_.shape[0] + self.maxIter))
if self.flag_val:
print(' |_ train, val residual : %2.2e / %2.2e, %2.2e' %
(self.train_hist['f'][-1], self.tol_f, self.train_hist['f val'][-1]))
print(' |_ train, val power fun: %2.2e / %2.2e, %2.2e' %
(self.train_hist['p'][-1], self.tol_p, self.train_hist['p val'][-1]))
else:
print(' |_ train residual : %2.2e / %2.2e' % (self.train_hist['f'][-1], self.tol_f))
print(' |_ train power fun : %2.2e / %2.2e' % (self.train_hist['p'][-1], self.tol_p))
if self.verbose and when == 'track':
print('Training ongoing with')
print(' |_ selected points : %8d / %8d' % (self.train_hist['n'][-1], self.ctrs_.shape[0] + self.maxIter))
if self.flag_val:
print(' |_ train, val residual : %2.2e / %2.2e, %2.2e' %
(self.train_hist['f'][-1], self.tol_f, self.train_hist['f val'][-1]))
print(' |_ train, val power fun: %2.2e / %2.2e, %2.2e' %
(self.train_hist['p'][-1], self.tol_p, self.train_hist['p val'][-1]))
else:
print(' |_ train residual : %2.2e / %2.2e' % (self.train_hist['f'][-1], self.tol_f))
print(' |_ train power fun : %2.2e / %2.2e' % (self.train_hist['p'][-1], self.tol_p))
#%% Utilities to
import pickle
def save_object(obj, filename):
with open(filename, 'wb') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def load_object(filename):
with open(filename, 'rb') as input:
obj = pickle.load(input)
return obj