-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEMD_Dense_Train.py
139 lines (115 loc) · 4.29 KB
/
EMD_Dense_Train.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
# coding: utf-8
# In[2]:
#import setGPU
import os
import numpy as np
import h5py
import glob
import itertools
import sys
from sklearn.utils import shuffle
import setGPU
# In[12]:
penaltyValue = float(sys.argv[1])
fileIN = "/data/mpierini/qcd_sqrtshatTeV_13TeV_PU40_SIDEBAND_List.h5"
f = h5py.File(fileIN, 'r')
X_train = np.array(f.get("X_train"), dtype=np.float32)
Y_train = np.array(f.get("Y_train"), dtype=np.float32)
X_val = np.array(f.get("X_val"), dtype=np.float32)
Y_val = np.array(f.get("Y_val"), dtype=np.float32)
print(X_train.shape, Y_train.shape)
print(X_val.shape, Y_val.shape)
# keras imports
from keras.models import Model, Sequential
from keras.layers import Dense, Input, Conv2D, Dropout, Flatten, Concatenate, Reshape, BatchNormalization, Activation, Lambda
from keras.layers import MaxPooling2D
from keras.utils import plot_model
from keras import regularizers
from keras import backend as K
from keras import metrics
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, TerminateOnNaN
# In[ ]:
image_shape = (X_train.shape[-3],X_train.shape[-2],X_train.shape[-1])
# In[ ]:
inputImage = Input(shape=(image_shape))
x = BatchNormalization()(inputImage)
#
x = Conv2D(10, kernel_size=(6,1), data_format="channels_first", strides=(1, 1), padding="same",
input_shape=image_shape, kernel_initializer='lecun_uniform')(x)
#x = Dropout(0.2)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
#
x = Conv2D(5, kernel_size=(10,1), data_format="channels_first", strides=(1, 1), padding="same",
input_shape=image_shape, kernel_initializer='lecun_uniform')(x)
#x = Dropout(0.2)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
#
x = Conv2D(2, kernel_size=(5,1), data_format="channels_first", strides=(1, 1), padding="same",
input_shape=image_shape, kernel_initializer='lecun_uniform')(x)
#x = Dropout(0.2)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
#
x = Flatten()(x)
x = Dense(80, activation="relu", kernel_initializer='lecun_uniform', name='dense_relu1')(x)
#x = Dropout(0.2)(x)
#
x = Dense(40, activation="relu", kernel_initializer='lecun_uniform', name='dense_relu2')(x)
#x = Dropout(0.2)(x)
#
x = Dense(20, activation="relu", kernel_initializer='lecun_uniform', name='dense_relu3')(x)
#x = Dropout(0.2)(x)
#
# CUSTOM
# -------
#outputMean = Dense(1, activation='linear', kernel_initializer='lecun_uniform')(x)
#outputSigma = Dense(1, activation='elu', kernel_initializer='lecun_uniform')(x)
#outputSigma = Lambda(lambda x: x + 1.000001)(outputSigma)
#output = Concatenate()([outputMean,outputSigma])
# MSE or MAE
# ----------
output = Dense(1, activation='linear', kernel_initializer='lecun_uniform')(x)
model = Model(inputs=inputImage, outputs=output)
from keras import backend as K
# Gaussian distributed variables
def chisqLoss(x, pars):
mu = pars[:,0]
sigma = pars[:,1]
norm_x = K.tf.divide(x - mu, sigma)
nll_loss = K.log(sigma) + 0.5*K.square(norm_x)
nll_loss = K.mean(nll_loss, axis=-1)
return nll_loss
# In[ ]:
def MAE_AsyLoss(x, xhat):
loss = K.abs(x-xhat)
#penalty = (K.sign(x-xhat)+1)/2.*K.abs(x-xhat)/x
penalty = (K.sign(x-xhat)+1)/2.*penaltyValue*loss
return loss*(1+penalty)
#model.compile(optimizer='adam', loss=chisqLoss)
#model.compile(optimizer='adam', loss='mae')
#model.compile(optimizer='adam', loss='mape')
model.compile(optimizer='adam', loss=MAE_AsyLoss)
model.summary()
# In[ ]:
# train
history = model.fit(X_train, Y_train, epochs=500, batch_size=128, verbose = 2,
validation_data=(X_val, Y_val),
callbacks = [
EarlyStopping(monitor='val_loss', patience=10, verbose=1),
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, verbose=1),
TerminateOnNaN()])
#nameModel = 'EMD_Dense_MAE'
#nameModel = 'EMD_Dense_MAPE'
nameModel = 'EMD_Dense_MAE_AsymmetryLarge_%s' %sys.argv[1]
# store history
f = h5py.File("models/%s_history.h5" %nameModel, "w")
f.create_dataset("training_loss", data=np.array(history.history['loss']),compression='gzip')
f.create_dataset("validation_loss", data=np.array(history.history['val_loss']),compression='gzip')
f.close()
# store model
model_json = model.to_json()
with open("models/%s.json" %nameModel, "w") as json_file:
json_file.write(model_json)
model.save_weights("models/%s.h5" %nameModel)