-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdump_bounds.py
168 lines (140 loc) · 6.63 KB
/
dump_bounds.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
# Copyright (c) 2020, Zhouxing shi <[email protected]>
# Licenced under the BSD 2-Clause License.
"""
Main results for 1-word perturbation
"""
import sys, os, json, pdb, argparse
def load_result(filename):
with open(filename) as file:
return json.loads(file.read())
def print_small_float(x):
str = "%.1E" % x
idx = str.find("E")
if str[idx + 2] == "0":
str = str[:(idx+2)] + str[(idx+3):]
return str
if __name__ == "__main__":
acc = load_result("results/res_accuracy.json")
p_list = ["1", "2", "100"]
def dump_one_word():
for i, dataset in enumerate(["Yelp", "SST"]):
for j, model in enumerate(["1", "2", "3"]):
if j == 0:
print("\multirow{9}{*}{%s} " % dataset, end="")
else:
print("\cline{2-12}")
print("& ", end="")
for k, p in enumerate(["1", "2", "\infty"]):
if k == 0:
print("\multirow{3}{*}{%s} & \multirow{3}{*}{%.2f} " % (
model, acc["model_{}_{}".format(dataset.lower(), model)] * 100), end="")
else:
print("& & ", end="")
print("& ", end="")
print("$\ell_%s$ & " % p, end="")
res_upper = load_result("results/res_model_{}_{}_discrete_{}_1.json".format(
dataset.lower(), model, p_list[k]))
print("{:.3f} & {:.3f} & ".format(res_upper["minimum"], res_upper["average"]))
res = load_result("results/res_model_{}_{}_ibp_{}_1.json".format(
dataset.lower(), model, p_list[k]))
print("{} & {} & ".format(
print_small_float(res["minimum"]), print_small_float(res["average"])))
res = load_result("results/res_model_{}_{}_baf_{}_1.json".format(
dataset.lower(), model, p_list[k]))
print("{:.3f} & {:.3f} & ".format(res["minimum"], res["average"]))
print("{:.0f}\\% & {:.0f}\\% ".format(
res["minimum"] / res_upper["minimum"] * 100,
res["average"] / res_upper["average"] * 100), end="")
print("\\\\")
print("\hline")
def dump_two_word():
for j, model in enumerate(["1", "2", "3"]):
print("{} & ".format(model))
for i, dataset in enumerate(["Yelp", "SST"]):
res = load_result("results/res_model_{}_{}_ibp_2_2.json".format(
dataset.lower(), model))
print("{} & {} & ".format(
print_small_float(res["minimum"]), print_small_float(res["average"])))
res = load_result("results/res_model_{}_{}_baf_2_2.json".format(
dataset.lower(), model))
print("{:.3f} & {:.3f} ".format(
res["minimum"], res["average"]))
print("\\\\")
def dump_framework():
acc = [91.3, 83.3]
for i, dataset in enumerate(["Yelp", "SST"]):
for j, p in enumerate(["1", "2", "\infty"]):
if j == 0:
print("\multirow{3}{*}{%s} & \multirow{3}{*}{%.2f}" % (dataset, acc[i]), end="")
else:
print("& ", end="")
print(" & $\ell_%s$ & " % p, end="")
for k, method in enumerate(["forward", "backward", "baf"]):
if k > 0:
print(" & ", end="")
_p = p if p != "\infty" else "100"
res = load_result("results/res_model_{}_small_1_{}_{}_1.json".format(
dataset.lower(), method, p_list[j]))
avg_time = 0
for example in res["examples"]:
avg_time += example["time"]
avg_time /= len(res["examples"])
print("%.3f & %.3f & %.1f " % (
res["minimum"], res["average"], avg_time), end="")
print("\\\\")
print("\hline")
def dump_layer_normalization():
ln_list = ["_standard", "_no", ""]
for i, dataset in enumerate(["Yelp", "SST"]):
for j, model in enumerate(["1", "2"]):
for jj, ln in enumerate(["Standard", "None", "Ours"]):
for k, p in enumerate(["1", "2", "\infty"]):
if j == 0 and jj == 0 and k == 0:
print("\multirow{%d}{*}{%s} " % (2 * 3 * 3, dataset), end="")
if jj == 0 and k == 0:
print(" & \multirow{%d}{*}{%s} " % (3 * 3, model), end="")
else:
print(" & ", end="")
if k == 0:
print(" & \multirow{3}{*}{%s} " % (ln), end="")
print(" & \multirow{3}{*}{%.1f} " % (
acc["model_{}_{}{}".format(dataset.lower(), model, ln_list[jj])] * 100), end="")
else:
print(" & ", end="")
print(" & ", end="")
print(" & $\ell_%s$ " % p, end="")
res_upper = load_result("results/res_model_{}_{}{}_discrete_{}_1{}.json".format(
dataset.lower(), model, ln_list[jj], p_list[k], ln_list[jj]))
print("& {:.3f} & {:.3f} ".format(res_upper["minimum"], res_upper["average"]))
res = load_result("results/res_model_{}_{}{}_baf_{}_1{}.json".format(
dataset.lower(), model, ln_list[jj], p_list[k], ln_list[jj]))
print("& {:.3f} & {:.3f} ".format(res["minimum"], res["average"]))
if jj == 0:
print("& %s\\ & %s\\ " % (
print_small_float(res["minimum"] / res_upper["minimum"]),
print_small_float(res["average"] / res_upper["average"])), end="")
else:
print("& %.0f\\%% & %.0f\\%% " % (
res["minimum"] / res_upper["minimum"] * 100,
res["average"] / res_upper["average"] * 100), end="")
print("\\\\")
if jj < 2:
print("\cline{3-11}")
if j == 0:
print("\cline{2-11}")
print("\hline")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("exp", type=str, default=None,
choices=["one", "two", "frameworks", "ln"])
args = parser.parse_args()
if args.exp == "one":
dump_one_word()
elif args.exp == "two":
dump_two_word()
elif args.exp == "frameworks":
dump_framework()
elif args.exp == "ln":
dump_layer_normalization()
else:
raise NotImplementedError