-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrkhs_scaler.py
798 lines (691 loc) · 36.4 KB
/
rkhs_scaler.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# Copyright (c) Rui Miao 2021-2022
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import numpy as np
import scipy
import sklearn
from sklearn.base import BaseEstimator
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from sklearn.metrics.pairwise import pairwise_kernels
from sklearn.model_selection import KFold
from sklearn.model_selection import StratifiedKFold
from sklearn.kernel_approximation import Nystroem, RBFSampler
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import RobustScaler as Scaler
#from sklearn.preprocessing import StandardScaler as Scaler
def _check_auto(param):
return (isinstance(param, str) and (param == 'auto'))
class _BaseRKHSIV:
def __init__(self, *args, **kwargs):
return
def _get_delta(self, n):
'''
delta -> Critical radius
'''
delta_scale = 5 if _check_auto(self.delta_scale) else self.delta_scale
delta_exp = .4 if _check_auto(self.delta_exp) else self.delta_exp
return delta_scale / (n**(delta_exp))
def _get_alpha_scale(self):
return 60 if _check_auto(self.alpha_scale) else self.alpha_scale
def _get_alpha_scales(self):
return ([c for c in np.geomspace(0.1, 1e5, self.n_alphas)]
if _check_auto(self.alpha_scales) else self.alpha_scales)
def _get_alpha(self, delta, alpha_scale):
return alpha_scale * (delta**4)
def _get_kernel(self, X, Y=None):
if callable(self.kernel):
params = self.kernel_params or {}
else:
params = {"gamma": self.gamma,
"degree": self.degree,
"coef0": self.coef0}
return pairwise_kernels(X, Y, metric=self.kernel,
filter_params=True, **params)
def _get_gamma_f(self, condition):
if _check_auto(self.gamma_f):
params = {"squared": True}
K_condition_euclidean = sklearn.metrics.pairwise_distances(X = condition, metric='euclidean', n_jobs=-1, **params)
# gamma_f = 1./(condition.shape[1] * np.median(K_condition_euclidean[np.tril_indices(condition.shape[0],-1)]))
gamma_f = 1./(np.median(K_condition_euclidean[np.tril_indices(condition.shape[0],-1)]))
return gamma_f
else:
return self.gamma_f
def _get_kernel_f(self, X, Y=None, gamma_f=0.1):
params = {"gamma": gamma_f}
return pairwise_kernels(X, Y, metric='rbf', filter_params=True, **params)
def _get_kernel_h(self, X, Y=None, gamma_h=0.01):
params = {"gamma": gamma_h}
return pairwise_kernels(X, Y, metric='rbf', filter_params=True, **params)
class RKHSIV(BaseEstimator, _BaseRKHSIV):
def __init__(self, gamma_h=0.1, gamma_f='auto',
delta_scale='auto', delta_exp='auto', alpha_scale='auto'):
"""
Parameters:
gamma_h : the gamma parameter for the rbf kernel of h
gamma_f : the gamma parameter for the rbf kernel of f
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scale : the scale of the regularization; alpha = alpha_scale * (delta**4)
"""
self.gamma_f = gamma_f
self.gamma_h = gamma_h
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp
self.alpha_scale = alpha_scale # regularization strength from Theorem 5
def fit(self, X, y, condition):
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and get gamma_f -> Kf -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition=condition)
self.gamma_f = gamma_f
Kf = self._get_kernel_f(condition, gamma_f=self.gamma_f)
RootKf = scipy.linalg.sqrtm(Kf).astype(float)
# Standardize X and get Kh
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
self.X = X.copy()
Kh = self._get_kernel_h(X, gamma_h=self.gamma_h)
# delta & alpha
n = X.shape[0] # number of samples
delta = self._get_delta(n)
alpha = self._get_alpha(delta, self._get_alpha_scale())
# M
M = RootKf @ np.linalg.inv(
Kf / (2 * n * delta**2) + np.eye(n) / 2) @ RootKf
#self.a = np.linalg.pinv(Kh @ M @ Kh + alpha * Kh) @ Kh @ M @ y
self.a = np.linalg.lstsq(Kh @ M @ Kh + alpha * Kh, Kh @ M @ y, rcond=None)[0]
return self
def predict(self, X):
X = self.transX.transform(X)
return self._get_kernel_h(X, Y=self.X, gamma_h=self.gamma_h) @ self.a
def score(self, X, y, M):
n = X.shape[0]
#delta = self._get_delta(n)
#Kf = self._get_kernel_f(Z, gamma_f=self.gamma_f)
#RootKf = scipy.linalg.sqrtm(Kf).astype(float)
#M = RootKf @ np.linalg.inv(
# Kf / (2 * n * delta**2) + np.eye(n) / 2) @ RootKf
y_pred = self.predict(X)
return ((y - y_pred).T @ M @ (y - y_pred)).reshape(-1)[0] / n**2
class RKHSIVCV(RKHSIV):
def __init__(self, gamma_f='auto', gamma_hs='auto', n_gamma_hs=20,
delta_scale='auto', delta_exp='auto', alpha_scales='auto', n_alphas=30, cv=6):
"""
Parameters:
gamma_f : the gamma parameter for the kernel of f
gamma_hs : the list of gamma parameters for kernel of h
n_gamma_hs : how many gamma_hs to try
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scales : a list of scale of the regularization to choose from; alpha = alpha_scale * (delta**4)
n_alphas : how many alpha_scales to try
cv : how many folds to use in cross-validation for alpha_scale, gamma_h
"""
self.gamma_f = gamma_f
self.gamma_hs = gamma_hs
self.n_gamma_hs=n_gamma_hs
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp # worst-case critical value of RKHS spaces
self.alpha_scales = alpha_scales # regularization strength from Theorem 5
self.n_alphas = n_alphas
self.cv = cv
def _get_gamma_hs(self,X):
if _check_auto(self.gamma_hs):
params = {"squared": True}
K_X_euclidean = sklearn.metrics.pairwise_distances(X = X, metric='euclidean', **params)
#return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)/X.shape[1]
return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)
else:
return self.gamma_hs
def fit(self, X, y, condition):
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and get gamma_f -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition = condition)
Kf = self._get_kernel_f(condition, gamma_f=gamma_f)
RootKf = scipy.linalg.sqrtm(Kf).astype(float)
# Standardize X and get gamma_hs
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
self.X = X.copy()
gamma_hs = self._get_gamma_hs(X)
#Khs = [self._get_kernel_h(X, gamma_h = gammah) for gammah in gamma_hs]
# delta & alpha
n = X.shape[0]
n_train = n * (self.cv - 1) / self.cv
delta_train = self._get_delta(n_train)
n_test = n / self.cv
delta_test = self._get_delta(n_test)
alpha_scales = self._get_alpha_scales()
# get best (alpha, gamma_h) START
scores = []
for it1, (train, test) in enumerate(KFold(n_splits=self.cv).split(X)):
# Standardize X_train
transX = Scaler()
X_train = transX.fit_transform(X[train])
X_test = transX.transform(X[test])
# Standardize condition_train and get Kf_train, RootKf_train, M_train
condition_train = Scaler().fit_transform(condition[train])
Kf_train = self._get_kernel_f(X=condition_train, gamma_f=self._get_gamma_f(condition=condition_train))
RootKf_train = scipy.linalg.sqrtm(Kf_train).astype(float)
M_train = RootKf_train @ np.linalg.inv(
Kf_train / (2 * n_train * (delta_train**2)) + np.eye(len(train)) / 2) @ RootKf_train
# Use M_test based on precomputed RootKf to make sure evaluations are the same
M_test = RootKf[np.ix_(test, test)] @ np.linalg.inv(
Kf[np.ix_(test, test)] / (2 * n_test * (delta_test**2)) + np.eye(len(test)) / 2) @ RootKf[np.ix_(test, test)]
scores.append([])
for it2, gamma_h in enumerate(gamma_hs):
Kh_train = self._get_kernel_h(X=X_train, gamma_h=gamma_h)
KMK_train = Kh_train @ M_train @ Kh_train
B_train = Kh_train @ M_train @ y[train]
scores[it1].append([])
for alpha_scale in alpha_scales:
alpha = self._get_alpha(delta_train, alpha_scale)
#a = np.linalg.pinv(KMK_train + alpha * Kh_train) @ B_train
a = np.linalg.lstsq(KMK_train + alpha * Kh_train, B_train, rcond=None)[0]
res = y[test] - self._get_kernel_h(X=X_test, Y=X_train, gamma_h=gamma_h) @ a
scores[it1][it2].append((res.T @ M_test @ res).reshape(-1)[0] / (res.shape[0]**2))
avg_scores = np.mean(np.array(scores), axis=0)
best_ind = np.unravel_index(np.argmin(avg_scores), avg_scores.shape)
self.gamma_h = gamma_hs[best_ind[0]]
self.best_alpha_scale = alpha_scales[best_ind[1]]
delta = self._get_delta(n)
self.best_alpha = self._get_alpha(delta, self.best_alpha_scale)
# M
M = RootKf @ np.linalg.inv(
Kf / (2 * n * delta**2) + np.eye(n) / 2) @ RootKf
# Kh
Kh = self._get_kernel_h(X, gamma_h=self.gamma_h)
# self.a = np.linalg.pinv(
# Kh @ M @ Kh + self.best_alpha * Kh) @ Kh @ M @ y
self.a = np.linalg.lstsq(
Kh @ M @ Kh + self.best_alpha * Kh, Kh @ M @ y, rcond=None)[0]
return self
class ApproxRKHSIV(_BaseRKHSIV):
def __init__(self, kernel_approx='nystrom', n_components=25,
gamma_f='auto', gamma_h=0.1,
delta_scale='auto', delta_exp='auto', alpha_scale='auto'):
"""
Parameters:
kernel_approx : what approximator to use; either 'nystrom' or 'rbfsampler' (for kitchen sinks)
n_components : how many approximation components to use
# kernel : a pairwise kernel function or a string; similar interface with KernelRidge in sklearn
gamma_h : the gamma parameter for the kernel of h
gamma_f : the gamma parameter for the kernel of f
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scale : the scale of the regularization; alpha = alpha_scale * (delta**4)
"""
self.kernel_approx = kernel_approx
self.n_components = n_components
self.gamma_f = gamma_f
self.gamma_h = gamma_h
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp
self.alpha_scale = alpha_scale # regularization strength from Theorem 5
def _get_new_approx_instance(self, gamma):
if self.kernel_approx == 'rbfsampler':
return RBFSampler(gamma=gamma, n_components=self.n_components, random_state=1)
elif self.kernel_approx == 'nystrom':
return Nystroem(kernel='rbf', gamma=gamma, random_state=1, n_components=self.n_components)
else:
raise AttributeError("Invalid kernel approximator")
def fit(self, X, y, condition):
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and get gamma_f -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition=condition)
self.gamma_f = gamma_f
self.featCond = self._get_new_approx_instance(gamma=self.gamma_f)
RootKf = self.featCond.fit_transform(condition)
# Standardize X and get gamma_hs -> RootKhs
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
self.featX = self._get_new_approx_instance(gamma=self.gamma_h)
RootKh = self.featX.fit_transform(X)
# delta & alpha
n = X.shape[0]
delta = self._get_delta(n)
alpha = self._get_alpha(delta, self._get_alpha_scale())
Q = np.linalg.pinv(RootKf.T @ RootKf /
(2 * n * delta**2) + np.eye(self.n_components) / 2)
A = RootKh.T @ RootKf
W = (A @ Q @ A.T + alpha * np.eye(self.n_components))
B = A @ Q @ RootKf.T @ y
# self.a = np.linalg.pinv(W) @ B
self.a = np.linalg.lstsq(W, B, rcond=None)[0]
self.fitted_delta = delta
return self
def predict(self, X):
X = self.transX.transform(X)
return self.featX.transform(X) @ self.a
class ApproxRKHSIVCV(ApproxRKHSIV):
def __init__(self, kernel_approx='nystrom', n_components=25,
gamma_f='auto', gamma_hs = 'auto', n_gamma_hs=10,
delta_scale='auto', delta_exp='auto', alpha_scales='auto', n_alphas=30, cv=6):
"""
Parameters:
kernel_approx : what approximator to use; either 'nystrom' or 'rbfsampler' (for kitchen sinks)
n_components : how many nystrom components to use
gamma_f : the gamma parameter for the kernel of f
gamma_hs : the list of gamma parameters for kernel of h
n_gamma_hs : how many gamma_hs to try
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scales : a list of scale of the regularization to choose from; alpha = alpha_scale * (delta**4)
n_alphas : how mny alpha_scales to try
cv : how many folds to use in cross-validation for alpha_scale
"""
self.kernel_approx = kernel_approx
self.n_components = n_components
self.gamma_f = gamma_f
self.gamma_hs = gamma_hs
self.n_gamma_hs=n_gamma_hs
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp # worst-case critical value of RKHS spaces
self.alpha_scales = alpha_scales # regularization strength from Theorem 5
self.n_alphas = n_alphas
self.cv = cv
def _get_gamma_hs(self,X):
if _check_auto(self.gamma_hs):
params = {"squared": True}
K_X_euclidean = sklearn.metrics.pairwise_distances(X = X, metric='euclidean', **params)
#return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)/X.shape[1]
return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)
else:
return self.gamma_hs
def fit(self, X, y, condition):
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and get gamma_f -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition = condition)
self.gamma_f = gamma_f
self.featCond = self._get_new_approx_instance(gamma=gamma_f)
RootKf = self.featCond.fit_transform(condition)
# Standardize X and get gamma_hs -> RootKhs
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
gamma_hs = self._get_gamma_hs(X)
RootKhs = [self._get_new_approx_instance(gamma=gammah).fit_transform(X) for gammah in gamma_hs]
# delta & alpha
n = X.shape[0]
alpha_scales = self._get_alpha_scales()
n_train = n * (self.cv - 1) / self.cv
n_test = n / self.cv
delta_train = self._get_delta(n_train)
delta_test = self._get_delta(n_test)
scores = []
for it1, (train, test) in enumerate(KFold(n_splits=self.cv).split(X)):
RootKf_train, RootKf_test = RootKf[train], RootKf[test]
Q_train = np.linalg.pinv(
RootKf_train.T @ RootKf_train / (2 * n_train * (delta_train**2)) + np.eye(self.n_components) / 2)
Q_test = np.linalg.pinv(
RootKf_test.T @ RootKf_test / (2 * n_test * (delta_test**2)) + np.eye(self.n_components) / 2)
scores.append([])
for it2, RootKh in enumerate(RootKhs):
RootKh_train, RootKh_test = RootKh[train], RootKh[test]
A_train = RootKh_train.T @ RootKf_train
AQA_train = A_train @ Q_train @ A_train.T
B_train = A_train @ Q_train @ RootKf_train.T @ y[train]
scores[it1].append([])
for alpha_scale in alpha_scales:
alpha = self._get_alpha(delta_train, alpha_scale)
# a = np.linalg.pinv(AQA_train + alpha *
# np.eye(self.n_components)) @ B_train
a = np.linalg.lstsq(AQA_train + alpha *
np.eye(self.n_components), B_train, rcond=None)[0]
res = RootKf_test.T @ (y[test] - RootKh_test @ a)
scores[it1][it2].append((res.T @ Q_test @ res).reshape(-1)[0] / (len(test)**2))
avg_scores = np.mean(np.array(scores), axis=0)
best_ind = np.unravel_index(np.argmin(avg_scores), avg_scores.shape)
self.gamma_h = gamma_hs[best_ind[0]]
self.featX = self._get_new_approx_instance(gamma=self.gamma_h)
RootKh = self.featX.fit_transform(X)
self.best_alpha_scale = alpha_scales[best_ind[1]]
delta = self._get_delta(n)
self.best_alpha = self._get_alpha(delta, self.best_alpha_scale)
Q = np.linalg.pinv(RootKf.T @ RootKf /
(2 * n * delta**2) + np.eye(self.n_components) / 2)
A = RootKh.T @ RootKf
W = (A @ Q @ A.T + self.best_alpha * np.eye(self.n_components))
B = A @ Q @ RootKf.T @ y
# self.a = np.linalg.pinv(W) @ B
self.a = np.linalg.lstsq(W, B, rcond=None)[0]
self.fitted_delta = delta
return self
class RKHSIV_q(_BaseRKHSIV):
def __init__(self, gamma_h=0.1, gamma_f='auto',
delta_scale='auto', delta_exp='auto', alpha_scale='auto'):
"""
Parameters:
gamma_h : the gamma parameter for the rbf kernel of h
gamma_f : the gamma parameter for the rbf kernel of f
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scale : the scale of the regularization; alpha = alpha_scale * (delta**4)
"""
self.gamma_f = gamma_f
self.gamma_h = gamma_h
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp
self.alpha_scale = alpha_scale # regularization strength from Theorem 5
def fit(self, X, y, condition, index):
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and get gamma_f -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition=condition)
self.gamma_f = gamma_f
Kf = self._get_kernel_f(condition, gamma_f=gamma_f)
RootKf = scipy.linalg.sqrtm(Kf).astype(float)
# Standardize X and get Kh, Kh0
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
self.X = X.copy()
Kh = self._get_kernel_h(X, gamma_h=self.gamma_h)
Kh0 = np.zeros_like(Kh)
Kh0[index,:] = Kh[index,:]
# delta & alpha
n = X.shape[0] # number of samples
#delta = self._get_delta(n)
delta = self._get_delta(np.sum(index)) # only sum(index) of effective data
alpha = self._get_alpha(delta, self._get_alpha_scale())
M = RootKf @ np.linalg.inv(
Kf / (2 * n * delta**2) + np.eye(n) / 2) @ RootKf
# self.a = np.linalg.pinv(Kh0.T @ M @ Kh0 + alpha * Kh) @ Kh0 @ M @ y
self.a = np.linalg.lstsq(Kh0.T @ M @ Kh0 + alpha * Kh, Kh0 @ M @ y, rcond=None)[0]
return self
def predict(self, X):
X = self.transX.transform(X)
return self._get_kernel_h(X, Y=self.X, gamma_h=self.gamma_h) @ self.a
def score(self, X, y, M, index):
n = X.shape[0]
#Kf = self._get_kernel_f(Z, gamma_f=self.gamma_f)
#RootKf = scipy.linalg.sqrtm(Kf).astype(float)
#M = RootKf @ np.linalg.inv(
# Kf / (2 * n * delta**2) + np.eye(n) / 2) @ RootKf
y_pred = np.zeros_like(y)
y_pred[index] = self.predict(X[index,:])
return ((y - y_pred).T @ M @ (y - y_pred)).reshape(-1)[0] / n**2
class RKHSIVCV_q(RKHSIV_q):
def __init__(self, gamma_f='auto', gamma_hs='auto', n_gamma_hs=25,
delta_scale='auto', delta_exp='auto', alpha_scales='auto', n_alphas=30, cv=6):
"""
Parameters:
gamma_f : the gamma parameter for the kernel of f
gamma_hs : the list of gamma parameters for kernel of h
n_gamma_hs : how many gamma_hs to try
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scales : a list of scale of the regularization to choose from; alpha = alpha_scale * (delta**4)
n_alphas : how many alpha_scales to try
cv : how many folds to use in cross-validation for alpha_scale, gamma_h
"""
self.gamma_f = gamma_f
self.gamma_hs = gamma_hs
self.n_gamma_hs=n_gamma_hs
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp # worst-case critical value of RKHS spaces
self.alpha_scales = alpha_scales # regularization strength from Theorem 5
self.n_alphas = n_alphas
self.cv = cv
def _get_gamma_hs(self,X):
if _check_auto(self.gamma_hs):
params = {"squared": True}
K_X_euclidean = sklearn.metrics.pairwise_distances(X = X, metric='euclidean', **params)
# return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)/X.shape[1]
return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)
else:
return self.gamma_hs
def fit(self, X, y, condition, index):
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and et gamma_f -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition = condition)
Kf = self._get_kernel_f(condition, gamma_f=gamma_f)
RootKf = scipy.linalg.sqrtm(Kf).astype(float)
# Standardize X and get gamma_hs
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
self.X = X.copy()
gamma_hs = self._get_gamma_hs(X)
#Khs = []
#for gammah in gamma_hs:
# Kh = self._get_kernel_h(X, gamma_h = gammah)
# Kh0 = np.zeros_like(Kh)
# Kh0[index,:] = Kh[index,:]
# Khs.append((Kh, Kh0))
# delta & alpha
n = X.shape[0]
alpha_scales = self._get_alpha_scales()
n_train = n * (self.cv - 1) / self.cv
n_test = n / self.cv
# get best (alpha, gamma_h) START
scores = []
for it1, (train, test) in enumerate(StratifiedKFold(n_splits=self.cv).split(X, index)):
# Standardize X_train
transX = Scaler()
X_train = transX.fit_transform(X[train])
X_test = transX.transform(X[test])
# Standardize condition_train and get Kf_train, RootKf_train, M_train
condition_train = Scaler().fit_transform(condition[train])
Kf_train = self._get_kernel_f(X=condition_train, gamma_f=self._get_gamma_f(condition=condition_train))
RootKf_train = scipy.linalg.sqrtm(Kf_train).astype(float)
delta_train = self._get_delta(np.sum(index[train]))
M_train = RootKf_train @ np.linalg.inv(
Kf_train / (2 * n_train * (delta_train**2)) + np.eye(len(train)) / 2) @ RootKf_train
# Use M_test based on precomputed RootKf to make sure evaluations are the same
delta_test = self._get_delta(np.sum(index[test]))
M_test = RootKf[np.ix_(test, test)] @ np.linalg.inv(
Kf[np.ix_(test, test)] / (2 * n_test * (delta_test**2)) + np.eye(len(test)) / 2) @ RootKf[np.ix_(test, test)]
scores.append([])
for it2, gamma_h in enumerate(gamma_hs):
Kh_train = self._get_kernel_h(X_train, gamma_h = gamma_h)
Kh0_train = np.zeros_like(Kh_train)
Kh0_train[index[train],:] = Kh_train[index[train],:]
KMK_train = Kh0_train @ M_train @ Kh0_train
B_train = Kh0_train @ M_train @ y[train]
scores[it1].append([])
for alpha_scale in alpha_scales:
alpha = self._get_alpha(delta_train, alpha_scale)
# a = np.linalg.pinv(KMK_train + alpha * Kh_train) @ B_train
a = np.linalg.lstsq(KMK_train + alpha * Kh_train, B_train, rcond=None)[0]
res = y[test]
res[index[test]] = y[index[test]] - self._get_kernel_h(X=X_test[index[test],:], Y=X_train, gamma_h=gamma_h) @ a
scores[it1][it2].append((res.T @ M_test @ res).reshape(-1)[0] / (res.shape[0]**2))
#self.alpha_scales = alpha_scales
avg_scores = np.mean(np.array(scores), axis=0)
best_ind = np.unravel_index(np.argmin(avg_scores), avg_scores.shape)
self.gamma_h = gamma_hs[best_ind[0]]
Kh = self._get_kernel_h(X, gamma_h=self.gamma_h)
Kh0 = np.zeros_like(Kh)
Kh0[index,:] = Kh[index,:]
self.best_alpha_scale = alpha_scales[best_ind[1]]
delta = self._get_delta(np.sum(index))
self.best_alpha = self._get_alpha(delta, self.best_alpha_scale)
M = RootKf @ np.linalg.inv(
Kf / (2 * n * delta**2) + np.eye(n) / 2) @ RootKf
# self.a = np.linalg.pinv(
# Kh0 @ M @ Kh0 + self.best_alpha * Kh) @ Kh0 @ M @ y
self.a = np.linalg.lstsq(
Kh0 @ M @ Kh0 + self.best_alpha * Kh, Kh0 @ M @ y, rcond=None)[0]
return self
class ApproxRKHSIV_q(_BaseRKHSIV):
def __init__(self, kernel_approx='nystrom', n_components=20,
gamma_f='auto', gamma_h=0.1,
delta_scale='auto', delta_exp='auto', alpha_scale='auto'):
"""
Parameters:
kernel_approx : what approximator to use; either 'nystrom' or 'rbfsampler' (for kitchen sinks)
n_components : how many approximation components to use
# kernel : a pairwise kernel function or a string; similar interface with KernelRidge in sklearn
gamma_h : the gamma parameter for the kernel of h
gamma_f : the gamma parameter for the kernel of f
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scale : the scale of the regularization; alpha = alpha_scale * (delta**4)
"""
self.kernel_approx = kernel_approx
self.n_components = n_components
self.gamma_f = gamma_f
self.gamma_h = gamma_h
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp
self.alpha_scale = alpha_scale # regularization strength from Theorem 5
def _get_new_approx_instance(self, gamma):
if self.kernel_approx == 'rbfsampler':
return RBFSampler(gamma=gamma, n_components=self.n_components, random_state=1)
elif self.kernel_approx == 'nystrom':
return Nystroem(kernel='rbf', gamma=gamma, random_state=1, n_components=self.n_components)
else:
raise AttributeError("Invalid kernel approximator")
def fit(self, X, y, condition, index):
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and get gamma_f -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition=condition)
self.gamma_f = gamma_f
self.featCond = self._get_new_approx_instance(gamma=self.gamma_f)
RootKf = self.featCond.fit_transform(condition)
# Standardize X and get RootKh
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
self.featX = self._get_new_approx_instance(gamma=self.gamma_h)
RootKh = self.featX.fit_transform(X)
RootKh[np.logical_not(index),:] = 0
# delta & alpha
n = X.shape[0]
#delta = self._get_delta(n)
delta = self._get_delta(np.sum(index)) # only sum(index) of effective data
alpha = self._get_alpha(delta, self._get_alpha_scale())
Q = np.linalg.pinv(RootKf.T @ RootKf /
(2 * n * delta**2) + np.eye(self.n_components) / 2)
A = RootKh.T @ RootKf
W = (A @ Q @ A.T + alpha * np.eye(self.n_components))
B = A @ Q @ RootKf.T @ y
# self.a = np.linalg.pinv(W) @ B
self.a = np.linalg.lstsq(W, B, rcond=None)[0]
self.fitted_delta = delta
return self
def predict(self, X):
X = self.transX.transform(X)
return self.featX.transform(X) @ self.a
class ApproxRKHSIVCV_q(ApproxRKHSIV_q):
def __init__(self, kernel_approx='nystrom', n_components=25,
gamma_f='auto', gamma_hs = 'auto', n_gamma_hs=10,
delta_scale='auto', delta_exp='auto', alpha_scales='auto', n_alphas=30, cv=6):
"""
Parameters:
kernel_approx : what approximator to use; either 'nystrom' or 'rbfsampler' (for kitchen sinks)
n_components : how many nystrom components to use
gamma_f : the gamma parameter for the kernel of f
gamma_hs : the list of gamma parameters for kernel of h
n_gamma_hs : how many gamma_hs to try
delta_scale : the scale of the critical radius; delta_n = delta_scal / n**(delta_exp)
delta_exp : the exponent of the cirical radius; delta_n = delta_scal / n**(delta_exp)
alpha_scales : a list of scale of the regularization to choose from; alpha = alpha_scale * (delta**4)
n_alphas : how mny alpha_scales to try
cv : how many folds to use in cross-validation for alpha_scale
"""
self.kernel_approx = kernel_approx
self.n_components = n_components
self.gamma_f = gamma_f
self.gamma_hs = gamma_hs
self.n_gamma_hs=n_gamma_hs
self.delta_scale = delta_scale # worst-case critical value of RKHS spaces
self.delta_exp = delta_exp # worst-case critical value of RKHS spaces
self.alpha_scales = alpha_scales # regularization strength from Theorem 5
self.n_alphas = n_alphas
self.cv = cv
def _get_gamma_hs(self,X):
if _check_auto(self.gamma_hs):
params = {"squared": True}
K_X_euclidean = sklearn.metrics.pairwise_distances(X = X, metric='euclidean', **params)
# return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)/X.shape[1]
return 1./np.quantile(K_X_euclidean[np.tril_indices(X.shape[0],-1)], np.array(range(1, self.n_gamma_hs))/self.n_gamma_hs)
else:
return self.gamma_hs
def fit(self, X, y, condition, index):
# index is a np vector with bool value and the same length of Y, indicate which Y's should be estimate by h
X, y = check_X_y(X, y, accept_sparse=True)
condition, y = check_X_y(condition, y, accept_sparse=True)
# Standardize condition and get gamma_f -> RootKf
condition = Scaler().fit_transform(condition)
gamma_f = self._get_gamma_f(condition = condition)
self.gamma_f = gamma_f
self.featCond = self._get_new_approx_instance(gamma=gamma_f)
RootKf = self.featCond.fit_transform(condition)
# Standardize X and get gamma_hs -> RootKhs
self.transX = Scaler()
self.transX.fit(X)
X = self.transX.transform(X)
gamma_hs = self._get_gamma_hs(X)
RootKhs=[]
for gammah in gamma_hs:
RootKh = self._get_new_approx_instance(gamma=gammah).fit_transform(X)
RootKh[np.logical_not(index),:] = 0
RootKhs.append(RootKh)
# delta & alpha
n = X.shape[0]
alpha_scales = self._get_alpha_scales()
n_train = n * (self.cv - 1) / self.cv
n_test = n / self.cv
#delta_train = self._get_delta(n_train)
#delta_test = self._get_delta(n_test)
#delta = self._get_delta(n)
scores = []
for it1, (train, test) in enumerate(StratifiedKFold(n_splits=self.cv).split(X,index)):
delta_test = self._get_delta(np.sum(index[test]))
delta_train = self._get_delta(np.sum(index[train]))
RootKf_train, RootKf_test = RootKf[train], RootKf[test]
Q_train = np.linalg.pinv(
RootKf_train.T @ RootKf_train / (2 * n_train * (delta_train**2)) + np.eye(self.n_components) / 2)
Q_test = np.linalg.pinv(
RootKf_test.T @ RootKf_test / (2 * n_test * (delta_test**2)) + np.eye(self.n_components) / 2)
scores.append([])
for it2, RootKh in enumerate(RootKhs):
RootKh_train, RootKh_test = RootKh[train], RootKh[test]
A_train = RootKh_train.T @ RootKf_train
AQA_train = A_train @ Q_train @ A_train.T
B_train = A_train @ Q_train @ RootKf_train.T @ y[train]
scores[it1].append([])
for alpha_scale in alpha_scales:
alpha = self._get_alpha(delta_train, alpha_scale)
# a = np.linalg.pinv(AQA_train + alpha *
# np.eye(self.n_components)) @ B_train
a = np.linalg.lstsq(AQA_train + alpha *
np.eye(self.n_components), B_train, rcond=None)[0]
res = RootKf_test.T @ (y[test] - RootKh_test @ a)
scores[it1][it2].append((res.T @ Q_test @ res).reshape(-1)[0] / (len(test)**2))
#self.alpha_scales = alpha_scales
avg_scores = np.mean(np.array(scores), axis=0)
best_ind = np.unravel_index(np.argmin(avg_scores), avg_scores.shape)
self.gamma_h = gamma_hs[best_ind[0]]
self.featX = self._get_new_approx_instance(gamma=self.gamma_h)
RootKh = self.featX.fit_transform(X)
RootKh[np.logical_not(index),:] = 0
self.best_alpha_scale = alpha_scales[best_ind[1]]
delta = self._get_delta(np.sum(index))
self.best_alpha = self._get_alpha(delta, self.best_alpha_scale)
Q = np.linalg.pinv(RootKf.T @ RootKf /
(2 * n * delta**2) + np.eye(self.n_components) / 2)
A = RootKh.T @ RootKf
W = (A @ Q @ A.T + self.best_alpha * np.eye(self.n_components))
B = A @ Q @ RootKf.T @ y
# self.a = np.linalg.pinv(W) @ B
self.a = np.linalg.lstsq(W, B, rcond=None)[0]
self.fitted_delta = delta
return self