-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_simulation_deconstruct.py
More file actions
146 lines (127 loc) · 4.02 KB
/
plot_simulation_deconstruct.py
File metadata and controls
146 lines (127 loc) · 4.02 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
import os
import argparse
import sys
import logging
import json
import glob
import copy
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use("Agg")
from matplotlib import pyplot as plt
import seaborn as sns
from evaluate_model import load_plain_nn, load_easier_net
STUPID_FILE_MAP = {
"test_ensemble_easier_net.csv": "res_easier_net.json",
"test_ensemble_sparse.csv": "res_sparse.json",
"test_ensemble_dropout.csv": "res_dropout.json",
"test_dropout.csv": "res_dropout.json",
"test_sparse.csv": "res_sparse.json",
"test_easier_net.csv": "res_easier_net.json",
}
def parse_args(args):
""" parse command line arguments """
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--sparse-single", type=str,
)
parser.add_argument(
"--sier-net", type=str,
)
parser.add_argument(
"--dropout-single", type=str,
)
parser.add_argument(
"--sparse-ensemble", type=str,
)
parser.add_argument(
"--easier-net", type=str,
)
parser.add_argument(
"--dropout-ensemble", type=str,
)
parser.add_argument(
"--out-file", type=str,
)
parser.set_defaults()
args = parser.parse_args()
return args
def load_network_struct(res_file: str):
res_file_split = res_file.rsplit("/", 1)
if "seed_init" not in res_file:
res_json_files = glob.glob(
os.path.join(
res_file_split[0], "seed_init_*", STUPID_FILE_MAP[res_file_split[1]]
)
)
assert len(res_json_files)
res_dicts = []
for res_json_file in res_json_files:
with open(res_json_file, "r") as f:
res_json = json.load(f)
res_dicts.append(
{
"max_layer": res_json["max_layer"],
"hidden_size_avg": res_json["hidden_size_avg"],
"support_size": res_json["support_size"],
}
)
return pd.DataFrame(pd.DataFrame(res_dicts).mean()).T
else:
res_json_file = os.path.join(
res_file_split[0], STUPID_FILE_MAP[res_file_split[1]]
)
with open(res_json_file, "r") as f:
res_json = json.load(f)
print(res_json_file)
print(res_json)
return pd.DataFrame(
[
{
"max_layer": res_json["max_layer"],
"hidden_size_avg": res_json["hidden_size_avg"],
"support_size": res_json["support_size"],
}
]
)
def get_best(res_files, model_str):
val_res_template = res_files.replace("test", "val")
val_res_files = glob.glob(val_res_template)
print(val_res_template)
print(model_str, "num files found", len(val_res_files))
val_res = pd.concat(
[pd.read_csv(f, index_col=0) for f in val_res_files]
).reset_index(drop=True)
best_idx = np.argmin(val_res.test_loss)
best_res_file = val_res_files[best_idx].replace("val_", "test_")
test_loss = pd.read_csv(best_res_file, index_col=0).test_loss[0]
res = pd.DataFrame(
[
{
"model": model_str,
"test_loss": test_loss,
"val_loss": val_res.test_loss[best_idx],
}
]
)
network_struct = load_network_struct(best_res_file)
res = pd.concat([res, network_struct], axis=1)
print(best_res_file)
return res
def main(args=sys.argv[1:]):
args = parse_args(args)
res = pd.concat(
[
get_best(args.dropout_single, "Dropout-Single"),
get_best(args.dropout_ensemble, "Dropout-Ensemble"),
get_best(args.sparse_single, "Sparse-Single"),
get_best(args.sparse_ensemble, "Sparse-Ensemble"),
get_best(args.sier_net, "SIER-net"),
get_best(args.easier_net, "EASIER-net"),
]
)
print(res)
res.to_latex(args.out_file, index=False)
if __name__ == "__main__":
main(sys.argv[1:])