-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_l1_regression.py
266 lines (242 loc) · 9.29 KB
/
main_l1_regression.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import utils.experiments as experiments
import power_alm.functions as fun
import power_alm.inner_solver as optim
import power_alm.solver as solver
import power_alm.problems as problems
import power_alm.alm as alm
class L1Regression(experiments.Experiment):
def __init__(self, name, config, optimizer_configs, num_runs):
super().__init__(name, config["n"], config, optimizer_configs, num_runs)
def get_filename(self):
return (self.name + "_config_"
+ self.config["name"]
+ "_num_runs_" + str(self.num_runs)
+ "_seed_" + str(self.config["seed"])
+ "_m_" + str(self.config["m"])
+ "_n_" + str(self.config["n"]))
def initialize_composite_problem(self):
m = config["m"]; n = config["n"]
A = 10 * np.random.rand(m, n) - 5
b = np.random.randn(m)
# b = np.zeros(m)
theta = 100.
# ALM parameters
penalty = 1.
x_init = np.random.randn(n)
y_init = np.random.randn(m)
problem = problems.L1RegressionProblem(A, b, theta = theta)
alm_problem = alm.L1RegressionAlmWrappedProblem(problem, 2., penalty, y_init)
self.composite_problem = optim.CompositeOptimizationProblem(x_init, alm_problem, fun.Zero())
def compute_optimum(self, x_init):
config = self.config
np.random.seed(config["seed"])
m = config["m"]; n = config["n"]
A = 10 * np.random.rand(m, n) - 5
b = np.random.randn(m)
# b = np.zeros(m)
theta = 100.
# ALM parameters
penalty = 100.
x_init = np.random.randn(n)
y_init = np.random.randn(m)
problem = problems.L1RegressionProblem(A, b, theta = theta)
alm_problem = alm.L1RegressionAlmWrappedProblem(problem, 2., penalty, y_init)
composite_problem = optim.CompositeOptimizationProblem(x_init, alm_problem, fun.Zero())
optimizer = solver.Solver(
composite_problem,
solver.SolverParameters(optim.Parameters(maxit=800, tol=1e-10, epsilon=1e-12, Wolfe=True), maxit=10, norm2=True),
inner_solver=optim.LBFGS,
callback= lambda k, nit, x : print(k - 1, nit, self.eval_objective_(x, composite_problem.diffable.y), self.eval_constraint_violation_(x, composite_problem.diffable.y), np.linalg.norm(composite_problem.diffable.eval_gradient(x)))
)
optimizer.run()
self.opt_obj = self.eval_objective_(optimizer.x, composite_problem.diffable.y)
self.opt_const = 0.
"""
Primal cost
"""
def eval_objective_(self, x, y):
# return self.composite_problem.diffable._problem.eval_objective(x) + np.dot(self.composite_problem.diffable.eval_res(x), y)
return self.composite_problem.diffable._problem.eval_objective(x) + np.linalg.norm(self.composite_problem.diffable.eval_res(x), 1)
def eval_objective(self, x):
return self.eval_objective_(x, self.composite_problem.diffable.y)
"""
Primal dual gap
"""
def eval_constraint_violation_(self, x, y):
return self.eval_objective_(x, y) + np.dot(y, self.composite_problem.diffable._problem.b) + 1. / (2 * self.composite_problem.diffable._problem.theta) * np.power(np.linalg.norm(- self.composite_problem.diffable._problem.A.T @ y), 2)
def eval_constraint_violation(self, x):
return self.eval_objective(x) + np.dot(self.composite_problem.diffable.y, self.composite_problem.diffable._problem.b) + 1. / (2 * self.composite_problem.diffable._problem.theta) * np.power(np.linalg.norm(- self.composite_problem.diffable._problem.A.T @ self.composite_problem.diffable.y), 2)
def eval_power_stepsize(self, x):
if self.config["norm2"]:
return np.power(self.composite_problem.diffable.penalty, self.composite_problem.diffable._q - 1.) * np.power(np.linalg.norm(self.composite_problem.diffable.y - self.composite_problem.diffable.y_old), 2 - self.composite_problem.diffable._q)
name = "l1_regression"
num_runs = 1
configs = [
{
"name": "l1_regression_normp_120x145",
"m": 120,
"n": 145,
"markevery": 2,
"plotevery": 20,
"seed": 120,
"maxit": 35,
"tol": 1e-7,
"init_proc": "np.zeros",
"norm2": False,
},
{
"name": "l1_regression_norm2_120x145",
"m": 120,
"n": 145,
"markevery": 2,
"plotevery": 20,
"seed": 120,
"maxit": 35,
"tol": 1e-7,
"init_proc": "np.zeros",
"norm2": True,
},
]
for config in configs:
optimizer_configs = [
{
"marker": "^",
"linestyle": "solid",
"color": "black",
"name": "p = 2, lamb = 1",
"label": "${q = 1, \lambda = 1}$",
"class": optim.UniversalFastPGMLan,
"parameters": solver.SolverParameters(
optim.Parameters(
maxit=100,
tol=1e-10,
),
maxit=config["maxit"],
tol=config["tol"],
penalty = 1.,
norm2=config["norm2"],
)
},
{
"marker": "^",
"linestyle": "dashdot",
"color": "black",
"name": "p = 2, adap lamb",
"label": "${q = 1, \lambda_0 = 1}$",
"class": optim.UniversalFastPGMLan,
"parameters": solver.SolverParameters(
optim.Parameters(
maxit=100,
tol=1e-10,
),
maxit=config["maxit"],
tol=config["tol"],
penalty = 1.,
norm2=config["norm2"],
adaptive_penalty=True,
)
},
{
"marker": "^",
"linestyle": "dashed",
"color": "black",
"name": "p = 2, lamb = 10",
"label": "${q = 1, \lambda = 10}$",
"class": optim.UniversalFastPGMLan,
"parameters": solver.SolverParameters(
optim.Parameters(
maxit=250,
tol=1e-10,
),
maxit=config["maxit"],
tol=config["tol"],
penalty = 10.,
norm2=config["norm2"],
)
},
{
"marker": "*",
"linestyle": "solid",
"color": "blue",
"name": "p = 1.9, lamb = 1",
"label": "${q = 0.9, \lambda = 1}$",
"class": optim.UniversalFastPGMLan,
"parameters": solver.SolverParameters(
optim.Parameters(
maxit=100,
tol=1e-10,
),
maxit=config["maxit"],
tol=config["tol"],
p = 1.9,
penalty = 1.,
norm2=config["norm2"],
)
},
{
"marker": "o",
"linestyle": "solid",
"color": "purple",
"name": "p = 1.8, lamb = 1",
"label": "${q = 0.8, \lambda = 1}$",
"class": optim.UniversalFastPGMLan,
"parameters": solver.SolverParameters(
optim.Parameters(
maxit=100,
tol=1e-10,
),
maxit=config["maxit"],
tol=config["tol"],
p = 1.8,
penalty = 1.,
norm2=config["norm2"],
)
},
{
"marker": "x",
"linestyle": "solid",
"color": "darkgreen",
"name": "p = 1.7, lamb = 1",
"label": "${q = 0.7, \lambda = 1}$",
"class": optim.UniversalFastPGMLan,
"parameters": solver.SolverParameters(
optim.Parameters(
maxit=100,
tol=1e-10,
),
maxit=config["maxit"],
tol=config["tol"],
penalty = 1.,
p = 1.7,
norm2=config["norm2"],
)
},
]
experiment = L1Regression(name, config, optimizer_configs, num_runs)
experiment.run(overwrite_file=False)
matplotlib.rcParams['mathtext.fontset'] = 'cm'
# matplotlib.rcParams.update({'font.size': 20})
# matplotlib.rcParams.update({'legend.fontsize': 18})
fig, ax = plt.subplots(figsize=(6, 5))
ax.set_xticks([]); ax.set_yticks([])
plt.xticks(fontsize=10); plt.yticks(fontsize=10)
# fig.suptitle("$m=" + str(config["m"]) + "$, " +
# "$n=" + str(config["n"]) + "$, ", fontsize=16)
ax.grid(True)
experiment.plot(markevery=config["markevery"], SINGLE_PLOT=False)
filename = experiment.get_filename()
suffix = ".pdf"
plt.savefig(experiments.RESULTS + filename + suffix, bbox_inches='tight')
plt.show(block=False)
matplotlib.rcParams['mathtext.fontset'] = 'cm'
matplotlib.rcParams.update({'font.size': 20})
matplotlib.rcParams.update({'legend.fontsize': 18})
fig, ax = plt.subplots(figsize=(6, 5))
# ax.set_xticks([]); ax.set_yticks([])
plt.xticks(fontsize=10); plt.yticks(fontsize=10)
ax.grid(True)
experiment.plot_powerstepsizes(markevery=config["markevery"], PLOT_TOTAL_NIT=True, xlabels = [""], ylabels=[""])
plt.show(block=True)