-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSR3_algorithm.py
More file actions
282 lines (252 loc) · 8.4 KB
/
SR3_algorithm.py
File metadata and controls
282 lines (252 loc) · 8.4 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
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 24 13:20:24 2021
@author: pccom
"""
import numpy as np
from scipy.linalg import cho_factor
from scipy.linalg import cho_solve
from pysindy.utils import prox_l1, prox_l0
def update_full_coef(cho, psi_transpose_y, coef_sparse, nu, iters):
"""Update the unregularized weight vector"""
b = psi_transpose_y + coef_sparse / nu
coef_full = cho_solve(cho, b)
return coef_full, iters + 1
def update_full_coef_constraints(
H,
psi_transpose_y,
coef_sparse,
nu,
equality_constraint_matrix,
equality_constraint_vector,
iters,
):
inv1 = np.linalg.inv(H)
inv1_mod = np.kron(np.eye(coef_sparse.shape[1]), inv1)
inv2 = np.linalg.inv(
equality_constraint_matrix.dot(inv1_mod).dot(equality_constraint_matrix.T)
)
RHS_val = -equality_constraint_vector + equality_constraint_matrix.dot(
inv1_mod
).dot(psi_transpose_y.flatten(order="F"))
phi = inv2.dot(RHS_val)
xi = inv1_mod.dot(
-equality_constraint_matrix.T.dot(phi) + psi_transpose_y.flatten(order="F")
)
xi_v2 = xi.reshape(coef_sparse.T.shape, order="C").T
return xi_v2, iters + 1
def update_sparse_coef(coef_full, threshold, type_penalty="l0"):
"""Update the regularized weight vector"""
if type_penalty == "l1":
coef_sparse = prox_l1(coef_full, threshold)
else:
coef_sparse = prox_l0(coef_full, threshold)
return coef_sparse
def evaluate_objective(
Psi, Y, coef_full, coef_sparse, nu, threshold, type_penalty="l0"
):
"""objective function"""
R2 = np.linalg.norm(Y - np.dot(Psi, coef_full)) ** 2
D2 = np.linalg.norm(coef_full - coef_sparse) ** 2
if type_penalty == "l1":
return (
0.5 * np.sum(R2)
+ regularization_l1(coef_full, 0.5 * threshold ** 2 / nu)
+ 0.5 * np.sum(D2) / nu
)
else:
return (
0.5 * np.sum(R2)
+ regularization_l0(coef_full, 0.5 * threshold ** 2 / nu)
+ 0.5 * np.sum(D2) / nu
)
def regularization_l1(x, lam):
return lam * np.sum(np.abs(x))
def regularization_l0(x, lam):
return lam * np.count_nonzero(x)
def convergence_criterion(data, nu):
"""Calculate the convergence criterion for the optimization"""
this_coef = data[-1]
if len(data) > 1:
last_coef = data[-2]
else:
last_coef = np.zeros_like(this_coef)
err_coef = np.sqrt(np.sum((this_coef - last_coef) ** 2)) / nu
return err_coef
def run_SR3(
Psi,
Y,
parameters,
regularization="l0",
max_iter=30,
tolerance=1.0e-14,
equality_constraint_matrix=None,
equality_constraint_vector=None,
):
nu = parameters[0]
threshold = parameters[1]
Psi = Psi.T
Y = Y.T
n_samples, n_features = Psi.shape
_, n_dimension = Y.shape
coef_sparse = np.zeros((n_features, n_dimension))
# Precompute some objects for upcoming least-squares solves.
# Assumes that self.nu is fixed throughout optimization procedure.
H = np.dot(Psi.T, Psi) + np.diag(np.full(n_features, 1.0 / nu))
psi_transpose_y = np.dot(Psi.T, Y)
if equality_constraint_matrix is None and equality_constraint_vector is None:
cho = cho_factor(H)
obj_his = []
iters = 0
for i in range(max_iter):
if equality_constraint_matrix is None and equality_constraint_vector is None:
coef_full, iters = update_full_coef(
cho, psi_transpose_y, coef_sparse, nu, iters
)
else:
coef_full, iters = update_full_coef_constraints(
H,
psi_transpose_y,
coef_sparse,
nu,
equality_constraint_matrix,
equality_constraint_vector,
iters,
)
coef_sparse = update_sparse_coef(
coef_full, threshold, type_penalty=regularization
)
obj_his.append(
evaluate_objective(
Psi,
Y,
coef_full,
coef_sparse,
nu,
threshold,
type_penalty=regularization,
)
)
if convergence_criterion(obj_his, nu) < tolerance:
break
coef_ = coef_sparse.T
# coef_full_ = coef_full.T
# obj_his = obj_his
return coef_
# Select the parameter that minimizes the loss_function.
def regularization_selection_SR3(
loss_function,
type_algorithm,
options,
show_progress=False,
skip_minimization_check=False,
):
if type_algorithm == "minimization":
from scipy.optimize import minimize
reg = minimize(
lambda x: loss_function(x),
np.asarray([1.0e3, 0.00]),
method="SLSQP",
bounds=[(0.01, np.inf), (0.0, np.inf)],
tol=1.0e-12,
options={"maxiter": 1000, "ftol": 1e-09},
)
# reg = minimize_scalar(lambda x: loss_function(x), method='bounded', bounds=(options['min'], options['max']), options = {'xatol': options['xatol'], 'maxiter': options['number_evaluations']})
if skip_minimization_check:
if show_progress:
print(
"Final parameter selected: ",
reg["x"],
" Number of calls: ",
reg,
" Max number calls: ",
)
return reg["x"]
else:
# Compare to the loss if we have zero regularization.
if loss_function(reg["x"]) <= loss_function(0.0):
if show_progress:
print(
"Final parameter selected: ",
reg["x"],
" Number of calls: ",
reg,
" Max number calls: ",
options["number_evaluations"],
)
return reg["x"]
else:
if show_progress:
print(
"Final parameter selected: ",
0.0,
" Number of calls: ",
reg,
" Max number calls: ",
options["number_evaluations"],
)
return 0.0
if type_algorithm == "bayesian":
from hyperopt import fmin, hp, tpe, Trials
from auxiliary_functions import plot_params_tried
if options["distribution"] == "log-uniform":
trls = Trials()
# SPACE = [hp.uniform(str(dim), 1.0e-3, 1.0e5) for dim in range(2)]
SPACE = {"x": hp.loguniform("x", 4, 24), "y": hp.loguniform("y", -20, -3)}
def modified_loss_function(x):
vect = np.asarray([x["x"], x["y"]])
return loss_function(vect)
reg = fmin(
modified_loss_function,
space=SPACE,
trials=trls,
max_evals=options["number_evaluations"],
algo=tpe.suggest,
show_progressbar=False,
)
print("Regularization parameter: ", reg)
if show_progress:
plot_params_tried(SPACE, trls, dist_plots=True)
return np.asarray([reg["x"], reg["y"]])
assert False, "Type of algorithm selected is incorrect"
def train_SR3(
Psi,
Y,
testing_function,
type_regularization="l0",
equality_constraint_matrix=None,
equality_constraint_vector=None,
type_parameter_search="bayesian",
options={
"min": 0.0,
"max": 1.0,
"number_evaluations": 2000,
"distribution": "log-uniform",
"inner_iterations": 5000,
},
show_progress=False,
skip_minimization_check=True,
):
alpha = regularization_selection_SR3(
lambda x: testing_function.evaluate(
run_SR3(
Psi,
Y,
x,
regularization=type_regularization,
equality_constraint_matrix=equality_constraint_matrix,
equality_constraint_vector=equality_constraint_vector,
)
),
type_parameter_search,
options,
show_progress=show_progress,
skip_minimization_check=True,
)
return run_SR3(
Psi,
Y,
alpha,
equality_constraint_matrix=equality_constraint_matrix,
equality_constraint_vector=equality_constraint_vector,
)