-
Notifications
You must be signed in to change notification settings - Fork 2
/
qkerasModel.py
194 lines (160 loc) · 7.02 KB
/
qkerasModel.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
import h5py
import numpy as np
import argparse
import tensorflow
import matplotlib.pyplot as plt
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv1D, Dense, Flatten, Input, GlobalAveragePooling1D
#from dataForgeScripts.dataForge import N_FEAT, N_PART_PER_JET
from qkeras import *
from tensorflow.keras.regularizers import l1
from tensorflow_model_optimization.python.core.sparsity.keras import prune
from tensorflow_model_optimization.python.core.sparsity.keras import pruning_callbacks
from tensorflow_model_optimization.python.core.sparsity.keras import pruning_schedule
import tensorflow_model_optimization as tfmot
N_FEAT = 14
N_PART_PER_JET = 10
def main(args):
signalTrainFile = args.SignalTrainFile
bkgTrainFile = args.BkgTrainFile
sig_jetData_TrainFile = args.sig_jetData_TrainFile
bkg_jetData_TrainFile = args.bkg_jetData_TrainFile
print("Reading signal from " + signalTrainFile)
print("Reading background from " + bkgTrainFile)
print("Reading signal jet data from " + sig_jetData_TrainFile)
print("Reading background jet data from " + bkg_jetData_TrainFile)
with h5py.File(signalTrainFile, "r") as hf:
dataset = hf["Training Data"][:]
with h5py.File(bkgTrainFile, "r") as hf:
datasetQCD = hf["Training Data"][:]
with h5py.File(sig_jetData_TrainFile, "r") as hf:
sampleData = hf["Sample Data"][:]
with h5py.File(bkg_jetData_TrainFile, "r") as hf:
sampleDataQCD = hf["Sample Data"][:]
"""" I am combining the features with jet data in order to shuffle all at
at once. This way, I will still have a 1-1 correspondance of jets and data
after shuffling. """
dataset = np.concatenate((dataset, datasetQCD))#Put datasets on top of one another
sampleData = np.concatenate((sampleData,sampleDataQCD))
fullData = np.concatenate((dataset, sampleData), axis=1)
np.random.shuffle(fullData) #randomize QCD and Stop samples
dataset = fullData[0:,0:141]
sampleData = fullData[0:,141:]
# Separate datasets into inputs and outputs, expand the dimensions of the inputs to be used with Conv1D layers
X = dataset[:, 0 : len(dataset[0]) - 1]
y = dataset[:, len(dataset[0]) - 1]
X = X.reshape((X.shape[0], N_PART_PER_JET, N_FEAT))
# Establish the sample weights
thebins = np.linspace(0, 500, 20) # check for right range
bkgPts = sampleData[y==0][:,0]
sigPts = sampleData[y==1][:,0]
bkg_counts, _ = np.histogram(bkgPts, bins=thebins)
sig_counts, _ = np.histogram(sigPts, bins=thebins)
total_bkg = len(bkgPts)
total_sig = len(sigPts)
weights_pt = np.nan_to_num(sig_counts / bkg_counts, nan=total_sig / total_bkg)
# Compile the network
x = inputs = Input(shape=(10, 14), name="input_1")
x = QConv1D(
filters=10,
kernel_size=1,
strides=1,
kernel_quantizer=quantized_bits(8, 3, alpha=1),
bias_quantizer=quantized_bits(8, 3, alpha=1),
kernel_initializer="lecun_uniform",
kernel_regularizer=l1(0.0001),
bias_regularizer=l1(0.0001),
name="q_conv1d",
)(x)
x = QActivation(activation=quantized_relu(8), name="q_activation")(x)
x = QConv1D(
filters=10,
kernel_size=1,
strides=1,
kernel_quantizer=quantized_bits(8, 3, alpha=1),
bias_quantizer=quantized_bits(8, 3, alpha=1),
kernel_initializer="lecun_uniform",
kernel_regularizer=l1(0.0001),
bias_regularizer=l1(0.0001),
name="q_conv1d_1",
)(x)
x = QActivation(activation=quantized_relu(8), name="q_activation_1")(x)
x = GlobalAveragePooling1D(name="global_average_pooling1d")(x)
x = QDense(
10,
kernel_quantizer=quantized_bits(8, 3, alpha=1),
bias_quantizer=quantized_bits(8, 3, alpha=1),
kernel_initializer="lecun_uniform",
kernel_regularizer=l1(0.0001),
bias_regularizer=l1(0.0001),
name="q_dense",
)(x)
x = QActivation(activation=quantized_relu(8), name="q_activation_2")(x)
x = QDense(
1,
kernel_quantizer=quantized_bits(8, 3, alpha=1),
bias_quantizer=quantized_bits(8, 3, alpha=1),
kernel_initializer="lecun_uniform",
kernel_regularizer=l1(0.0001),
bias_regularizer=l1(0.0001),
name="q_dense_1",
)(x)
outputs = Activation(activation="sigmoid", name="sigmoid")(x)
model = Model(inputs=inputs, outputs=outputs, name="model")
#Pruning Step
pruning_params = {
"pruning_schedule":
pruning_schedule.ConstantSparsity(0.75, begin_step=2000, frequency=100)
}
full_prune = True
if full_prune:
model = prune.prune_low_magnitude(model, **pruning_params)
initial_learning_rate = 0.001
lr_schedule = tensorflow.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate,
decay_steps=100000,
decay_rate=0.96,
staircase=True)
"""The line below compiles the model with the learning schedule"""
#model.compile(loss="binary_crossentropy", optimizer=tensorflow.keras.optimizers.Adam(
#learning_rate=lr_schedule), metrics=["binary_accuracy"])
"""The line below uses the default Adam learning rate"""
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["binary_accuracy"])
# Add in the sample weights, 1-to-1 correspondence with training data
# Sample weight of all signal events being equal to 1
# Sample weight of all background events being equal to the sig/bkg ratio at that jet's pT
weights = np.ones(len(y))
pt_indicies = np.clip(np.digitize(sampleData[:,0], bins=thebins) - 1, 0, len(weights_pt) - 1)
weights[y==0] = weights_pt[pt_indicies][y==0]
# Train the network
callbacks = [tensorflow.keras.callbacks.EarlyStopping(monitor="val_loss", verbose=1, patience=10),
pruning_callbacks.UpdatePruningStep()]
history=model.fit(
X,
y,
epochs=200,
batch_size=50,
verbose=2,
sample_weight=np.asarray(weights),
validation_split=0.20,
callbacks=[callbacks],
)
plt.figure(figsize=(7,5), dpi=120)
plt.plot(history.history['loss'], label = 'Train')
plt.plot(history.history['val_loss'], label = 'Validation')
plt.title('Model Loss', fontsize=25)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(loc='best')
plt.savefig("qkModLoss.png")
model = tfmot.sparsity.keras.strip_pruning(model)
# Save the network
model.save("qkL1JetTagModel.h5")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process arguments")
parser.add_argument("SignalTrainFile", type=str, help="input the signal train file")
parser.add_argument("BkgTrainFile", type=str, help="input the signal train file")
parser.add_argument("sig_jetData_TrainFile", type=str, help="input signal jet data of the form ...sampleData.h5")
parser.add_argument("bkg_jetData_TrainFile", type=str, help="input the bkg jet data of form ...sampleDataQCD.h5 for example")
args = parser.parse_args()
main(args)