-
Notifications
You must be signed in to change notification settings - Fork 16
/
main_elmo.py
200 lines (168 loc) · 6.33 KB
/
main_elmo.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
from scripts.util import read_file, tokenize
from sklearn.model_selection import train_test_split
from keras.callbacks import EarlyStopping, ModelCheckpoint
from scripts.rnn import RNNKeras, RNNKerasCPU, LSTMKeras, SARNNKerasCPU, SARNNKeras
from scripts.cnn import TextCNN, LSTMCNN, VDCNN
import argparse
import os
import numpy as np
import datetime
import pandas as pd
from scripts.util import find_threshold
from sklearn.metrics import f1_score
from keras.utils import Sequence
from elmoformanylangs import Embedder
def train_model(model, embedding_path, should_find_threshold, return_prob, use_additive_emb):
batch_size = 16
epochs = 100
max_len = 100
def to_length(texts, length):
def pad_func(vector, pad_width, iaxis, kwargs):
str = kwargs.get('padder', '<pad>')
vector[:pad_width[0]] = str
vector[-pad_width[1]:] = str
return vector
ret = []
for sentence in texts:
sentence = np.array([token.replace("_", " ") for token in sentence], dtype=np.unicode)
sentence = sentence[:min(length, len(sentence))]
if length > len(sentence):
sentence = np.pad(
sentence, mode=pad_func,
pad_width=(0, length - len(sentence))
)
ret.append(sentence)
return np.array(ret)
class TrainSeq(Sequence):
def __init__(self, X, y, batch_size, elmo):
self._X, self._y = X, y
self._batch_size = batch_size
self._indices = np.arange(len(self._X))
self._elmo = elmo
def __len__(self):
return int(np.ceil(len(self._X) / float(self._batch_size)))
def __getitem__(self, idx):
id = self._indices[idx * self._batch_size:(idx + 1) * self._batch_size]
return np.array(self._elmo.sents2elmo(self._X[id])), self._y[id]
def on_epoch_end(self):
np.random.shuffle(self._indices)
class TestSeq(Sequence):
def __init__(self, x, batch_size, elmo):
self._X = x
self._batch_size = batch_size
self._elmo = elmo
def __len__(self):
return int(np.ceil(len(self._X) / float(self._batch_size)))
def __getitem__(self, idx):
return np.array(self._elmo.sents2elmo(self._X[idx * self._batch_size:(idx + 1) * self._batch_size]))
model_name = '-'.join(
'.'.join(str(datetime.datetime.now()).split('.')[:-1]).split(' '))
elmo = Embedder(embedding_path, batch_size=batch_size)
train_data = read_file('./data/train.crash')
test_data = read_file('./data/test.crash', is_train=False)
train_tokenized_texts = tokenize(train_data['text'])
test_tokenizes_texts = tokenize(test_data['text'])
labels = train_data['label'].values.astype(np.float16).reshape(-1, 1)
texts = to_length(train_tokenized_texts, max_len)
texts_test = to_length(test_tokenizes_texts, max_len)
print('Number of train data: {}'.format(labels.shape))
texts_train, texts_val, labels_train, labels_val = train_test_split(
texts, labels,
test_size=0.05
)
model_path = './models/{}-version'.format(model_name)
try:
os.mkdir('./models')
except:
print('Folder already created')
try:
os.mkdir(model_path)
except:
print('Folder already created')
checkpoint = ModelCheckpoint(
filepath='{}/models.hdf5'.format(model_path),
monitor='val_f1', verbose=1,
mode='max',
save_best_only=True
)
early = EarlyStopping(monitor='val_f1', mode='max', patience=5)
callbacks_list = [checkpoint, early]
train_seq = TrainSeq(texts_train, labels_train, batch_size=batch_size, elmo = elmo)
val_seq = TrainSeq(texts_val, labels_val, batch_size=min(batch_size, len(texts_val)), elmo = elmo)
test_seq = TestSeq(texts_test, batch_size=min(batch_size, len(texts_test)), elmo = elmo)
model = model(
maxlen = max_len,
embed_size=1024,
use_fasttext = True,
use_additive_emb = use_additive_emb
)
model.fit_generator(
train_seq,
validation_data=val_seq,
callbacks=callbacks_list,
epochs=epochs,
workers=False
)
model.load_weights('{}/models.hdf5'.format(model_path))
prediction_prob = model.predict_generator(val_seq, workers=False)
if should_find_threshold:
OPTIMAL_THRESHOLD = find_threshold(prediction_prob, labels_val)
else:
OPTIMAL_THRESHOLD = 0.5
print('OPTIMAL_THRESHOLD: {}'.format(OPTIMAL_THRESHOLD))
prediction = (prediction_prob > OPTIMAL_THRESHOLD).astype(np.int8)
print('F1 validation score: {}'.format(f1_score(prediction, labels_val)))
with open('{}/f1'.format(model_path), 'w') as fp:
fp.write(str(f1_score(prediction, labels_val)))
test_prediction = model.predict_generator(test_seq, workers=False)
df_predicton = pd.read_csv("./data/sample_submission.csv")
if return_prob:
df_predicton["label"] = test_prediction
else:
df_predicton["label"] = (
test_prediction > OPTIMAL_THRESHOLD).astype(np.int8)
print('Number of test data: {}'.format(df_predicton.shape[0]))
df_predicton.to_csv('{}/prediction.csv'.format(model_path), index=False)
model_dict = {
'RNNKeras': RNNKeras,
'RNNKerasCPU': RNNKerasCPU,
'LSTMKeras': LSTMKeras,
'SARNNKerasCPU': SARNNKerasCPU,
'SARNNKeras': SARNNKeras,
'TextCNN': TextCNN,
'LSTMCNN': LSTMCNN,
'VDCNN': VDCNN
}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-m',
'--model',
help='Model use',
default='RNNKerasCPU'
)
parser.add_argument(
'-e',
'--embedding',
help='Model use',
default='./embeddings/smallFasttext.vi.vec'
)
parser.add_argument(
'--find_threshold',
action='store_true',
help='Model use'
)
parser.add_argument(
'--prob',
action='store_true',
help='Model use'
)
parser.add_argument(
'--add_embed',
action='store_true',
help='Model use'
)
args = parser.parse_args()
if not args.model in model_dict:
raise RuntimeError('Model not found')
train_model(model_dict[args.model], args.embedding, args.find_threshold, args.prob, args.add_embed)