-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunfold.py
136 lines (107 loc) · 5.14 KB
/
unfold.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
from __future__ import absolute_import, division, print_function
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
import tensorflow.keras
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Dense, Input, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
def weighted_binary_crossentropy(y_true, y_pred):
weights = tf.gather(y_true, [1], axis=1) # event weights
y_true = tf.gather(y_true, [0], axis=1) # actual y_true for loss
# Clip the prediction value to prevent NaN's and Inf's
epsilon = K.epsilon()
y_pred = K.clip(y_pred, epsilon, 1. - epsilon)
t_loss = -weights * ((y_true) * K.log(y_pred) +
(1 - y_true) * K.log(1 - y_pred))
return K.mean(t_loss)
def multifold(num_observables, iterations, theta0_G, theta0_S,
theta_unknown_S,weights_MC_sim=None,weights_MC_data=None,verbose=1):
if weights_MC_sim is None:
weights_MC_sim = np.ones(len(theta0_S))
if weights_MC_data is None:
weights_MC_data = np.ones(len(theta_unknown_S))
theta0 = np.stack([theta0_G, theta0_S], axis=1)
labels0 = np.zeros(len(theta0))
theta_unknown = np.stack([theta_unknown_S, theta_unknown_S], axis=1)
labels1 = np.ones(len(theta0_G))
labels_unknown = np.ones(len(theta_unknown_S))
xvals_1 = np.concatenate((theta0_S, theta_unknown_S))
yvals_1 = np.concatenate((labels0, labels_unknown))
xvals_2 = np.concatenate((theta0_G, theta0_G))
yvals_2 = np.concatenate((labels0, labels1))
weights = np.empty(shape=(iterations, 2, len(theta0_G)))
models = {}
inputs = Input((num_observables, ))
hidden_layer_1 = Dense(50, activation='relu')(inputs)
dropoutlayer = Dropout(0.1)(hidden_layer_1)
hidden_layer_2 = Dense(50, activation='relu')(dropoutlayer)
hidden_layer_3 = Dense(50, activation='relu')(hidden_layer_2)
outputs = Dense(1, activation='sigmoid')(hidden_layer_3)
model = Model(inputs=inputs, outputs=outputs)
earlystopping = EarlyStopping(patience=10,
verbose=verbose,
restore_best_weights=True)
# from NN (DCTR)
def reweight(events):
f = model.predict(events, batch_size=5000)
weights = f / (1. - f)
return np.squeeze(np.nan_to_num(weights))
weights_pull = weights_MC_sim
weights_push = weights_MC_sim
#weights_pull = np.ones(len(theta0_S))
#weights_push = np.ones(len(theta0_S))
history = {}
history['step1'] = []
history['step2'] = []
for i in range(iterations):
print("ITERATION: {}".format(i + 1))
print("STEP 1...")
weights_1 = np.concatenate((weights_push,weights_MC_data ))
X_train_1, X_test_1, Y_train_1, Y_test_1, w_train_1, w_test_1 = train_test_split(
xvals_1, yvals_1, weights_1)
Y_train_1 = np.stack((Y_train_1, w_train_1), axis=1)
Y_test_1 = np.stack((Y_test_1, w_test_1), axis=1)
batch_size=200
model.compile(loss=weighted_binary_crossentropy,
optimizer='Adam',
metrics=['accuracy'])
hist_s1 = model.fit(X_train_1[X_train_1[:,0]!=-10],
Y_train_1[X_train_1[:,0]!=-10],
epochs=1000,
batch_size=batch_size,
validation_data=(X_test_1[X_test_1[:,0]!=-10], Y_test_1[X_test_1[:,0]!=-10]),
callbacks=[earlystopping],
verbose=verbose)
history['step1'].append(hist_s1)
weights_pull = weights_push * reweight(theta0_S)
weights_pull[theta0_S[:,0]==-10] = 1
weights[i, :1, :] = weights_pull
models[i, 1] = model.get_weights()
print("STEP 2...")
weights_2 = np.concatenate((weights_MC_sim, weights_pull))
#weights_2 = np.concatenate((np.ones(len(theta0_G)), weights_pull))
# ones for MC Truth (not MC weights), actual weights for (reweighted) MC Truth
X_train_2, X_test_2, Y_train_2, Y_test_2, w_train_2, w_test_2 = train_test_split(
xvals_2, yvals_2, weights_2)
# zip ("hide") the weights with the labels
Y_train_2 = np.stack((Y_train_2, w_train_2), axis=1)
Y_test_2 = np.stack((Y_test_2, w_test_2), axis=1)
model.compile(loss=weighted_binary_crossentropy,
optimizer='Adam',
metrics=['accuracy'])
hist_s2 = model.fit(X_train_2[X_train_2[:,0]!=-10],
Y_train_2[X_train_2[:,0]!=-10],
epochs=1000,
batch_size=batch_size,
validation_data=(X_test_2[X_test_2[:,0]!=-10], Y_test_2[X_test_2[:,0]!=-10]),
callbacks=[earlystopping],
verbose=verbose)
history['step2'].append(hist_s2)
#weights_push = reweight(theta0_G)
weights_push = weights_MC_sim * reweight(theta0_G)
weights[i, 1:2, :] = weights_push
models[i, 2] = model.get_weights()
return weights, models, history