-
Notifications
You must be signed in to change notification settings - Fork 0
/
l1m30_bigruNumberbatchen.py
258 lines (221 loc) · 10.6 KB
/
l1m30_bigruNumberbatchen.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 21 15:15:34 2018
@author: ldong
"""
import sys, numpy as np, pandas as pd
from timeit import default_timer as timer
import cPickle as pk
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, GRU, SpatialDropout1D, concatenate
from keras.layers import Bidirectional, GlobalMaxPool1D, BatchNormalization, GlobalAveragePooling1D, PReLU
from keras.models import Model
from keras import initializers, regularizers, constraints, optimizers, layers
from keras.callbacks import EarlyStopping, Callback, ModelCheckpoint
from keras.optimizers import Adam
#num_cores = 32
#from keras import backend
#backend.set_session(backend.tf.Session(config=backend.tf.ConfigProto(inter_op_parallelism_threads=num_cores,\
# intra_op_parallelism_threads=num_cores,\
# device_count={'CPU':num_cores})))
import os
#os.environ['OMP_NUM_THREADS'] = str(num_cores)
import warnings
warnings.filterwarnings('ignore')
from sklearn.metrics import roc_auc_score
class GetBest(Callback):
def __init__(self, trn_data, val_data, val_flag=True,
monitor='val_acc', verbose=0, mode='max', period=1):
super(GetBest, self).__init__()
self.monitor = monitor
self.verbose = verbose
self.period = period
self.best_epochs = 0
self.epochs_since_last_save = 0
self.x = trn_data[0]
self.y = trn_data[1]
self.x_val = val_data[0]
self.y_val = val_data[1]
self.aucs = []
self.val_aucs = []
self.val_flag = val_flag
if mode not in ['auto', 'min', 'max']:
warnings.warn('GetBest mode %s is unknown, '
'fallback to auto mode.' % (mode),
RuntimeWarning)
mode = 'auto'
if mode == 'min':
self.monitor_op = np.less
self.best = np.Inf
elif mode == 'max':
self.monitor_op = np.greater
self.best = -np.Inf
else:
if 'acc' in self.monitor or self.monitor.startswith('fmeasure'):
self.monitor_op = np.greater
self.best = -np.Inf
else:
self.monitor_op = np.less
self.best = np.Inf
def on_train_begin(self, logs=None):
self.best_weights = self.model.get_weights()
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
self.epochs_since_last_save += 1
if self.epochs_since_last_save >= self.period:
self.epochs_since_last_save = 0
#filepath = self.filepath.format(epoch=epoch + 1, **logs)
current = logs.get(self.monitor)
if current is None:
warnings.warn('Can pick best model only with %s available, '
'skipping.' % (self.monitor), RuntimeWarning)
else:
if self.monitor_op(current, self.best):
if self.verbose > 0:
print('\nEpoch %05d: %s improved from %0.5f to %0.5f,'
' storing weights.'
% (epoch + 1, self.monitor, self.best,
current))
self.best = current
self.best_epochs = epoch + 1
self.best_weights = self.model.get_weights()
else:
if self.verbose > 0:
print('\nEpoch %05d: %s did not improve' %
(epoch + 1, self.monitor))
def on_train_end(self, logs=None):
if self.verbose > 0:
print('Using epoch %05d with %s: %0.5f' % (self.best_epochs, self.monitor,
self.best))
self.model.set_weights(self.best_weights)
if self.val_flag == True:
y_pred = self.model.predict(self.x)
roc = roc_auc_score(self.y, y_pred)
y_pred_val = self.model.predict(self.x_val)
roc_val = roc_auc_score(self.y_val, y_pred_val)
print('\rroc-auc: %s - roc-auc_val: %s \n' % (str(round(roc,4)),str(round(roc_val,4))))
self.aucs.append(roc)
self.val_aucs.append(roc_val)
else:
y_pred = self.model.predict(self.x)
roc = roc_auc_score(self.y, y_pred)
print('\rroc-auc: %s \n' % (str(round(roc,4))))
self.aucs.append(roc)
#class roc_callback(Callback):
# def __init__(self,trn_data,val_data):
# self.x = trn_data[0]
# self.y = trn_data[1]
# self.x_val = val_data[0]
# self.y_val = val_data[1]
# self.aucs = []
# self.val_aucs = []
#
# def on_train_end(self, logs={}):
# y_pred = self.model.predict(self.x)
# roc = roc_auc_score(self.y, y_pred)
# y_pred_val = self.model.predict(self.x_val)
# roc_val = roc_auc_score(self.y_val, y_pred_val)
# print('\rroc-auc: %s - roc-auc_val: %s \n' % (str(round(roc,4)),str(round(roc_val,4))))
# self.aucs.append(roc)
# self.val_aucs.append(roc_val)
# return
if __name__ == "__main__":
t0 = timer()
ifold = int(sys.argv[1])
kfold = int(sys.argv[2])
if kfold ==0: epoch_median = ifold
# path = '/work/05313/tg846129/stampede2/jigsaw/data/'
path = '/workspace/ldong/jigsaw/data/'
output_prefix = path+sys.argv[0].split('.')[0]
EMBEDDING_FILE=path+'numberbatch-en-17.06.txt'
TRAIN_DATA_FILE=path+'train.csv'
TEST_DATA_FILE=path+'test.csv'
train = pd.read_csv(TRAIN_DATA_FILE)#.iloc[0:10000]
test = pd.read_csv(TEST_DATA_FILE)#.iloc[0:10000]
# with open(path+'clean_data.pkl', 'r') as f:
# train, test = pk.load(f)
embed_size = 300 # how big is each word vector
max_features = 40000 # how many unique words to use (i.e num rows in embedding vector)
maxlen = 200 # max number of words in a comment to use
train["comment_text"] = train.comment_text.str.lower()
test["comment_text"] = test.comment_text.str.lower()
list_sentences_train = train["comment_text"].fillna("_na_").values
list_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
y = train[list_classes].values
list_sentences_test = test["comment_text"].fillna("_na_").values
tokenizer = Tokenizer(num_words=max_features)
tokenizer.fit_on_texts(list(list_sentences_train))
list_tokenized_train = tokenizer.texts_to_sequences(list_sentences_train)
list_tokenized_test = tokenizer.texts_to_sequences(list_sentences_test)
X_t = pad_sequences(list_tokenized_train, maxlen=maxlen)
X_te = pad_sequences(list_tokenized_test, maxlen=maxlen)
if kfold != 0:
import cPickle as pk
with open(path+'val_flag_'+str(kfold)+'fold.pkl','r') as f:
val_flag = pk.load(f)
ind_trn = np.where(~val_flag[ifold])[0].tolist()
np.random.shuffle(ind_trn)
trn_x, trn_y = X_t[ind_trn], y[ind_trn]
val_x, val_y = X_t[val_flag[ifold]], y[val_flag[ifold]]
else:
trn_x, trn_y = X_t, y
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
embeddings_index = dict(get_coefs(*o.strip().split()) for o in open(EMBEDDING_FILE))
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros([nb_words, embed_size])
for word, i in word_index.items():
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None: embedding_matrix[i] = embedding_vector
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False)(inp)
x = SpatialDropout1D(0.2)(x)
x1 = Bidirectional(GRU(128, return_sequences=True))(x)
x2 = Bidirectional(GRU(64, return_sequences=True))(x)
conc = concatenate([x1, x2])
# x = PReLU()(x)
# x = SpatialDropout1D(0.3)(x)
avg_pool = GlobalAveragePooling1D()(conc)
max_pool = GlobalMaxPool1D()(conc)
x = concatenate([avg_pool, max_pool])
# x = Dense(64, activation='relu')(x)
# x = Dropout(0.2)(x)
x = Dense(6, activation="sigmoid")(x)
print 'Timer before Model: ', timer()-t0
model = Model(inputs=inp, outputs=x)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
early_stop = EarlyStopping(monitor='val_acc', min_delta=1e-6, patience=3)
if kfold != 0:
# callback_roc = roc_callback(trn_data=(trn_x,trn_y), val_data=(val_x,val_y))
callback = GetBest(trn_data=(trn_x,trn_y), val_data=(val_x,val_y),
monitor='val_acc', verbose=1, mode='max', period=1)
History = model.fit(trn_x, trn_y, batch_size=32, epochs=24,
validation_data=(val_x, val_y), callbacks=[callback, early_stop])
y_val = model.predict([val_x], batch_size=1024, verbose=1)
with open(output_prefix+'_y_val_'+str(ifold)+'fold.pkl','w') as f:
pk.dump(y_val, f, protocol=pk.HIGHEST_PROTOCOL)
else:
checkpointer = ModelCheckpoint(filepath='/workspace/ldong/jigsaw/checkpoint/rerun.hdf5', verbose=1)
callback = GetBest(trn_data=(trn_x,trn_y), val_data=(None, None), val_flag=False,
monitor='acc', verbose=1, mode='max', period=1)
History = model.fit(trn_x, trn_y, batch_size=32, epochs=epoch_median, callbacks=[callback])#[checkpointer, callback])
y_test = model.predict([X_te], batch_size=1024, verbose=1)
sample_submission = pd.read_csv(path+'sample_submission.csv')#.iloc[0:10000]
sample_submission[list_classes] = y_test
history = History.history
history['auc'] = callback.aucs
if kfold != 0:
history['val_auc'] = callback.val_aucs
sample_submission.to_csv(output_prefix+'_submission_fold'+str(ifold)+'.csv', index=False)
import cPickle as pk
with open(output_prefix+'_history'+str(ifold)+'.pkl', 'w') as f:
pk.dump(history, f, protocol=pk.HIGHEST_PROTOCOL)
else:
sample_submission.to_csv(output_prefix+'_submission_rerun.csv', index=False)
import cPickle as pk
with open(output_prefix+'_history_rerun.pkl', 'w') as f:
pk.dump(history, f, protocol=pk.HIGHEST_PROTOCOL)
print 'RNN Done!'