-
Notifications
You must be signed in to change notification settings - Fork 134
/
get_finetune_tables_and_plots.py
429 lines (375 loc) · 12.7 KB
/
get_finetune_tables_and_plots.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
import pickle
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from rliable import library as rly, metrics, plot_utils
dataframe = pd.read_csv("runs_tables/finetune_urls.csv")
with open("bin/finetune_scores.pickle", "rb") as handle:
full_scores = pickle.load(handle)
os.makedirs("./out", exist_ok=True)
def get_average_scores(scores):
avg_scores = {algo: {ds: None for ds in scores[algo]} for algo in scores}
stds = {algo: {ds: None for ds in scores[algo]} for algo in scores}
for algo in scores:
for data in scores[algo]:
sc = scores[algo][data]
if len(sc) > 0:
ml = min(map(len, sc))
sc = [s[:ml] for s in sc]
scores[algo][data] = sc
avg_scores[algo][data] = np.mean(sc, axis=0)
stds[algo][data] = np.std(sc, axis=0)
return avg_scores, stds
def get_max_scores(scores):
avg_scores = {algo: {ds: None for ds in scores[algo]} for algo in scores}
stds = {algo: {ds: None for ds in scores[algo]} for algo in scores}
for algo in scores:
for data in scores[algo]:
sc = scores[algo][data]
if len(sc) > 0:
ml = min(map(len, sc))
sc = [s[:ml] for s in sc]
scores[algo][data] = sc
max_scores = np.max(sc, axis=1)
avg_scores[algo][data] = np.mean(max_scores)
stds[algo][data] = np.std(max_scores)
return avg_scores, stds
def get_last_scores(avg_scores, avg_stds):
last_scores = {
algo: {
ds: avg_scores[algo][ds][-1] if avg_scores[algo][ds] is not None else None
for ds in avg_scores[algo]
}
for algo in avg_scores
}
stds = {
algo: {
ds: avg_stds[algo][ds][-1] if avg_stds[algo][ds] is not None else None
for ds in avg_scores[algo]
}
for algo in avg_scores
}
return last_scores, stds
full_offline_scores = {
algo: {data: None for data in full_scores[algo]} for algo in full_scores
}
full_online_scores = {
algo: {data: None for data in full_scores[algo]} for algo in full_scores
}
regrets = {algo: {data: None for data in full_scores[algo]} for algo in full_scores}
regrets_std = {algo: {data: None for data in full_scores[algo]} for algo in full_scores}
for algo in full_offline_scores:
for data in full_offline_scores[algo]:
full_offline_scores[algo][data] = [s[0] for s in full_scores[algo][data]]
full_online_scores[algo][data] = [s[1] for s in full_scores[algo][data]]
regrets[algo][data] = np.mean([s[2] for s in full_scores[algo][data]])
regrets_std[algo][data] = np.std([s[2] for s in full_scores[algo][data]])
avg_offline_scores, avg_offline_stds = get_average_scores(full_offline_scores)
max_offline_scores, max_offline_stds = get_max_scores(full_offline_scores)
last_offline_scores, last_offline_stds = get_last_scores(
avg_offline_scores, avg_offline_stds
)
avg_online_scores, avg_online_stds = get_average_scores(full_online_scores)
max_online_scores, max_online_stds = get_max_scores(full_online_scores)
last_online_scores, last_online_stds = get_last_scores(
avg_online_scores, avg_online_stds
)
def add_domains_avg(scores):
for algo in scores:
antmaze = [
scores[algo][data]
for data in [
"antmaze-umaze-v2",
"antmaze-umaze-diverse-v2",
"antmaze-medium-play-v2",
"antmaze-medium-diverse-v2",
"antmaze-large-play-v2",
"antmaze-large-diverse-v2",
]
]
adroit = [
scores[algo][data]
for data in [
"pen-cloned-v1",
"door-cloned-v1",
"hammer-cloned-v1",
"relocate-cloned-v1",
]
]
scores[algo]["antmaze avg"] = np.mean(antmaze)
scores[algo]["adroit avg"] = np.mean(adroit)
scores[algo]["total avg"] = np.mean(np.hstack((antmaze, adroit)))
add_domains_avg(last_offline_scores)
add_domains_avg(last_online_scores)
add_domains_avg(regrets)
algorithms = ["AWAC", "CQL", "IQL", "SPOT", "Cal-QL"]
datasets = dataframe["dataset"].unique()
ordered_datasets = [
"antmaze-umaze-v2",
"antmaze-umaze-diverse-v2",
"antmaze-medium-play-v2",
"antmaze-medium-diverse-v2",
"antmaze-large-play-v2",
"antmaze-large-diverse-v2",
"antmaze avg",
"pen-cloned-v1",
"door-cloned-v1",
"hammer-cloned-v1",
"relocate-cloned-v1",
"adroit avg",
"total avg",
]
def get_table(
scores,
stds,
pm="$\\pm$",
delim=" & ",
row_delim="\\midrule",
row_end=" \\\\",
row_begin="",
scores2=None,
stds2=None,
scores_delim=" $\\to$ ",
):
rows = [row_begin + delim.join(["Task Name"] + algorithms) + row_end]
prev_env = "halfcheetah"
for data in ordered_datasets:
env = data.split("-")[0]
if env != prev_env:
if len(row_delim) > 0:
rows.append(row_delim)
prev_env = env
row = [data]
for algo in algorithms:
if data in stds[algo]:
row.append(
f"{scores[algo][data]:.2f} {pm} {stds[algo][data]:.2f}"
+ (
""
if scores2 is None
else f"{scores_delim} {scores2[algo][data]:.2f} {pm} {stds2[algo][data]:.2f}" # noqa
)
)
else:
row.append(
f"{scores[algo][data]:.2f}"
+ (
""
if scores2 is None
else f"{scores_delim} {scores2[algo][data]:.2f}"
)
)
rows.append(row_begin + delim.join(row) + row_end)
return "\n".join(rows)
print(
get_table(
last_offline_scores,
last_offline_stds,
scores2=last_online_scores,
stds2=last_online_stds,
)
)
print()
print(get_table(regrets, regrets_std, "$\\pm$"))
print()
print(
get_table(
last_offline_scores,
last_offline_stds,
"±",
"|",
"",
"|",
"|",
scores2=last_online_scores,
stds2=last_online_stds,
scores_delim=" -> ",
)
)
print()
print(get_table(regrets, regrets_std, "±", "|", "", "|", "|"))
"""# Tunning plots"""
plt.rcParams["figure.figsize"] = (15, 8)
plt.rcParams["figure.dpi"] = 300
sns.set(style="ticks", font_scale=1.5)
# plt.rcParams.update({
# "font.family": "serif",
# "font.serif": "Times New Roman"
# })
# sns.set_palette("tab19")
linestyles = [
("solid", "solid"),
("dotted", (0, (1, 1))),
("long dash with offset", (5, (10, 3))),
("densely dashed", (0, (5, 1))),
("densely dashdotted", (0, (3, 1, 1, 1))),
("densely dashdotdotted", (0, (3, 1, 1, 1, 1, 1))),
]
for data in datasets:
min_score = 1e6
max_score = -1e6
for i, algo in enumerate(algorithms):
if avg_online_scores[algo][data] is not None:
to_draw = avg_online_scores[algo][data]
std_draw = avg_online_stds[algo][data]
if len(to_draw) == 600 or len(to_draw) == 601:
to_draw = to_draw[::3]
std_draw = std_draw[::3]
if len(to_draw) == 1000:
to_draw = to_draw[::5]
std_draw = std_draw[::5]
if len(to_draw) == 3000:
to_draw = to_draw[::15]
std_draw = std_draw[::15]
steps = np.linspace(0, 1, len(to_draw))
min_score = min(min_score, np.min(to_draw))
max_score = max(max_score, np.max(to_draw))
plt.plot(
steps, to_draw, label=algo, linestyle=linestyles[i % len(linestyles)][1]
)
plt.fill_between(steps, to_draw - std_draw, to_draw + std_draw, alpha=0.1)
plt.title(data)
plt.xlabel("Fraction of total tuning steps")
plt.ylabel("Normalized score")
plt.ylim([min_score - 3, max_score + 3])
plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
plt.grid()
plt.savefig(f"out/tuning_{data}.pdf", dpi=300, bbox_inches="tight")
# plt.show()
plt.close()
def convert_dataset_name(name):
name = name.replace("v2", "")
name = name.replace("v1", "")
name = name.replace("v0", "")
name = name.replace("medium-", "m-")
name = name.replace("umaze-", "u-")
name = name.replace("large-", "l-")
name = name.replace("replay-", "re-")
name = name.replace("random-", "ra-")
name = name.replace("expert-", "e-")
name = name.replace("play-", "p-")
name = name.replace("diverse-", "d-")
name = name.replace("human-", "h-")
name = name.replace("cloned-", "c-")
return name[:-1]
def plot_bars(scores, save_name):
agg_l = []
for env in ["antmaze", "pen", "door", "hammer", "relocate"]:
if env in ["halfcheetah", "hopper", "walker2d"]:
datas = ["medium-v2", "medium-expert-v2", "medium-replay-v2"]
elif "maze2d" in env:
datas = ["umaze-v1", "medium-v1", "large-v1"]
elif "antmaze" in env:
datas = [
"umaze-v2",
"umaze-diverse-v2",
"medium-play-v2",
"medium-diverse-v2",
"large-play-v2",
"large-diverse-v2",
]
else:
datas = ["cloned-v1"]
for data in datas:
line = convert_dataset_name(f"{env}-{data}")
for algo in algorithms:
agg_l.append([algo, line, scores[algo][f"{env}-{data}"]])
df_agg = pd.DataFrame(agg_l, columns=["Algorithm", "Dataset", "Normalized Score"])
sns.set(style="ticks", font_scale=2)
plt.rcParams["figure.figsize"] = (20, 10) # (10, 6)
b = sns.barplot(
data=df_agg[df_agg.Dataset.apply(lambda x: "ant" in x)],
x="Dataset",
y="Normalized Score",
hue="Algorithm",
)
# plt.tight_layout()
plt.xticks(fontsize=30)
plt.yticks(fontsize=30)
plt.legend(fontsize=10)
plt.xticks(rotation=45)
sns.move_legend(b, "upper left", bbox_to_anchor=(1, 1))
plt.grid()
plt.savefig(f"out/bars_{save_name}_ant.pdf", dpi=300, bbox_inches="tight")
# plt.show()
plt.close()
b = sns.barplot(
data=df_agg[
df_agg.Dataset.apply(
lambda x: "pen" in x or "hammer" in x or "door" in x or "relocate" in x
)
],
x="Dataset",
y="Normalized Score",
hue="Algorithm",
)
plt.grid()
# plt.tight_layout()
plt.xticks(fontsize=30)
plt.yticks(fontsize=30)
plt.legend(fontsize=10)
plt.xticks(rotation=45)
sns.move_legend(b, "upper left", bbox_to_anchor=(1, 1))
plt.savefig(f"out/bars_{save_name}_adroit.pdf", dpi=300, bbox_inches="tight")
# plt.show()
plt.close()
plot_bars(last_online_scores, "last_online")
plt.rcParams["figure.figsize"] = (10, 6)
plt.rcParams["figure.dpi"] = 300
sns.set(style="ticks", font_scale=0.5)
plt.rcParams.update(
{
# "font.family": "serif",
"font.serif": "Times New Roman"
}
)
def flatten(data):
res = {}
for algo in data:
flat = []
for env in data[algo]:
if "avg" not in env:
env_list = np.array(data[algo][env])[:, -1]
flat.append(env_list)
res[algo] = np.array(flat).T
return res
flat = flatten(full_online_scores)
algorithms = list(flat)
normalized_score_dict = flat
# Human normalized score thresholds
thresholds = np.linspace(-5.0, 150.0, 31)
score_distributions, score_distributions_cis = rly.create_performance_profile(
normalized_score_dict, thresholds
)
# Plot score distributions
fig, ax = plt.subplots(ncols=1, figsize=(7, 5))
# plt.legend()
plot_utils.plot_performance_profiles(
score_distributions,
thresholds,
performance_profile_cis=score_distributions_cis,
colors=dict(zip(algorithms, sns.color_palette("colorblind"))),
xlabel=r"D4RL Normalized Score $(\tau)$",
ax=ax,
legend=True,
)
plt.savefig("out/perf_profiles_online.pdf", dpi=300, bbox_inches="tight")
algorithm_pairs = {}
sns.set(style="ticks", font_scale=0.5)
algs = [
"SPOT",
"CQL",
"IQL",
"AWAC",
]
for a1 in ["Cal-QL"]:
for a2 in algs:
algorithm_pairs[f"{a1},{a2}"] = (flat[a1], flat[a2])
average_probabilities, average_prob_cis = rly.get_interval_estimates(
algorithm_pairs, metrics.probability_of_improvement, reps=200
)
ax = plot_utils.plot_probability_of_improvement(average_probabilities, average_prob_cis)
# ax.set_xlim(0.5, 0.8)
plt.savefig("out/improvement_probability_online.pdf", dpi=300, bbox_inches="tight")