-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexp1.py
198 lines (176 loc) · 6.44 KB
/
exp1.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
r"""Exp 1:
- Fix:
- n=24, f=0
- momentum=0
- Number of iterations = 4500
- *Long tail* (alpha=500)
- Number of runs = 3
- ATK = NA
- LR = 0.01
- Varies:
- IID vs NonIID
- 5 Aggregators: AVG, KRUM, CM, RFA, CClip
- Bucketing or not
python exp1.py --use-cuda --debug --identifier "debug" \
-n 5 -f 0 --attack NA --agg avg
"""
from utils import get_args
from utils import main
from utils import EXP_DIR
args = get_args()
assert args.f == 0
assert args.attack == "NA"
assert args.momentum == 0
LOG_DIR = EXP_DIR + "exp1/"
if args.identifier:
LOG_DIR += f"{args.identifier}/"
elif args.debug:
LOG_DIR += "debug/"
else:
LOG_DIR += f"n{args.n}_f0NA_LT_m0/"
INP_DIR = LOG_DIR
OUT_DIR = LOG_DIR + "output/"
LOG_DIR += f"{args.agg}_{args.attack}_{args.noniid}_s{args.bucketing}_seed{args.seed}"
# Number of iterations = 4500
if args.debug:
MAX_BATCHES_PER_EPOCH = 30
EPOCHS = 10
else:
MAX_BATCHES_PER_EPOCH = 30
EPOCHS = 150
if not args.plot:
main(args, LOG_DIR, EPOCHS, MAX_BATCHES_PER_EPOCH)
else:
# Temporarily put the import functions here to avoid
# random error stops the running processes.
import os
import pandas as pd
from codes.parser import extract_validation_entries
def exp_grid():
attack = "NA"
for agg in ["cm", "cp", "rfa", "krum", "avg"]:
for seed in [0, 1, 2]:
for bucketing in [0, 2]:
for noniid in [False, True]:
yield agg, attack, noniid, bucketing, seed
results = []
for agg, attack, noniid, bucketing, seed in exp_grid():
grid_identifier = f"{agg}_{attack}_{noniid}_s{bucketing}_seed{seed}"
path = INP_DIR + grid_identifier + "/stats"
try:
values = extract_validation_entries(path)
accs = list(map(lambda x: x["top1"], values))
acc = sum(accs[-5:]) / len(accs[-5:])
results.append(
{
# The entry Iteration is dummy as we only take the test accuracy
# At the end of training
"Iterations": values[-1]["E"] * MAX_BATCHES_PER_EPOCH,
"Accuracy (%)": acc,
"Noniid": noniid,
"AGG": agg,
"seed": seed,
"bucketing": bucketing > 0,
}
)
except Exception as e:
pass
results = pd.DataFrame(results)
print(results)
# Example output:
# \begin{tabular}{lcc}
# \toprule
# Aggr & \iid & {\noniid} \\\midrule
# \textsc{Avg} & $98.84\!\pm\!0.08$ & $98.84\!\pm\!0.07$ \\
# \krum & $98.10\!\pm\!0.14$ & $82.97\!\pm\!3.64$ \\
# \cm & $97.82\!\pm\!0.20$ & $80.36\!\pm\!0.04$ \\
# \rfa & $98.72\!\pm\!0.11$ & $84.76\!\pm\!0.83$ \\
# \cclip & $98.76\!\pm\!0.10$ & $98.15\!\pm\!0.19$ \\
# \bottomrule
# \end{tabular}
if not os.path.exists(OUT_DIR):
os.makedirs(OUT_DIR)
def query(agg, noniid, bucketing, value="mean"):
#
a = results[
(results["AGG"] == agg)
& (results["Noniid"] == noniid)
& (results["bucketing"] == bucketing)
]
if value == "mean":
b = a["Accuracy (%)"].mean()
return "{:.2f}".format(b)
if value == "std":
b = a["Accuracy (%)"].std()
return "{:.2f}".format(b)
raise NotImplementedError(value)
for bucketing in [True, False]:
filename = "exp1.tex" if not bucketing else "exp1_bucketing.tex"
with open(OUT_DIR + filename, "w") as f:
f.write(r"\begin{tabular}{lcc}" + "\n")
f.write(r"\toprule" + "\n")
f.write(r"Aggr& \iid& {\noniid}\\\midrule" + "\n")
# ----------------------------------------------------------------
f.write(
r"\textsc{Avg} & $"
+ query("avg", False, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("avg", False, bucketing=bucketing, value="std")
+ r"$ & $"
+ query("avg", True, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("avg", True, bucketing=bucketing, value="std")
+ r"$ \\"
+ "\n"
)
f.write(
r"\krum & $"
+ query("krum", False, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("krum", False, bucketing=bucketing, value="std")
+ r"$ & $"
+ query("krum", True, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("krum", True, bucketing=bucketing, value="std")
+ r"$ \\"
+ "\n"
)
f.write(
r"\cm & $"
+ query("cm", False, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("cm", False, bucketing=bucketing, value="std")
+ r"$ & $"
+ query("cm", True, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("cm", True, bucketing=bucketing, value="std")
+ r"$ \\"
+ "\n"
)
f.write(
r"\rfa & $"
+ query("rfa", False, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("rfa", False, bucketing=bucketing, value="std")
+ r"$ & $"
+ query("rfa", True, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("rfa", True, bucketing=bucketing, value="std")
+ r"$ \\"
+ "\n"
)
f.write(
r"\cclip & $"
+ query("cp", False, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("cp", False, bucketing=bucketing, value="std")
+ r"$ & $"
+ query("cp", True, bucketing=bucketing, value="mean")
+ r"\!\pm\!"
+ query("cp", True, bucketing=bucketing, value="std")
+ r"$ \\"
+ "\n"
)
# ----------------------------------------------------------------
f.write(r"\bottomrule" + "\n")
f.write(r"\end{tabular}" + "\n")