-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain_stack.py
133 lines (111 loc) · 3.87 KB
/
main_stack.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
from scripts.util import read_file, tokenize, make_embedding, text_to_sequences, f1
from scripts.constant import DEFAULT_MAX_FEATURES
from sklearn.model_selection import train_test_split
from scripts.rnn import SARNNKeras
from scripts.cnn import LSTMCNN, VDCNN
from scripts.stack import StackedGeneralizer
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 keras.utils import CustomObjectScope
from keras_self_attention import SeqSelfAttention, SeqWeightedAttention
def stack(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'])
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
)
texts_id = text_to_sequences(train_tokenized_texts, word_map)
print('Number of train data: {}'.format(labels.shape))
texts_id_train, texts_id_val, labels_train, labels_val = train_test_split(
texts_id, 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 = LogisticRegression()
models = [
model(
embeddingMatrix=embedding_mat,
embed_size=400,
max_features=embedding_mat.shape[0]
)
for model in models_list
]
stack = StackedGeneralizer(models, meta_model)
stack.train_meta_model(
texts_id_train, labels_train,
texts_id_val, labels_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,
batch_size = batch_size,
epochs = epochs,
patience = patience,
model_path = model_path
)
prediction = stack.predict(texts_id_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_prediction = stack.predict(test_id_texts)
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 = [
SARNNKeras, LSTMCNN, VDCNN
]
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,
'f1': f1}
):
stack(
models_list, args.embedding, int(args.max), args.mix
)