-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn_model_test.py
307 lines (223 loc) · 10.2 KB
/
nn_model_test.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from keras.models import Sequential
from keras.layers import Dense, Dropout, Lambda
from keras.optimizers import RMSprop, Adam
from keras.constraints import maxnorm
from keras.models import load_model
from keras import backend as K
import tensorflow as tf
from keras.callbacks import TensorBoard
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
import numpy as np
# import matplotlib.pyplot as plt
import bcubed
# from tensorflow.python import debug as tf_debug
# from generate_full_dataset import generate_full_dataset
PROB_THRESHOLD = 0.7
class Model:
def __init__(self, mode='pretrain', filepath=None):
self.mode = mode
if mode == 'test':
self.model = load_model(filepath, custom_objects={'max_margin_loss': max_margin_loss})
else:
self.model = self.set()
if mode == 'train':
self.model.load_weights(filepath)
self.compile()
def set(self):
model = Sequential()
# model.add(Dropout(0.3, input_shape=(1238,))) # makes loss worse
model.add(Dense(units=500, kernel_initializer='normal', activation='relu', input_dim=1238,
kernel_constraint=maxnorm(3)))
# model.add(Dense(units=1000, kernel_initializer='normal', activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.3))
model.add(Dense(units=250, kernel_initializer='normal', activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=100, kernel_initializer='normal', activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.3))
model.add(Dense(units=1, kernel_initializer='normal', activation='sigmoid'))
# if self.mode == 'train':
# # dummy = tf.expand_dims(tf.constant([0.]), 0)
# # dummy = tf.constant([0.])
# model.add(Lambda(lambda x: tf.stack([x, tf.constant([0.], shape=x.get_shape())])))
return model
def compile(self):
optimizer = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, decay=0.0)
loss = 'binary_crossentropy'
if self.mode == 'train':
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-07, decay=0.0)
loss = max_margin_loss
self.model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
print('Model is compiled. Start training.')
def train(self, x_train, y_train, batch_size, epochs):
# tensorboard = TensorBoard(log_dir='/output/logs', histogram_freq=2, batch_size=100, write_graph=False,
# write_grads=True, write_images=False, embeddings_freq=0,
# embeddings_layer_names=None, embeddings_metadata=None)
# Debugging
# K.set_session(tf_debug.LocalCLIDebugWrapperSession(tf.Session()))
history = self.model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
validation_split=0.2, shuffle=False, verbose=2)
# if self.mode == 'pretrain':
# self.model.save('/output/CRModel.h5')
# self.model.save_weights('/output/CRModel_weights.h5')
# else:
# self.model.save('/output/CRModel_final.h5')
def predict(self, x_test):
return self.model.predict(x_test)
# Computes the loss on test data
def evaluate(self, x_test, y_test):
return self.model.evaluate(x_test, y_test)
def estimate_metrics(self, x_test, y_test):
labels_pred = self.predict(x_test)
labels_pred = (labels_pred > PROB_THRESHOLD)
print('Labels predicted: ', str(labels_pred))
accuracy = metrics.accuracy_score(y_test, labels_pred)
metric = metrics.precision_recall_fscore_support(y_test, labels_pred)
print('Accuracy is ' + str(accuracy))
print('Metrics (precision, recall, f score, support) is ' + str(metric))
def bcubed(self, x_test, y_test):
ldict = {}
cdict = {}
labels_pred = self.predict(x_test)
labels_pred = (labels_pred > PROB_THRESHOLD)
for i, label in enumerate(y_test):
ldict[i] = {int(label)}
cdict[i] = {int(labels_pred[i])}
precision = bcubed.precision(cdict, ldict)
recall = bcubed.recall(cdict, ldict)
fscore = bcubed.fscore(precision, recall)
print('B-cubed metric:\nPrecision = {}\nRecall = {}\nF-score = {}'.format(precision, recall, fscore))
def auc(labels, predictions):
print('labels: {}'.format(labels[:, 0]))
print('predictions: {}'.format(predictions))
return tf.metrics.auc(labels[:, 0], predictions)
def precision_at_thresholds(labels, predictions):
return tf.metrics.precision_at_thresholds(labels[:, 0], predictions, thresholds=[0.3, 0.5, 0.7])
# def b_cubed(y_true, y_pred):
# ldict = {}
# cdict = {}
#
# y_predicted = K.less(PROB_THRESHOLD, y_pred)
# print('y_predicted: {}'.format(y_predicted))
#
# for i, label in enumerate(y_true):
# ldict[i] = {int(label)}
# cdict[i] = {int(y_predicted[i])}
#
# precision = bcubed.precision(cdict, ldict)
# recall = bcubed.recall(cdict, ldict)
# fscore = bcubed.fscore(precision, recall)
#
# return fscore
# LOSS FUNCTION
def max_margin_loss(y_true, y_pred):
k = tf.shape(y_true)[0]
mentions_sorted, indices_sorted = tf.nn.top_k(y_true[:, 1], k, sorted=True)
# mentions_sorted = tf.Print(mentions_sorted, [mentions_sorted], "mentions_sorted:", first_n=1, summarize=100)
# print('Mentions_sorted: {}'.format(mentions_sorted))
labels_sorted = K.gather(y_true[:, 0], indices_sorted)
# labels_sorted = tf.Print(labels_sorted, [labels_sorted], "labels_sorted:", first_n=1, summarize=100)
# print('Labels_sorted: {}'.format(labels_sorted))
scores_sorted = K.gather(y_pred[:, 0], indices_sorted)
# scores_sorted = tf.Print(scores_sorted, [scores_sorted], "scores_sorted:", first_n=1, summarize=100)
# print('Scores_sorted: {}'.format(scores_sorted))
return max_margin(labels_sorted, mentions_sorted, scores_sorted)
def max_margin(labels, mentions, scores):
batch = 100
def different(start, end):
indices = tf.range(start, end, delta=1)
# indices = tf.Print(indices, [tf.gather(mentions, indices)], 'indices: ', first_n=50, summarize=40)
labels_to_process = tf.gather(labels, indices)
predictions_to_process = tf.gather(scores, indices)
return process_antecedents(labels_to_process, predictions_to_process)
def mention_group_loss(i, prev_mention, i_start):
condition = tf.equal(tf.gather(mentions, i), prev_mention)
m_loss = tf.cond(condition, lambda: K.constant(0.), lambda: different(i_start, i))
return m_loss
def set_i_start(i, prev_mention, i_start):
condition = tf.equal(tf.gather(mentions, i), prev_mention)
return tf.cond(condition, lambda: i_start, lambda: i)
i0 = tf.constant(0)
prev_mention0 = tf.gather(mentions, i0)
i_start0 = tf.constant(0)
loss0 = tf.constant(0.)
c = lambda i, prev_mention, i_start, loss: tf.less(i, batch)
b = lambda i, prev_mention, i_start, loss: [i + 1,
tf.gather(mentions, i),
set_i_start(i, prev_mention, i_start),
loss + mention_group_loss(i, prev_mention, i_start)]
loop = tf.while_loop(c, b, [i0, prev_mention0, i_start0, loss0], name='while_loop')
loss = loop[3]
return loss
def process_antecedents(labels, predictions):
penalty = 1.0
true_scores = labels * predictions
highest_true_score = K.max(true_scores)
penalties = penalty * (1 - labels)
return K.max(penalties * (1 + predictions - highest_true_score))
###
def preprocess_dataset(dataset, mode):
"""
Load, shuffle and split dataset into train and test ones.
:return:
"""
# np.random.shuffle(dataset)
n = 10000
data = dataset[:n, :-2]
if mode == 'pretrain':
labels = dataset[:n, -2]
else:
labels = dataset[:n, -2:]
return train_test_split(data, labels, test_size=0.05, shuffle=False)
def pipeline(batch_size, epochs, mode='pretrain', filepath=None):
# dataset = generate_full_dataset()
# print('Dataset is generated.')
dataset = np.load('./data/dataset/full_dataset_no_duplicates.npy')
print('Dataset is loaded.')
data_train, data_test, labels_train, labels_test = preprocess_dataset(dataset, mode)
print('Dataset is preprocessed.')
model = Model(mode=mode, filepath=filepath)
model.train(data_train, labels_train, batch_size, epochs)
#
# print('Evaluation:')
# model.estimate_metrics(data_test, labels_test)
# np.save('/output/data_test.npy', np.c_[data_test, labels_test])
def evaluate_model(test_size=None):
# Download from Floyd datasets through /data/ folder
model = Model('/dataset/CRModel.h5')
print('Model is downloaded.')
dataset = np.load('/dataset/data_test.npy')
if test_size != None:
dataset = dataset[:test_size]
np.random.shuffle(dataset)
data_test = dataset[:, :-1]
labels_test = dataset[:, -1]
print('Dataset for testing is ready.')
print('ESTIMATE MODEL')
model.estimate_metrics(data_test, labels_test)
model.bcubed(data_test, labels_test)
def main():
# Pre-training
# pipeline(batch_size=100, epochs=20)
# Training with max-margin loss
pipeline(batch_size=100, epochs=1, mode='train', filepath='CRModel_weights.h5')
# evaluate_model()
def check():
s = tf.constant(0)
e = tf.constant(3)
arr = tf.constant([0, 1, 2, 3, 4, 5])
r = tf.range(s, e, delta=1)
res = tf.gather(arr, r)
with tf.Session() as sess:
print(sess.run(res))
def check_count(lab, pred):
labels = np.array([int(l) for l in lab.split()])
predictions = np.array([float(p) for p in pred.split()])
true_scores = labels * predictions
highest_true_score = np.max(true_scores)
penalties = 1.0 * (1 - labels)
result_arr = penalties * (1 + predictions - highest_true_score)
result_max = np.max(result_arr)
print("Result array: {}\nMax loss: {}".format(result_arr, result_max))
if __name__ == '__main__':
main()