forked from zhengying-liu/Structured-Data-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
okl_fct.py
230 lines (194 loc) · 6.56 KB
/
okl_fct.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
import scipy.io
import numpy as np
from numpy import linalg as LA
import time
##########################################################################################################################
def okl(K,Y,lam_list):
"""
OKL with block coordinate descent
model = okl(K,Y,lam_list)
-------------------------------------------
Inputs:
K: input kernel matrix: (l x l) matrix,
Y: training outputs: (l x m) matrix.
lam_list: list of regularization parameters.
--------------------------------------------
Output:
model : output model containing the following information:
L : output kernel
C : coefficient matrix
lambda : regularization parameter
J : value of the objective functional
time : execution time
"""
#constants
l = K.shape[0]
m = Y.shape[1]
N = len(lam_list)
MAX_ITER = 1000
TOL = 0.001
delta = TOL * LA.norm(Y)
# initialization
J = np.zeros((MAX_ITER,1))
L = np.eye(m)
C = np.zeros((l,m))
# eigendecomposition of the input kernel matrix
DX, UX = LA.eigh(K)
'''
For Symetric Real matrix :
LA.eig -> complex space (due to computation issue)
LA.eigh -> real space
'''
DX_ = DX.reshape((len(DX),1))
dx = abs(DX_)
'''
The eigenvalues may be negative -> abs
'''
DX = np.diag(DX)
Ytilde = np.dot(UX.T,Y)
# MAIN loop
model={}
for k in range(N):
start_time = time.time()
lam = lam_list[k]
print("lambda = "+str(lam))
nit = 0
res = LA.norm(Y)
while(res > delta):
# Sub-problem w.r.t. C.
# Solve the Sylvester equation KCL+lambda*C = Y using eigendecomposition of K and L.
DY, UY = LA.eigh(L)
DY_ = DY.reshape((len(DY),1))
dy = abs(DY_)
DY = np.diag(DY)
Q = np.dot(Ytilde,UY)
V = Q / (np.dot(dx,dy.T)+lam) #element wise division
C = np.dot(np.dot(UX,V),UY.T)
# Sub-problem w.r.t. L
F = np.dot(V,UY.T)
E = np.dot(DX,F)
R = np.dot(E.T,E)
DE, UE = LA.eigh(R)
DE_ = DE.reshape((len(DE),1))
dep = abs(DE_)+lam
DE = np.diag(DE)
Lp = L
temp = np.dot(R,L) + np.dot(L.T,R.T) + lam*np.dot(E.T,F)
P = np.dot(np.dot(UE.T,temp),UE)
temp = np.dot(dep,np.ones((1,m))) + np.dot(np.ones((m,1)),dep.T)
L =np.dot(np.dot(UE, P/temp), UE.T)
# Compute the value of the objective functional
temp = F / 4 - Ytilde / (2*lam)
J[nit] = LA.norm(Y)**2 / (2*lam) + np.trace(np.dot(np.dot(temp.T,E),L))
#Compute the variation of L
res = LA.norm(L-Lp)
#Check whether the maximum number of iterations has been reached
if nit >= MAX_ITER:
print('Reached maximum number of iterations')
break
nit += 1
modelk={}
modelk['L']=L
modelk['C']=C
modelk['nit']=nit
modelk['lambda']=lam
modelk['J']=J[:nit]
modelk['time']=time.time() - start_time
model[k]=modelk
return model
##########################################################################################################################
def lrokl(K,Y,lam_list,p):
"""
Low rank OKL with block coordinate descent
model = lrokl(K,Y,lam_list,p)
-------------------------------------------
Inputs:
K: input kernel matrix: (l x l) matrix,
Y: training outputs: (l x m) matrix.
lam_list: list of regularization parameters.
p : rank parameter
--------------------------------------------
Output:
model : output model containing the following information:
B : linear operators
A : coefficient matrix
lambda : regularization parameter
J : value of the objective functional
time : execution time
"""
#constants
l = K.shape[0]
m = Y.shape[1]
N = len(lam_list)
MAX_ITER = 1000
TOL = 0.001
delta = TOL * LA.norm(Y)
# initialization
J = np.zeros((MAX_ITER,1))
B = np.eye(m,p)
# eigendecomposition of the input kernel matrix
DX, UX = LA.eigh(K)
'''
For Symetric Real matrix :
LA.eig -> complex space (due to computation issue)
LA.eigh -> real space
'''
DX_ = DX.reshape((len(DX),1))
dx = abs(DX_)
'''
The eigenvalues may be negative -> abs
'''
DX = np.diag(DX)
Ytilde = np.dot(UX.T,Y)
# MAIN loop
model={}
for k in range(N):
start_time = time.time()
lam = lam_list[k]
#print("lambda = "+str(lam))
nit = 0
res = LA.norm(Y)
while(res > delta):
# Sub-problem w.r.t. A.
# Solve the Sylvester equation KAB'B+lambda*A = YB
# via eigendecomposition of K and B'B
DY, UY = LA.eigh(np.dot(B.T,B))
DY_ = DY.reshape((len(DY),1))
dy = abs(DY_)
DY = np.diag(DY)
Q = np.dot(np.dot(Ytilde,B),UY)
V = Q / (np.dot(dx,dy.T)+lam) #element wise division
# Sub-problem w.r.t. B
Bp = B
E = np.dot(DX,V)
temp = LA.inv(np.dot(E.T,E)+lam*np.eye(p))
P = np.dot(np.dot(Ytilde.T,E),temp)
B = np.dot(P,UY.T)
# Compute the value of the objective functional
if l > m:
temp =np.dot((Ytilde - np.dot(E,P.T)).T,Ytilde)/lam
else:
temp =(np.dot(Ytilde,(Ytilde - np.dot(E,P.T)).T))/lam
J[nit] = (np.trace(temp) + np.trace(np.dot(V.T,E)))/2
# Compute the increment of B
res = LA.norm(B-Bp)
#Check whether the maximum number of iterations has been reached
if nit >= MAX_ITER:
print('Reached maximum number of iterations')
break
nit += 1
A = np.dot(np.dot(UX,V),UY.T)
modelk={}
modelk['A']=A
modelk['B']=B
modelk['nit']=nit
modelk['lambda']=lam
modelk['J']=J[:nit]
modelk['time']=time.time() - start_time
model[k]=modelk
return model
##########################################################################################################################
def mse(Y,Y_pred):
return np.mean((Y-Y_pred)**2)
def predict(K,C,L):
return np.dot(np.dot(K,C),L)