-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtrain_event_4v.py
169 lines (135 loc) · 5.43 KB
/
train_event_4v.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
import autograd as ag
import click
import copy
import numpy as np
import logging
import pickle
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import RobustScaler
from sklearn.utils import check_random_state
from recnn.recnn import log_loss
from recnn.recnn import adam
from recnn.recnn import event_baseline_init
from recnn.recnn import event_baseline_predict
logging.basicConfig(level=logging.INFO,
format="[%(asctime)s %(levelname)s] %(message)s")
@click.command()
@click.argument("filename_train")
@click.argument("filename_model")
@click.argument("n_events")
@click.option("--n_features_rnn", default=4)
@click.option("--n_hidden_rnn", default=40)
@click.option("--n_epochs", default=20)
@click.option("--batch_size", default=64)
@click.option("--step_size", default=0.0005)
@click.option("--decay", default=0.9)
@click.option("--n_particles_per_event", default=10)
@click.option("--random_state", default=1)
def train(filename_train,
filename_model,
n_events,
n_features_rnn=4,
n_hidden_rnn=40,
n_epochs=5,
batch_size=64,
step_size=0.01,
decay=0.7,
n_particles_per_event=10,
random_state=1):
# Initialization
n_events = int(n_events)
logging.info("Calling with...")
logging.info("\tfilename_train = %s" % filename_train)
logging.info("\tfilename_model = %s" % filename_model)
logging.info("\tn_events = %d" % n_events)
logging.info("\tn_features_rnn = %d" % n_features_rnn)
logging.info("\tn_hidden_rnn = %d" % n_hidden_rnn)
logging.info("\tn_epochs = %d" % n_epochs)
logging.info("\tbatch_size = %d" % batch_size)
logging.info("\tstep_size = %f" % step_size)
logging.info("\tdecay = %f" % decay)
logging.info("\tn_particles_per_event = %d" % n_particles_per_event)
logging.info("\trandom_state = %d" % random_state)
rng = check_random_state(random_state)
# Make data
logging.info("Loading data + preprocessing...")
fd = open(filename_train, "rb")
# training file is assumed to be formatted a sequence of pickled pairs
# (e_i, y_i), where e_i is a list of (phi, eta, pt, mass) tuples.
X = []
y = []
for i in range(n_events):
v_i, y_i = pickle.load(fd)
v_i = v_i[:n_particles_per_event] # truncate to the top particles
X.append(v_i)
y.append(y_i)
y = np.array(y)
fd.close()
logging.info("\tfilename = %s" % filename_train)
logging.info("\tX size = %d" % len(X))
logging.info("\ty size = %d" % len(y))
# Preprocessing
logging.info("Preprocessing...")
tf_features = RobustScaler().fit(
np.vstack([features for features in X]))
for i in range(len(X)):
X[i] = tf_features.transform(X[i])
if len(X[i]) < n_particles_per_event:
X[i] = np.vstack([X[i],
np.zeros((n_particles_per_event - len(X[i]), 4))])
# Split into train+test
logging.info("Splitting into train and validation...")
X_train, X_valid, y_train, y_valid = train_test_split(X, y,
test_size=1000,
stratify=y,
random_state=rng)
# Training
logging.info("Training...")
predict = event_baseline_predict
init = event_baseline_init
trained_params = init(n_features_rnn, n_hidden_rnn,
random_state=rng)
n_batches = int(np.ceil(len(X_train) / batch_size))
best_score = [-np.inf] # yuck, but works
best_params = [trained_params]
def loss(X, y, params):
y_pred = predict(params, X,
n_particles_per_event=n_particles_per_event)
l = log_loss(y, y_pred).mean()
return l
def objective(params, iteration):
rng = check_random_state(iteration)
start = rng.randint(len(X_train) - batch_size)
idx = slice(start, start+batch_size)
return loss(X_train[idx], y_train[idx], params)
def callback(params, iteration, gradient):
if iteration % 25 == 0:
roc_auc = roc_auc_score(y_valid,
predict(params, X_valid,
n_particles_per_event=n_particles_per_event))
if roc_auc > best_score[0]:
best_score[0] = roc_auc
best_params[0] = copy.deepcopy(params)
fd = open(filename_model, "wb")
pickle.dump(best_params[0], fd)
fd.close()
logging.info(
"%5d\t~loss(train)=%.4f\tloss(valid)=%.4f"
"\troc_auc(valid)=%.4f\tbest_roc_auc(valid)=%.4f" % (
iteration,
loss(X_train[:5000], y_train[:5000], params),
loss(X_valid, y_valid, params),
roc_auc,
best_score[0]))
for i in range(n_epochs):
logging.info("epoch = %d" % i)
logging.info("step_size = %.4f" % step_size)
trained_params = adam(ag.grad(objective),
trained_params,
step_size=step_size,
num_iters=1 * n_batches,
callback=callback)
step_size = step_size * decay
if __name__ == "__main__":
train()