-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplots.py
More file actions
353 lines (286 loc) · 13.1 KB
/
plots.py
File metadata and controls
353 lines (286 loc) · 13.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import matplotlib.pyplot as plt
import numpy as np
import functions as fn
from matplotlib.lines import Line2D
import scipy.sparse as sp
def plot_cost_and_norm(cost, norm, iters, title):
plt.figure('Cost and Norm of ' + title)
plt.subplot(1, 2, 1)
plt.semilogy(np.arange(iters),cost[:iters])
plt.xlabel('Iteration', fontsize=14)
plt.ylabel('Cost', fontsize=14)
plt.title('Cost of ' + title, fontsize=16)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.subplot(1, 2, 2)
plt.semilogy(np.arange(iters), norm[:iters])
plt.xlabel('Iteration', fontsize=14)
plt.ylabel('Norm', fontsize=14)
plt.title('Norm of ' + title, fontsize=16)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.show()
def convergence_of_z(ZZ_gt, iters, NN, graph_type):
plt.figure('Convergence of Z')
plt.suptitle('Convergence with ' + graph_type + ' graph')
plt.subplot(1, 2, 1)
for ii in range(NN):
plt.semilogx(np.arange(iters), ZZ_gt[:iters, ii, 0], label='Node ' + str(ii))
plt.xlabel('Iteration', fontsize=14)
plt.ylabel('Z', fontsize=14)
plt.title('Convergence of Z_x', fontsize=16)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.legend(fontsize=12)
plt.subplot(1, 2, 2)
for ii in range(NN):
plt.semilogx(np.arange(iters), ZZ_gt[:iters, ii, 1], label='Node ' + str(ii))
plt.xlabel('Iteration', fontsize=14)
plt.ylabel('Z', fontsize=14)
plt.title('Convergence of Z_y', fontsize=16)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.legend(fontsize=12)
plt.show()
def error_of_z(ZZ_gt, iters, NN, ZZ_opt):
error = np.zeros((iters,NN,2))
for kk in range(iters):
for ii in range(NN):
error[kk,ii,:] = ZZ_gt[kk,ii]-ZZ_opt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
for ii in range(NN):
ax1.semilogx(np.arange(iters-1), error[:iters-1, ii, 0], label='Node ' + str(ii))
ax1.set_xlabel('Iteration')
ax1.set_ylabel('Consensus error - Z[0]')
ax1.set_title('Consensus Error Evolution - Z[0]')
ax1.grid(True)
ax1.legend()
for ii in range(NN):
ax2.semilogx(np.arange(iters-1), error[:iters-1, ii, 1], label='Node ' + str(ii))
ax2.set_xlabel('Iteration')
ax2.set_ylabel('Consensus error - Z[1]')
ax2.set_title('Consensus Error Evolution - Z[1]')
ax2.grid(True)
ax2.legend()
plt.tight_layout()
plt.show()
def plot_dataset(dataset, labels, ww, bb, title, type):
for ii in range(dataset.shape[0]):
if labels[ii] == 1:
plt.plot(dataset[ii, 0], dataset[ii, 1], 'bo', label='Class 1' if ii == 0 else "")
else:
plt.plot(dataset[ii, 0], dataset[ii, 1], 'ro', label='Class -1' if ii == 0 else "")
resolution = 1000
xx = np.linspace(min(dataset[:, 0]), max(dataset[:, 0]), resolution)
yy = np.linspace(min(dataset[:, 1]), max(dataset[:, 1]), resolution)
XX, YY = np.meshgrid(xx, yy)
ZZ = np.zeros(XX.shape)
for ii in range(XX.shape[0]):
for jj in range(XX.shape[1]):
ZZ[ii, jj] = fn.binary_classifier(fn.phi(np.array([XX[ii, jj], YY[ii, jj]]), type), ww, bb)[0]
plt.contour(XX, YY, ZZ, levels=[0], colors='k')
plt.grid()
plt.title(title)
plt.show()
def plot_test_dataset(test_dataset, test_labels, test_predictions, ww, bb, w_true, b_true, test_data_size,title, type):
plt.figure(figsize=(10, 6))
x = np.linspace(-10, 10, 400)
y = np.linspace(-5, 5, 400)
X, Y = np.meshgrid(x, y)
Z = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z[i, j] = np.dot(w_true, fn.phi([X[i, j], Y[i, j]], type)) + b_true
plt.contour(X, Y, Z, levels=[0], colors='black')
Z_pred = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z_pred[i, j] = np.dot(ww, fn.phi([X[i, j], Y[i, j]], type)) + bb
plt.contour(X, Y, Z_pred, levels=[0], colors='green', linestyles='dashed')
for i in range(test_data_size-1):
if test_predictions[i] == 1 and test_labels[i] == 1:
plt.scatter(test_dataset[i, 0], test_dataset[i, 1], color='blue', marker='o')
elif test_predictions[i] == -1 and test_labels[i] == -1:
plt.scatter(test_dataset[i, 0], test_dataset[i, 1], color='red', marker='o')
else:
plt.scatter(test_dataset[i, 0], test_dataset[i, 1], color='green', marker='x')
plt.title(title)
plt.xlabel("D_1")
plt.ylabel("D_2")
plt.grid(True)
scatter_correct_class1 = plt.scatter([], [], color='blue', marker='o', label='Correct Class 1')
scatter_correct_class_neg1 = plt.scatter([], [], color='red', marker='o', label='Correct Class -1')
scatter_misclassified = plt.scatter([], [], color='green', marker='x', label='Misclassified')
plt.legend(handles=[scatter_correct_class1, scatter_correct_class_neg1, scatter_misclassified], loc='upper left', bbox_to_anchor=(1, 1))
plt.show()
def plot_decision_boundary_evolution(ww_plot, bb_plot, w_true, b_true, type, title,t1,t2,t3):
plt.figure(figsize=(10, 6))
x = np.linspace(-10, 10, 400)
y = np.linspace(-5, 5, 400)
X, Y = np.meshgrid(x, y)
Z = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z[i, j] = np.dot(w_true, fn.phi([X[i, j], Y[i, j]], type)) + b_true
actual_contour = plt.contour(X, Y, Z, levels=[0], colors='black')
colors = ['green', 'blue', 'red']
labels = ['Iteration ' + str(t1), 'Iteration ' + str(t2), 'Iteration ' + str(t3)]
linestyles = ['dashed', 'dashed', 'dashed']
for idx in range(3):
ww = ww_plot[idx]
bb = bb_plot[idx]
Z_pred = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z_pred[i, j] = np.dot(ww, fn.phi([X[i, j], Y[i, j]], type)) + bb
plt.contour(X, Y, Z_pred, levels=[0], colors=colors[idx], linestyles=linestyles[idx])
plt.title(title)
plt.xlabel("D_1")
plt.ylabel("D_2")
plt.grid(True)
custom_lines = [
Line2D([0], [0], color='black', lw=2, label='Actual Decision Boundary'),
Line2D([0], [0], color='green', lw=2, linestyle='dashed', label=labels[0]),
Line2D([0], [0], color='blue', lw=2, linestyle='dashed', label=labels[1]),
Line2D([0], [0], color='red', lw=2, linestyle='dashed', label=labels[2])
]
plt.legend(handles=custom_lines)
plt.show()
def plot_decision_boundaries_agents_T3(ww_plot_agents, bb_plot_agents, w_true, b_true, type, NN, iterations):
fig, axs = plt.subplots(5, 2, figsize=(20, 10)) # 2 rows, 5 columns for 10 agents
axs = axs.flatten()
x = np.linspace(-10, 10, 400)
y = np.linspace(-5, 5, 400)
X, Y = np.meshgrid(x, y)
Z = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z[i, j] = np.dot(w_true, fn.phi([X[i, j], Y[i, j]], type)) + b_true
for agent in range(NN):
ax = axs[agent]
ax.contour(X, Y, Z, levels=[0], colors='black')
colors = ['green', 'blue', 'red']
labels = [f'Iteration {t}' for t in iterations]
for idx in range(len(iterations)):
ww = ww_plot_agents[agent, idx]
bb = bb_plot_agents[agent, idx]
Z_pred = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z_pred[i, j] = np.dot(ww, fn.phi([X[i, j], Y[i, j]], type)) + bb
ax.contour(X, Y, Z_pred, levels=[0], colors=colors[idx], linestyles='dashed')
ax.set_title(f'Agent {agent + 1}')
ax.set_xlabel("D_1")
ax.set_ylabel("D_2")
ax.grid(True)
custom_lines = [
Line2D([0], [0], color='black', lw=2, label='Actual Decision Boundary'),
Line2D([0], [0], color='green', lw=2, linestyle='dashed', label=labels[0]),
Line2D([0], [0], color='blue', lw=2, linestyle='dashed', label=labels[1]),
Line2D([0], [0], color='red', lw=2, linestyle='dashed', label=labels[2])
]
fig.legend(handles=custom_lines, loc='upper right', ncol=2 )
plt.suptitle('Decision Boundaries of Agents at Different Iterations')
plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.show()
def plot_error_evolution(error, NN, MAXITERS):
fig, axs = plt.subplots(2, 5, figsize=(20, 10))
axs = axs.flatten()
for agent in range(NN):
ax = axs[agent]
ax.semilogy(range(MAXITERS-1), error[:-1, agent], label=f'Agent {agent + 1}')
ax.set_title(f'Agent {agent + 1}')
ax.set_xlabel('Iterations')
ax.set_ylabel('Error $||\\nabla \\ell_i - s_i||$')
ax.grid(True)
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.suptitle('Error Evolution of Agents')
plt.show()
def plot_predictions(dataset, labels, ww, bb, title, NN, w_true, b_true, type):
x = np.linspace(-10, 10, 400)
y = np.linspace(-5, 5, 400)
X, Y = np.meshgrid(x, y)
Z = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z[i, j] = np.dot(w_true, fn.phi([X[i, j], Y[i, j]], type)) + b_true
fig, axs = plt.subplots(5, 2, figsize=(20, 25))
fig.suptitle(title, fontsize=16)
for jj in range(NN):
for ii in range(len(dataset[jj])):
prediction, _ = fn.binary_classifier(fn.phi(dataset[jj][ii], type), ww[jj], bb[jj])
if prediction == 1 and labels[jj][ii] == 1:
axs[jj // 2, jj % 2].scatter(dataset[jj][ii][0], dataset[jj][ii][1], color='blue', marker='o', s=10)
elif prediction == -1 and labels[jj][ii] == -1:
axs[jj // 2, jj % 2].scatter(dataset[jj][ii][0], dataset[jj][ii][1], color='red', marker='o', s=10)
else:
axs[jj // 2, jj % 2].scatter(dataset[jj][ii][0], dataset[jj][ii][1], color='green', marker='x', s=25)
Z_pred = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z_pred[i, j] = np.dot(ww[jj], fn.phi([X[i, j], Y[i, j]], type)) + bb[jj]
axs[jj // 2, jj % 2].contour(X, Y, Z_pred, levels=[0], colors='green', linestyles='dashed')
axs[jj // 2, jj % 2].contour(X, Y, Z, levels=[0], colors='black')
axs[jj // 2, jj % 2].set_title(f'Agent {jj+1}')
scatter_correct_class1 = plt.scatter([], [], color='blue', marker='o', s=25, label='Correct Class 1')
scatter_correct_class_neg1 = plt.scatter([], [], color='red', marker='o', s=25, label='Correct Class -1')
scatter_misclassified = plt.scatter([], [], color='green', marker='x', s=25, label='Misclassified')
fig.legend(handles=[scatter_correct_class1, scatter_correct_class_neg1, scatter_misclassified], loc='upper left')
plt.tight_layout(rect=[0.1, 0.1, 1, 0.95])
plt.show()
def plot_sorted_dataset(dataset, N, w_true, b_true, type):
plt.figure(figsize=(10, 6))
x = np.linspace(-10, 10, 400)
y = np.linspace(-5, 5, 400)
X, Y = np.meshgrid(x, y)
Z = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z[i, j] = np.dot(w_true, fn.phi([X[i, j], Y[i, j]], type)) + b_true
colors = plt.cm.jet(np.linspace(0, 1, N))
for i, split in enumerate(dataset):
plt.scatter(split[:, 0], split[:, 1], color=colors[i], label=f'Agent {i+1}')
plt.title('Dataset Splits Among Agents')
plt.xlabel('D[0]')
plt.ylabel('D[1]')
plt.legend()
plt.show()
def plot_cost_T3(cost_gt, norm_gt, MAXITERS, title):
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.semilogy(np.arange(MAXITERS - 1), cost_gt, label="Cost of " + title)
plt.xlabel('Iteration')
plt.ylabel('Cost')
plt.title('Cost vs Iteration')
plt.grid()
plt.subplot(1, 2, 2)
plt.semilogy(np.arange(MAXITERS - 1), norm_gt, label="Norm of gradient of " + title)
plt.xlabel('Iteration')
plt.ylabel('Gradient Norm')
plt.title('Gradient Norm vs Iteration')
plt.grid()
plt.tight_layout()
plt.show()
def plot_parameter_convergence(WW, BB, w_true, b_true, dim_w, NN, MAXITERS):
for dim in range(dim_w):
plt.figure(figsize=(12, 6))
for ii in range(NN):
plt.semilogx(np.arange(MAXITERS), WW[:, ii, dim], label=f'Node {ii+1}')
plt.axhline(y=w_true[dim], color='r', linestyle='--', label='True Weight' if ii == 0 else "")
plt.xlabel('Iteration')
plt.ylabel(f'Weight {dim+1} Value')
plt.title(f'Convergence of Weight {dim+1}')
plt.legend()
plt.grid()
plt.show()
plt.figure(figsize=(12, 6))
for ii in range(NN):
plt.semilogx(np.arange(MAXITERS), BB[:, ii], label=f'Node {ii+1}')
plt.axhline(y=b_true, color='r', linestyle='--', label='True Bias')
plt.xlabel('Iteration')
plt.ylabel('Bias Value')
plt.title('Convergence of Bias')
plt.legend()
plt.grid()
plt.show()