-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrainer.py
138 lines (109 loc) · 3.81 KB
/
trainer.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
"""
file: trainer.py
description: Train a series of caloclf models from a config spec.
"""
import json
import logging
import os
import random
import sys
from itertools import product
from sklearn.model_selection import ParameterGrid
from loaders import load_all_data
from models import (
train_caloclf_model,
build_lagan_style_model,
build_shower_shape_model,
build_densenet_model,
build_raveled_model,
)
if __name__ == "__main__":
logger = logging.getLogger(
"%s.%s"
% (__package__, os.path.splitext(os.path.split(__file__)[-1])[0])
)
logging.basicConfig(level=logging.DEBUG)
cfg = json.load(open(sys.argv[1]))
# {
# "basedir": "./data/",
# "ending": "_angle_position_5deg_xy.h5",
# "jobs": [{
# "data_prefix": "data_",
# "model": {
# "lcn": true,
# "bn": true,
# "dropout_rate": 0.0
# },
# "training": {
# "class_one": "gamma",
# "class_two": "eplus",
# "adam_lr": 0.0001,
# "batch_size": 512
# }
# }]
# }
def safe_to_list(v):
if isinstance(v, list):
return v
return [v]
BASEDIR = cfg["data"]["basedir"]
ENDING = cfg["data"]["ending"]
logger.info(
"Reading from basedir = {}, ending = {}".format(BASEDIR, ENDING)
)
data = {}
for job_id, job in enumerate(cfg["jobs"]):
logger.info("loading job {}/{}".format(job_id + 1, len(cfg["jobs"])))
training_hparams = {
k: safe_to_list(v) for k, v in job["training"].items()
}
model_hparams = {k: safe_to_list(v) for k, v in job["model"].items()}
model_fn = eval(job["model_fn"])
# these should be single lists
CLASS_ONE = training_hparams["class_one"]
CLASS_TWO = training_hparams["class_two"]
assert len(CLASS_ONE) == 1
assert len(CLASS_TWO) == 1
CLASS_ONE = CLASS_ONE[0]
CLASS_TWO = CLASS_TWO[0]
# number of random hyperparameter combinations to choose from
nb_selections = job["random_selection"]
nb_proc = job["proc"]
logger.info("training {} vs. {}".format(CLASS_ONE, CLASS_TWO))
logger.info("checking for prebuilt data...")
key = CLASS_ONE + CLASS_TWO
if key not in data:
logger.info("data not pre-built, making data...")
data[key] = load_all_data(
basedir=BASEDIR,
class_one=CLASS_ONE,
class_two=CLASS_TWO,
ending=ENDING,
)
else:
logger.info("data was pre-built, continuing")
data_train = data[key][job["data_prefix"] + "train"]
data_test = data[key][job["data_prefix"] + "test"]
labels_train = data[key]["labels_train"]
labels_test = data[key]["labels_test"]
t_hparam_grid = ParameterGrid(training_hparams)
m_hparam_grid = ParameterGrid(model_hparams)
logger.info("selecting {} combinations".format(nb_selections))
candidates = list(product(list(t_hparam_grid), list(m_hparam_grid)))
hparam_grid = random.sample(population=candidates, k=nb_selections)
# if nb_proc > 1:
for count, (t_hparams, m_hparams) in enumerate(hparam_grid):
logger.info(
"performing grid element {} of {}".format(
count + 1, nb_selections
)
)
train_caloclf_model(
model_fn=model_fn,
data_train=data_train,
labels_train=labels_train,
data_test=data_test,
labels_test=labels_test,
model_hparams=m_hparams,
training_hparams=t_hparams,
)