-
Notifications
You must be signed in to change notification settings - Fork 156
/
plot_final_e20.py
129 lines (103 loc) · 5.88 KB
/
plot_final_e20.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
import os, sys
import re
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from matplotlib import rcParams
from mpl_toolkits.axisartist.axislines import Subplot
matplotlib.rc('xtick', labelsize=17)
matplotlib.rc('ytick', labelsize=17)
def parse_log(file_name):
rounds = []
accu = []
loss = []
sim = []
for line in open(file_name, 'r'):
search_train_accu = re.search(r'At round (.*) training accuracy: (.*)', line, re.M | re.I)
if search_train_accu:
rounds.append(int(search_train_accu.group(1)))
else:
search_test_accu = re.search(r'At round (.*) accuracy: (.*)', line, re.M | re.I)
if search_test_accu:
accu.append(float(search_test_accu.group(2)))
search_loss = re.search(r'At round (.*) training loss: (.*)', line, re.M | re.I)
if search_loss:
loss.append(float(search_loss.group(2)))
search_loss = re.search(r'gradient difference: (.*)', line, re.M | re.I)
if search_loss:
sim.append(float(search_loss.group(1)))
return rounds, sim, loss, accu
f = plt.figure(figsize=[23, 10])
log = ["synthetic_1_1", "mnist", "femnist", "shakespeare", "sent140_772user"]
titles = ["Synthetic", "MNIST", "FEMNIST", "Shakespeare", "Sent140"]
rounds = [200, 100, 200, 40, 800]
mus=[1, 1, 1, 0.001, 0.01]
drop_rates=[0, 0.5, 0.9]
sampling_rate = [1, 1, 2, 1, 10]
labels = ['FedAvg', r'FedProx ($\mu$=0)', r'FedProx ($\mu$>0)']
improv = 0
for drop_rate in range(3):
for idx in range(5):
ax = plt.subplot(3, 5, 5*(drop_rate)+idx+1)
if drop_rate == 0:
rounds1, sim1, losses1, test_accuracies1 = parse_log(log[idx] + "/fedprox_drop0_mu0")
else:
rounds1, sim1, losses1, test_accuracies1 = parse_log(log[idx] + "/fedavg_drop"+str(drop_rates[drop_rate]))
rounds2, sim2, losses2, test_accuracies2 = parse_log(log[idx] + "/fedprox_drop"+str(drop_rates[drop_rate])+"_mu0")
rounds3, sim3, losses3, test_accuracies3 = parse_log(log[idx] + "/fedprox_drop"+str(drop_rates[drop_rate])+"_mu" + str(mus[idx]))
if sys.argv[1] == 'loss':
if drop_rate == 2 and idx == 4:
plt.plot(np.asarray(rounds1[:len(losses1):sampling_rate[idx]]), np.asarray(losses1)[::sampling_rate[idx]], ":", linewidth=3.0, label=labels[0], color="#ff7f0e")
plt.plot(np.asarray(rounds2[:len(losses2):sampling_rate[idx]]), np.asarray(losses2)[::sampling_rate[idx]], '--', linewidth=3.0, label=labels[1], color="#e377c2")
plt.plot(np.asarray(rounds3[:len(losses3):sampling_rate[idx]]), np.asarray(losses3)[::sampling_rate[idx]], linewidth=3.0, label=labels[2], color="#17becf")
else:
plt.plot(np.asarray(rounds1[:len(losses1):sampling_rate[idx]]),
np.asarray(losses1)[::sampling_rate[idx]], ":", linewidth=3.0, color="#ff7f0e")
plt.plot(np.asarray(rounds2[:len(losses2):sampling_rate[idx]]),
np.asarray(losses2)[::sampling_rate[idx]], '--', linewidth=3.0, color="#e377c2")
plt.plot(np.asarray(rounds3[:len(losses3):sampling_rate[idx]]),
np.asarray(losses3)[::sampling_rate[idx]], linewidth=3.0, color="#17becf")
elif sys.argv[1] == 'accuracy':
if drop_rate == 2 and idx == 4:
plt.plot(np.asarray(rounds1[:len(test_accuracies1):sampling_rate[idx]]), np.asarray(test_accuracies1)[::sampling_rate[idx]], ":", linewidth=3.0, label=labels[0], color="#ff7f0e")
plt.plot(np.asarray(rounds2[:len(test_accuracies2):sampling_rate[idx]]), np.asarray(test_accuracies2)[::sampling_rate[idx]], '--', linewidth=3.0, label=labels[1], color="#e377c2")
plt.plot(np.asarray(rounds3[:len(test_accuracies3):sampling_rate[idx]]), np.asarray(test_accuracies3)[::sampling_rate[idx]], linewidth=3.0, label=labels[2], color="#17becf")
else:
plt.plot(np.asarray(rounds1[:len(test_accuracies1):sampling_rate[idx]]),
np.asarray(test_accuracies1)[::sampling_rate[idx]], ":", linewidth=3.0, color="#ff7f0e")
plt.plot(np.asarray(rounds2[:len(test_accuracies2):sampling_rate[idx]]),
np.asarray(test_accuracies2)[::sampling_rate[idx]], '--', linewidth=3.0, color="#e377c2")
plt.plot(np.asarray(rounds3[:len(test_accuracies3):sampling_rate[idx]]),
np.asarray(test_accuracies3)[::sampling_rate[idx]], linewidth=3.0, color="#17becf")
plt.xlabel("# Rounds", fontsize=22)
plt.xticks(fontsize=17)
plt.yticks(fontsize=17)
if sys.argv[1] == 'loss' and idx == 0:
plt.ylabel('Training Loss', fontsize=22)
elif sys.argv[1] == 'accuracy' and idx == 0:
plt.ylabel('Testing Accuracy', fontsize=22)
if drop_rate == 0:
plt.title(titles[idx], fontsize=22, fontweight='bold')
ax.tick_params(color='#dddddd')
ax.spines['bottom'].set_color('#dddddd')
ax.spines['top'].set_color('#dddddd')
ax.spines['right'].set_color('#dddddd')
ax.spines['left'].set_color('#dddddd')
ax.set_xlim(0, rounds[idx])
if sys.argv[1] == "loss":
if drop_rate == 0 and idx == 4:
plt.ylim(0.3, 1.8)
if drop_rate == 0 and idx == 1:
plt.ylim(0.2, 1.5)
if drop_rate == 1 and idx == 1:
plt.ylim(0.2, 1.5)
if drop_rate == 2 and idx == 1:
plt.ylim(0.2, 1.5)
if drop_rate == 2 and idx == 4:
plt.ylim(0.2, 4)
if drop_rate == 1 and idx == 4:
plt.ylim(0.2, 1.8)
f.legend(frameon=False, loc='lower center', ncol=3, prop=dict(weight='bold'), borderaxespad=-0.3, fontsize=26) # note: different from plt.legend
plt.tight_layout()
plt.subplots_adjust(bottom=0.12)
f.savefig(sys.argv[1] + "_full.pdf")