-
Notifications
You must be signed in to change notification settings - Fork 16
/
main_stack_hier.py
191 lines (160 loc) · 5.86 KB
/
main_stack_hier.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
from scripts.util import \
read_file, \
tokenize, make_embedding, text_to_sequences, \
sent_embedding, sent_tokenize, text_sents_to_sequences, f1
from scripts.constant import DEFAULT_MAX_FEATURES
from sklearn.model_selection import train_test_split
from scripts.rnn import SARNNKeras, HARNN, AttLayer, RNNKeras, OriginalHARNN, AdditiveLayer
from scripts.cnn import VDCNN, TextCNN, LSTMCNN
from scripts.stack import StackedGeneralizerWithHier
import argparse
import os
import numpy as np
import datetime
import pandas as pd
from sklearn.metrics import f1_score
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from keras.utils import CustomObjectScope
from keras_self_attention import SeqSelfAttention, SeqWeightedAttention
def stack(models_list, hier_models_list, embedding_path, max_features, should_mix):
model_name = '-'.join(
'.'.join(str(datetime.datetime.now()).split('.')[:-1]).split(' '))
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'])
train_tokenized_texts_sent = sent_tokenize(train_data['text'])
test_tokenizes_texts_sent = sent_tokenize(test_data['text'])
labels = train_data['label'].values.astype(np.float16).reshape(-1, 1)
embed_size, word_map, embedding_mat = make_embedding(
list(train_tokenized_texts) +
list(test_tokenizes_texts) if should_mix else train_tokenized_texts,
embedding_path,
max_features
)
embed_size_sent, word_map_sent, embedding_mat_sent = sent_embedding(
list(train_tokenized_texts_sent) +
list(test_tokenizes_texts_sent) if should_mix else train_tokenized_texts_sent,
embedding_path,
max_features
)
texts_id = text_to_sequences(train_tokenized_texts, word_map)
texts_id_sent = text_sents_to_sequences(
train_tokenized_texts_sent,
word_map_sent,
max_nb_sent = 3,
max_sent_len = 50
)
print('Number of train data: {}'.format(labels.shape))
texts_id_train, texts_id_val, texts_id_sent_train, texts_id_sent_val, labels_train, labels_val = train_test_split(
texts_id, texts_id_sent, 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')
batch_size = 16
epochs = 100
patience = 3
# meta_model = RandomForestClassifier (
# n_estimators=200,
# criterion="entropy",
# max_depth=5,
# max_features=0.5
# )
# meta_model = MLPClassifier(
# hidden_layer_sizes = (10),
# early_stopping = True,
# validation_fraction = 0.05,
# batch_size = batch_size,
# n_iter_no_change = patience
# )
meta_model = LogisticRegression()
models = [
model(
embeddingMatrix=embedding_mat,
embed_size=embed_size,
max_features=embedding_mat.shape[0]
)
for model in models_list
]
hier_models = [
model(
embeddingMatrix=embedding_mat_sent,
embed_size=embed_size_sent,
max_features=embedding_mat_sent.shape[0],
max_nb_sent = 3,
max_sent_len = 50
)
for model in hier_models_list
]
stack = StackedGeneralizerWithHier(models, hier_models, meta_model)
stack.train_meta_model(
X = texts_id_train, y = labels_train,
X_val = texts_id_val, y_val = labels_val,
X_hier = texts_id_sent_train, X_hier_val = texts_id_sent_val,
model_path = model_path,
epochs = epochs,
batch_size = batch_size,
patience = patience
)
stack.train_models(
X = texts_id_train, y = labels_train,
X_val = texts_id_val, y_val = labels_val,
X_hier = texts_id_sent_train, X_hier_val = texts_id_sent_val,
batch_size = batch_size,
epochs = epochs,
patience = patience,
model_path = model_path
)
prediction = stack.predict(texts_id_val, texts_id_sent_val)
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_id_texts = text_to_sequences(test_tokenizes_texts, word_map)
test_id_texts_sent = text_sents_to_sequences(test_tokenizes_texts_sent, word_map_sent, 3, 50)
test_prediction = stack.predict(test_id_texts, test_id_texts_sent)
df_predicton = pd.read_csv("./data/sample_submission.csv")
df_predicton["label"] = test_prediction
print('Number of test data: {}'.format(df_predicton.shape[0]))
df_predicton.to_csv('{}/prediction.csv'.format(model_path), index=False)
if __name__ == '__main__':
models_list = [
VDCNN, TextCNN, SARNNKeras, RNNKeras
]
hier_models_list = [
OriginalHARNN, HARNN
]
parser = argparse.ArgumentParser()
parser.add_argument(
'-e',
'--embedding',
help='Model use',
default='./embeddings/smallFasttext.vi.vec'
)
parser.add_argument(
'--max',
help='Model use',
default=DEFAULT_MAX_FEATURES
)
parser.add_argument(
'--mix',
action='store_true',
help='Model use'
)
args = parser.parse_args()
with CustomObjectScope({
'SeqSelfAttention': SeqSelfAttention,
'SeqWeightedAttention': SeqWeightedAttention,
'AttLayer': AttLayer,
'AdditiveLayer': AdditiveLayer,
'f1': f1
}):
stack(models_list, hier_models_list, args.embedding,
int(args.max), args.mix)