/
add_best_model_to_bayes_search.py
67 lines (55 loc) · 1.87 KB
/
add_best_model_to_bayes_search.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
import argparse
import pandas
import torch
import math
import os
from shutil import copyfile
from copy import deepcopy
if __name__ == "__main__":
# parse args
parser = argparse.ArgumentParser()
parser.add_argument(
"--checkpoint",
type=str,
required=True,
help="location of checkpoint file of bayes search",
)
parser.add_argument(
"--best_model",
type=str,
required=True,
help="location of dumped best model",
)
args, _ = parser.parse_known_args()
# load checkpoint file
cp = torch.load(args.checkpoint)
# load dumped best model
bm = pandas.read_csv(args.best_model, index_col=0)
bm = pandas.DataFrame(bm).T
# backup original checkpoint file if not done already
if not os.path.isfile(args.checkpoint + ".bak"):
copyfile(args.checkpoint, args.checkpoint + ".bak")
print("Original checkpoint file stored in {}.bak".format(args.checkpoint))
# add best model to trials
cp["parameters"].append(deepcopy(cp["parameters"][-1]))
cp["results"].append(None)
for key in cp["parameters"][-1]:
param_type = type(cp["parameters"][-1][key])
if isinstance(bm[key][0], float) and math.isnan(bm[key][0]):
cp["parameters"][-1][key] = ''
else:
if param_type == int:
cp["parameters"][-1][key] = param_type(float(bm[key][0]))
elif param_type == bool:
if bm[key][0].lower() == 'false':
cp["parameters"][-1][key] = False
else:
cp["parameters"][-1][key] = True
else:
cp["parameters"][-1][key] = param_type(bm[key][0])
torch.save(cp, args.checkpoint)
# done
print("Added best model settings from {} as a trial in {}".format(
args.best_model, args.checkpoint
)
)