forked from muecke-math/Leibniz-Summer-School-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise-KRR.py
156 lines (106 loc) · 3.58 KB
/
exercise-KRR.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
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 21 19:25:24 2021
@author: Nicole
"""
############# Distributed Ridge Regression
############# simulated Data
############# regularized KRR
## for a theoretical description in the context of RKHS's see Sec. 4 of
## https://www.jmlr.org/papers/volume19/16-569/16-569.pdf
import numpy as np
import matplotlib.pyplot as plt
#from sklearn.model_selection import KFold
import math
## noise variance and standard deviation
sigma = 0.005**2
sd = np.sqrt(sigma)
## squared empirical L2- norm
def l2_norm(x):
"""Define norm."""
return np.sqrt(np.mean(x**2))
### definition regression function
## low smoothness
def f(x):
return 0.5*x*(1-x)
## high smoothness
#def f(x):
# return (1/(2*np.pi))*np.sin(2*np.pi*x)
### number training and test samples
n_train=500
n_test=int(n_train/2)
### define kernel function
def k(x,t):
return min(x,t)-x*t
## nb local nodes
alpha = np.array([0,0.1,0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
M = n_train**alpha
M = np.floor(M)
np.floor(n_train/M)
### regularization parameter
## low smoothness
Lam = np.array([0.01, 0.1, 0.5])
## high smoothness
#Lam = np.array([0.001, 0.01, 1])
## repitions of the experiment
## we build an average at the end, the larger L,
## the longer the computation but the smoother the curve!
L = 1
## Error
Error_test = np.zeros((len(M), len(Lam)))
###### begin big loop
for _ in range(L):
X_train = np.random.uniform(0, 1, n_train)
X_test = np.random.uniform(0, 1, n_test)
Y_train = f(X_train) + np.random.normal(0,sd,n_train)
Y_test = f(X_test) + np.random.normal(0,sd,n_test)
## kernel matrix training samples
K = np.zeros((n_train, n_train))
for i in range(n_train):
for j in range(i):
K[i,j] = k(X_train[i], X_train[j])
K[j,i] = K[i,j]
KK = np.zeros((n_train, n_test))
for i in range(n_train):
for j in range(n_test):
KK[i,j] = k(X_train[i], X_test[j])
## begin loop regularization parameter
## begin loop local machines
for jj in range(len(Lam)):
lam = Lam[jj]
for a in range(len(M)):
m = int(M[a])
## local estimator at test samples
f_loc = np.zeros(n_test)
for mm in range(m):
samples_loc = math.floor(n_train/m)
ind_machines = np.random.choice(n_train, samples_loc, replace = False)
#local kernel matrix
K_loc = K[ind_machines, ind_machines] / samples_loc
#local labels
Y_loc = Y_train[ind_machines]
#local identity matrix
D = np.diag(np.ones(samples_loc))
#local coefficients
coeff_loc = np.linalg.pinv(K_loc+lam*D) @ Y_loc
coeff_loc = coeff_loc / samples_loc
#local estimator at test samples
f_loc = f_loc + coeff_loc @ KK[ind_machines,:]
f_ave = f_loc / m
#test error
Error_test[a,jj] = Error_test[a,jj] + l2_norm(f_ave - f(X_test))**2
## begin loop regularization parameter
## begin loop local machines
####### end big loop
## averaging
Error = Error_test / L
Efficiency = Error_test[0]/Error_test
### plots
plt.plot(alpha, Error[:,0], "k-", label=f"lam = {Lam[0]}")
plt.plot(alpha, Error[:,1], "r-", label=f"lam = {Lam[1]}")
plt.plot(alpha, Error[:,2], "b-", label=f"lam = {Lam[2]}")
plt.ylim([0, 2])
plt.xlabel("nb machines")
plt.ylabel("Test Error")
plt.legend(loc="upper right")
plt.show()