-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
176 lines (151 loc) · 6.58 KB
/
utils.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
import numpy as np
from numpy.linalg import norm
import pickle
import matplotlib.pyplot as plt
import itertools
from scipy.stats import norm as norm_d
from scipy.stats import expon
from scipy.stats import weibull_min as weibull
from scipy.stats import burr12 as burr
from scipy.stats import randint
from scipy.stats import uniform
from scipy.optimize import minimize
import copy
import math
import time
from scipy.optimize import minimize
from scipy.sparse.linalg import svds
from scipy.linalg import svdvals
import scipy
from sklearn.datasets import load_svmlight_file
import pickle
from pathlib import Path
def prepare_data(dataset):
filename = "datasets/" + dataset + ".txt"
data = load_svmlight_file(filename)
A, y = data[0], data[1]
m, n = A.shape
if (2 in y) & (1 in y):
y = 2 * y - 3
if (2 in y) & (4 in y):
y = y - 3
assert((-1 in y) & (1 in y))
sparsity_A = A.count_nonzero() / (m * n)
return A, y, m, n, sparsity_A
def compute_L(dataset, A):
filename = "dump/"+dataset+"_L.txt"
file_path = Path(filename)
if file_path.is_file():
with open(filename, 'rb') as file:
L, average_L, worst_L = pickle.load(file)
else:
sigmas = svds(A, return_singular_vectors=False)
m = A.shape[0]
L = sigmas.max()**2 / (4*m)
worst_L = 0
average_L = 0
denseA = A.toarray()
for i in range(m):
L_temp = (norm(denseA[i])**2)*1.0 / 4
average_L += L_temp / m
if L_temp > worst_L:
worst_L = L_temp
with open(filename, 'wb') as file:
pickle.dump([L, average_L, worst_L],file)
return L, average_L, worst_L
def save_solution(dataset, l2, l1, x_star, f_star):
filename = "dump/"+dataset+"_solution_l2_"+str(l2)+"_l1_"+str(l1)+".txt"
with open(filename, 'wb') as file:
pickle.dump([x_star, f_star], file)
def read_solution(dataset, l2, l1):
with open('dump/'+dataset+'_solution_l2_'+str(l2)+"_l1_"+str(l1)+".txt", 'rb') as file:
return pickle.load(file)
def read_results_from_file(filename, method, args):
if method == "SGD_const_stepsize":
with open('dump/'+filename+'_SGD_const_stepsize_gamma_'+str(args[0])+"_l2_"+str(args[1])+"_l1_"+str(args[2])
+"_num_of_epochs_"+str(args[3])+"_batch_size_"+str(args[4])+".txt", 'rb') as file:
return pickle.load(file)
if method == "clipped-SGD_const_stepsize":
with open('dump/'+filename+'_clipped-SGD_const_stepsize_gamma_'+str(args[0])+"_lambda_"+str(args[1])+"_l2_"
+str(args[2])+"_l1_"+str(args[3])
+"_num_of_epochs_"+str(args[4])+"_batch_size_"+str(args[5])+".txt", 'rb') as file:
return pickle.load(file)
if method == "clipped-SGD_const_stepsize_decr_clip":
with open('dump/'+filename+'_clipped-SGD_decr_clip_const_stepsize_gamma_'+str(args[0])
+"_init_lambda_"+str(args[1][0])+"_decr_period_"+str(args[1][1])
+"_decr_coeff_"+str(args[1][2])+"_l2_"
+str(args[2])+"_l1_"+str(args[3])
+"_num_of_epochs_"+str(args[4])+"_batch_size_"+str(args[5])+".txt", 'rb') as file:
return pickle.load(file)
if method == "SGD_const_stepsize toy expect":
with open('dump/'+filename+'_SGD_const_stepsize_toy_expect_gamma_'+str(args[0])
+"_num_of_iters_"+str(args[1])+"_distrib_"+args[2]+".txt", 'rb') as file:
return pickle.load(file)
if method == "clipped-SGD_const_stepsize toy expect":
with open('dump/'+filename+'_clipped_SGD_const_stepsize_toy_expect_gamma_'+str(args[0])
+"_lambda_"+str(args[1])
+"_num_of_iters_"+str(args[2])+"_distrib_"+args[3]
+"_clip_activates_"+str(args[4])+".txt", 'rb') as file:
return pickle.load(file)
if method == "clipped-SSTM":
with open('dump/'+filename+'_clipped-SSTM_a_'+str(args[0])+"_B_"+str(args[1])+"_L_"+str(args[2])
+"_l2_"+str(args[3])
+"_num_of_epochs_"+str(args[4])+"_batch_size_"+str(args[5])+".txt", 'rb') as file:
return pickle.load(file)
if method == "SSTM":
with open('dump/'+filename+'_SSTM_a_'+str(args[0])+"_L_"+str(args[1])
+"_l2_"+str(args[2])
+"_num_of_epochs_"+str(args[3])+"_batch_size_"+str(args[4])+".txt", 'rb') as file:
return pickle.load(file)
def make_plots(args):
supported_modes_y = ['func_vals', 'squared_distances']
supported_modes_x = ['time', 'data_passes', 'iters']
filename = args[0]
mode_y = args[1]
mode_x = args[2]
figsize = args[3]
sizes = args[4]
title = args[5]
methods = args[6]
bbox_to_anchor = args[7]
legend_loc = args[8]
save_fig = args[9]
title_size = sizes[0]
linewidth = sizes[1]
markersize = sizes[2]
legend_size = sizes[3]
xlabel_size = sizes[4]
ylabel_size = sizes[5]
xticks_size = sizes[6]
yticks_size = sizes[7]
assert(mode_y in supported_modes_y)
assert(mode_x in supported_modes_x)
plt.figure(figsize=figsize)
plt.title(title, fontsize=title_size)
marker = itertools.cycle(('+', 'd', 'x', 'o', '^', 's', '*', 'p', '<', '>', '^'))
num_of_methods = len(methods)
for idx, method in enumerate(methods):
res = read_results_from_file(filename, method[0], method[1])
if method[3] == None:
length = len(res['iters'])
else:
length = method[3]
plt.semilogy(res[mode_x][0:length], res[mode_y][0:length] / res[mode_y][0], linewidth=linewidth, marker=next(marker),
markersize = markersize,
markevery=range(-idx*int(length/(10*num_of_methods)), len(res[mode_x][0:length]), int(length/10)),
label = method[2])
plt.legend(bbox_to_anchor=bbox_to_anchor, loc=legend_loc, fontsize=legend_size)
if mode_x == 'time':
plt.xlabel(r"Time, $s$", fontsize=xlabel_size)
if mode_x == 'data_passes':
plt.xlabel(r"Number of passes through the data", fontsize=xlabel_size)
if mode_x == 'iters':
plt.xlabel(r"Number of iterations", fontsize=xlabel_size)
if mode_y == 'func_vals':
plt.ylabel(r"$\frac{f(x^k)-f(x^*)}{f(x^0)-f(x^*)}$", fontsize=ylabel_size)
if mode_y == 'squared_distances':
plt.ylabel(r"$\frac{||x^k - x^*||_2^2}{||x^0 - x^*||_2^2}$", fontsize=ylabel_size)
plt.xticks(fontsize=xticks_size)
_ = plt.yticks(fontsize=yticks_size)
if save_fig[0]:
plt.savefig("plot/"+save_fig[1], bbox_inches='tight')