-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
178 lines (146 loc) · 6.1 KB
/
helpers.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
import matplotlib.pyplot as plt
import numpy as np
from decentralized_SGD_logistic import DecentralizedSGDLogistic
def plot_losses(losses, iterations_indices, optimum_loss=0, title="My_nice_plot",
xlabel="Iteration", ylabel="Error", labels=None, yscale="log", ylim=None,
figsize=(10, 5), save_as_pdf=False, pdf_name="my_nice_plot_in_PDF"):
"""Plot the different losses with values shifted by the value of the optimal loss.
:param losses: 2D Array containing the losses for each topology
:param iterations_indices: 1D Array containing the indices (in number of iteration) of the given losses
:param optimum_loss: Value of the optimal loss
:param title: Title of the plot
:param xlabel: Label of the x axis
:param ylabel: Label of the y axis
:param labels: Labels of the different topologies, e.g., labels=["fully connected", "ring", "torus"]
:param yscale: Scale of the y axis, e.g., "linear", "log",...
:param ylim: Limits of the y axis, e.g., ylim=(0.001, 1)
:param figsize: Size of the plot
:param save_as_pdf: Saves the plot as pdf if True
:param pdf_name: If the plot is saved as pdf, saves with this name
"""
y = np.array(losses).squeeze()
y -= optimum_loss
x = iterations_indices
plt.figure(figsize=figsize)
if len(y.shape) > 1:
# If more than one topology
for i in range(y.shape[0]):
if labels:
plt.plot(x, y[i], label=labels[i])
else:
plt.plot(x, y[i])
else:
if labels:
plt.plot(x, y, label=labels)
else:
plt.plot(x, y)
plt.yscale(yscale)
if ylim:
plt.ylim(ylim)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
if labels:
plt.legend(loc="best")
if save_as_pdf:
plt.savefig("plots/" + pdf_name + ".pdf", bbox_inches='tight')
plt.show()
def plot_losses_with_std(losses, iterations_indices, optimum_loss=0, title="My_nice_plot",
xlabel="Iteration", ylabel="Error", labels=None, yscale="log", ylim=None,
figsize=(10, 5), save_as_pdf=False, pdf_name="my_nice_plot_in_PDF"):
"""Plot the different losses with values shifted by the value of the optimal loss.
:param losses: 2D Array containing the losses for each topology
:param iterations_indices: 1D Array containing the indices (in number of iteration) of the given losses
:param optimum_loss: Value of the optimal loss
:param title: Title of the plot
:param xlabel: Label of the x axis
:param ylabel: Label of the y axis
:param labels: Labels of the different topologies, e.g., labels=["fully connected", "ring", "torus"]
:param yscale: Scale of the y axis, e.g., "linear", "log",...
:param ylim: Limits of the y axis, e.g., ylim=(0.001, 1)
:param figsize: Size of the plot
:param save_as_pdf: Saves the plot as pdf if True
:param pdf_name: If the plot is saved as pdf, saves with this name
"""
colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple']
x = iterations_indices
plt.figure(figsize=figsize)
if len(losses) > 1:
# If more than one topology
for i in range(len(losses)):
mean = np.vstack(losses[i]).mean(axis=0)
std = np.vstack(losses[i]).std(axis=0)
y = mean - optimum_loss
if labels:
plt.plot(x, y, label=labels[i], color=colors[i])
else:
plt.plot(x, y[i], color=colors[i])
plt.fill_between(x, y-std, y+std, facecolor=colors[i], alpha=0.4)
else:
mean = np.vstack(losses[i]).mean(axis=0)
std = np.vstack(losses[i]).std(axis=0)
y = mean - optimum_loss
if labels:
plt.plot(x, y, label=labels, color=colors[0])
else:
plt.plot(x, y, color=colors[0])
plt.fill_between(x, y-std, y+std, facecolor=colors[0], alpha=0.4)
plt.yscale(yscale)
if ylim:
plt.ylim(ylim)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
if labels:
plt.legend(loc="best")
if save_as_pdf:
plt.savefig("plots/" + pdf_name + ".pdf", bbox_inches='tight', pad_inches=0)
plt.show()
def load_csv_data(data_path):
"""Loads data and returns y (class labels), tX (features) and ids (event ids)"""
y = np.genfromtxt(data_path, delimiter=",", skip_header=1, dtype=str, usecols=1)
x = np.genfromtxt(data_path, delimiter=",", skip_header=1)
input_data = x[:, 2:]
# convert class labels from strings to binary (-1,1)
yb = np.ones(len(y))
yb[np.where(y=='b')] = -1
return yb, input_data
def clean(input_data, mean=False):
#Replace -999 by most frequent value of column
for i in range(input_data.shape[1]):
current_col = input_data[:, i]
if -999.0 in current_col:
indices_to_change = (current_col == -999.0)
if mean:
curr_mean = np.mean(current_col[~indices_to_change])
current_col[indices_to_change] = curr_mean
else:
(values,counts) = np.unique(current_col[~indices_to_change], return_counts=True)
ind=np.argmax(counts)
current_col[indices_to_change] = values[ind] if len(values) > 0 else 0
return input_data
def standardize(x):
"""Standardize the given data"""
means = x.mean(0)
stds = x.std(0)
return (x - means)/stds
def load_data():
y, A = load_csv_data('train.csv')
A = standardize(clean(A, True))
y = 1 *(y > 0.0)
return y, A
def run_logistic(A, y, param, logging=False):
m = DecentralizedSGDLogistic(**param)
list_losses = m.fit(A, y, logging=logging)
if logging:
print()
print('Final score: {0:.4f}'.format(m.score(A, y)))
return list_losses
def run_logistic_n_times(A, y, params, n):
all_losses = []
for i in range(n):
print('Decentralized optimization, run number', i + 1, '\n')
params['data_distribution_random_seed']= i + 1
params['random_seed']= i + 1
all_losses.append(run_logistic(A, y, params, logging=True))
return all_losses