-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
328 lines (276 loc) · 15.2 KB
/
train.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- coding: utf-8 -*-
"""
@author: alexyang
@contact: alex.yang0326@gmail.com
@file: train.py
@time: 2019/2/9 13:31
@desc:
"""
import os
from os import path
import time
import numpy as np
from keras import optimizers
from config import ModelConfig, LOG_DIR, PERFORMANCE_LOG_TEMPLATE, PROCESSED_DATA_DIR, EMBEDDING_MATRIX_TEMPLATE, \
VARIATIONS, PREDICT_DIR
from models.keras_bilstm_model import BiLSTM
from models.keras_cnnrnn_model import CNNRNN
from models.keras_dcnn_model import DCNN
from models.keras_dpcnn_model import DPCNN
from models.keras_han_model import HAN
from models.keras_multi_text_cnn_model import MultiTextCNN
from models.keras_rcnn_model import RCNN
from models.keras_rnncnn_model import RNNCNN
from models.keras_text_cnn_model import TextCNN
from models.keras_vdcnn_model import VDCNN
from models.sklearn_base_model import SVMModel, LRModel, SGDModel, GaussianNBModel, MultinomialNBModel, \
BernoulliNBModel, RandomForestModel, GBDTModel, XGBoostModel
from models.keras_dialect_match_model import DialectMatchModel
from utils.data_loader import load_processed_data, load_ngram_data, load_processed_text_data
from utils.io import format_filename, write_log, writer_predict
os.environ['CUDA_VISIBLE_DEVICES'] = '2'
def get_optimizer(op_type, learning_rate):
if op_type == 'sgd':
return optimizers.SGD(learning_rate)
elif op_type == 'rmsprop':
return optimizers.RMSprop(learning_rate)
elif op_type == 'adagrad':
return optimizers.Adagrad(learning_rate)
elif op_type == 'adadelta':
return optimizers.Adadelta(learning_rate)
elif op_type == 'adam':
return optimizers.Adam(learning_rate)
else:
raise ValueError('Optimizer Not Understood: {}'.format(op_type))
# train deep learning based model
def train_dl_model(variation, input_level, word_embed_type, word_embed_trainable, batch_size, learning_rate,
optimizer_type, model_name, binary_threshold=0.5, checkpoint_dir=None, overwrite=False,
log_error=False, save_log=True, **kwargs):
config = ModelConfig()
config.variation = variation
config.input_level = input_level
if '_aug' in variation:
config.max_len = {'word': config.aug_word_max_len, 'char': config.aug_char_max_len}
config.word_embed_type = word_embed_type
config.word_embed_trainable = word_embed_trainable
config.word_embeddings = np.load(format_filename(PROCESSED_DATA_DIR, EMBEDDING_MATRIX_TEMPLATE,
variation=variation, type=word_embed_type))
config.batch_size = batch_size
config.learning_rate = learning_rate
config.optimizer = get_optimizer(optimizer_type, learning_rate)
config.binary_threshold = binary_threshold
if checkpoint_dir is not None:
config.checkpoint_dir = checkpoint_dir
if not os.path.exists(config.checkpoint_dir):
os.makedirs(config.checkpoint_dir)
config.exp_name = '{}_{}_{}_{}_{}'.format(variation, model_name, input_level, word_embed_type,
'tune' if word_embed_trainable else 'fix')
train_log = {'exp_name': config.exp_name, 'batch_size': batch_size, 'optimizer': optimizer_type,
'learning_rate': learning_rate, 'binary_threshold': binary_threshold}
print('Logging Info - Experiment: ', config.exp_name)
if model_name == 'bilstm':
model = BiLSTM(config, **kwargs)
elif model_name == 'cnnrnn':
model = CNNRNN(config, **kwargs)
elif model_name == 'dcnn':
model = DCNN(config, **kwargs)
elif model_name == 'dpcnn':
model = DPCNN(config, **kwargs)
elif model_name == 'han':
model = HAN(config, **kwargs)
elif model_name == 'multicnn':
model = MultiTextCNN(config, **kwargs)
elif model_name == 'rcnn':
model = RCNN(config, **kwargs)
elif model_name == 'rnncnn':
model = RNNCNN(config, **kwargs)
elif model_name == 'cnn':
model = TextCNN(config, **kwargs)
elif model_name == 'vdcnn':
model = VDCNN(config, **kwargs)
else:
raise ValueError('Model Name Not Understood : {}'.format(model_name))
train_input = load_processed_data(variation, input_level, 'train')
dev_input = load_processed_data(variation, input_level, 'dev')
test_input = load_processed_data(variation, input_level, 'test')
model_save_path = path.join(config.checkpoint_dir, '{}.hdf5'.format(config.exp_name))
if not path.exists(model_save_path) or overwrite:
start_time = time.time()
model.train(train_input, dev_input)
elapsed_time = time.time() - start_time
print('Logging Info - Training time: %s', time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
train_log['train_time'] = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
# load the best model
model.load_best_model()
print('Logging Info - Evaluate over valid data:')
valid_acc, valid_f1, valid_macro_f1, valid_p, valid_r = model.evaluate(dev_input)
train_log['valid_acc'] = valid_acc
train_log['valid_f1'] = valid_f1
train_log['valid_macro_f1'] = valid_macro_f1
train_log['valid_p'] = valid_p
train_log['valid_r'] = valid_r
train_log['time_stamp'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if log_error:
error_indexes, error_pred_probas = model.error_analyze(dev_input)
dev_text_input = load_processed_text_data(variation, 'dev')
for error_index, error_pred_prob in zip(error_indexes, error_pred_probas):
train_log['error_%d' % error_index] = '{},{},{},{}'.format(error_index,
dev_text_input['sentence'][error_index],
dev_text_input['label'][error_index],
error_pred_prob)
if save_log:
write_log(format_filename(LOG_DIR, PERFORMANCE_LOG_TEMPLATE, variation=variation), log=train_log, mode='a')
if test_input is not None:
test_predictions = model.predict(test_input)
writer_predict(format_filename(PREDICT_DIR, config.exp_name+'.labels'), test_predictions)
return valid_acc, valid_f1, valid_macro_f1, valid_p, valid_r
# train machine learning based model
def train_ml_model(model_name, variation, vectorizer_type, level, ngram_range, binary_threshold=0.5,
checkpoint_dir=None, overwrite=False, log_error=False, save_log=True, **kwargs):
config = ModelConfig()
config.binary_threshold = binary_threshold
if checkpoint_dir is not None:
config.checkpoint_dir = checkpoint_dir
if not os.path.exists(config.checkpoint_dir):
os.makedirs(config.checkpoint_dir)
config.exp_name = '{}_{}_{}_{}_{}'.format(variation, model_name, vectorizer_type, level, ngram_range)
train_log = {'exp_name': config.exp_name, 'binary_threshold': binary_threshold}
print('Logging Info - Experiment: ', config.exp_name)
if model_name == 'svm':
model = SVMModel(config, **kwargs)
elif model_name == 'lr':
model = LRModel(config, **kwargs)
elif model_name == 'sgd':
model = SGDModel(config, **kwargs)
elif model_name == 'gnb':
model = GaussianNBModel(config, **kwargs)
elif model_name == 'mnb':
model = MultinomialNBModel(config, **kwargs)
elif model_name == 'bnb':
model = BernoulliNBModel(config, **kwargs)
elif model_name == 'rf':
model = RandomForestModel(config, **kwargs)
elif model_name == 'gbdt':
model = GBDTModel(config, **kwargs)
elif model_name == 'xgboost':
model = XGBoostModel(config, **kwargs)
else:
raise ValueError('Model Name Not Understood : {}'.format(model_name))
train_input = load_ngram_data(variation, vectorizer_type, level, ngram_range, 'train')
dev_input = load_ngram_data(variation, vectorizer_type, level, ngram_range, 'dev')
test_input = load_ngram_data(variation, vectorizer_type, level, ngram_range, 'test')
model_save_path = path.join(config.checkpoint_dir, '{}.hdf5'.format(config.exp_name))
if not path.exists(model_save_path) or overwrite:
model.train(train_input)
model.load_best_model()
print('Logging Info - Evaluate over valid data:')
valid_acc, valid_f1, valid_macro_f1, valid_p, valid_r = model.evaluate(dev_input)
train_log['valid_acc'] = valid_acc
train_log['valid_f1'] = valid_f1
train_log['valid_macro_f1'] = valid_macro_f1
train_log['valid_p'] = valid_p
train_log['valid_r'] = valid_r
train_log['time_stamp'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if log_error:
error_indexes, error_pred_probas = model.error_analyze(dev_input)
dev_text_input = load_processed_text_data(variation, 'dev')
for error_index, error_pred_prob in zip(error_indexes, error_pred_probas):
train_log['error_%d' % error_index] = '{},{},{},{}'.format(error_index,
dev_text_input['sentence'][error_index],
dev_text_input['label'][error_index],
error_pred_prob)
if save_log:
write_log(format_filename(LOG_DIR, PERFORMANCE_LOG_TEMPLATE, variation=variation), log=train_log, mode='a')
if test_input is not None:
test_predictions = model.predict(test_input)
writer_predict(format_filename(PREDICT_DIR, config.exp_name+'.labels'), test_predictions)
return valid_acc, valid_f1, valid_p, valid_r
# train dialect matching based model
def train_match_model(variation, input_level, word_embed_type, word_embed_trainable, batch_size, learning_rate,
optimizer_type, encoder_type='concat_attention', metrics='euclidean', checkpoint_dir=None,
overwrite=False):
config = ModelConfig()
config.variation = variation
config.input_level = input_level
if '_aug' in variation:
config.max_len = {'word': config.aug_word_max_len, 'char': config.aug_char_max_len}
config.word_embed_type = word_embed_type
config.word_embed_trainable = word_embed_trainable
config.word_embeddings = np.load(format_filename(PROCESSED_DATA_DIR, EMBEDDING_MATRIX_TEMPLATE,
variation=variation, type=word_embed_type))
config.batch_size = batch_size
config.learning_rate = learning_rate
config.optimizer = get_optimizer(optimizer_type, learning_rate)
if checkpoint_dir is not None:
config.checkpoint_dir = checkpoint_dir
if not os.path.exists(config.checkpoint_dir):
os.makedirs(config.checkpoint_dir)
config.exp_name = '{}_dialect_match_{}_{}_{}_{}_{}'.format(variation, encoder_type, metrics, input_level,
word_embed_type,
'tune' if word_embed_trainable else 'fix')
config.checkpoint_monitor = 'val_loss'
config.early_stopping_monitor = 'val_loss'
train_log = {'exp_name': config.exp_name, 'batch_size': batch_size, 'optimizer': optimizer_type,
'learning_rate': learning_rate}
model = DialectMatchModel(config, encoder_type='concat_attention', metrics='euclidean')
train_input = load_processed_data(variation, input_level, 'train')
dev_input = load_processed_data(variation, input_level, 'dev')
model_save_path = path.join(config.checkpoint_dir, '{}.hdf5'.format(config.exp_name))
if not path.exists(model_save_path) or overwrite:
start_time = time.time()
model.train(train_input, dev_input)
elapsed_time = time.time() - start_time
print('Logging Info - Training time: %s', time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
train_log['train_time'] = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
# load the best model
model.load_best_model()
print('Logging Info - Evaluate over valid data:')
valid_acc, valid_f1 = model.evaluate(dev_input)
train_log['valid_acc'] = valid_acc
train_log['valid_f1'] = valid_f1
train_log['time_stamp'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
write_log(format_filename(LOG_DIR, PERFORMANCE_LOG_TEMPLATE, variation=variation+'_match'), log=train_log, mode='a')
return valid_acc, valid_f1
if __name__ == '__main__':
# train dl model
for variation in VARIATIONS:
for word_embed_type in ['w2v_data']:
for model_name in ['bilstm']:
for binary_threshold in [0.5]:
train_dl_model(variation, 'word', word_embed_type, True, 64, 0.01, 'adam', model_name, binary_threshold=binary_threshold)
# train ml model with skip ngram input
# for variation in VARIATIONS:
# for vectorizer_type in ['binary', 'tf', 'tfidf']:
# for level in ['word', 'char']:
# for ngram in [2, 3]:
# for skip_k in [1, 2, 3]:
# for model_name in ['mnb']:
# train_ml_model(model_name, variation, vectorizer_type, level, '%d_%d' % (ngram, skip_k))
# train ml mode with pos ngram input
# for variation in VARIATIONS:
# for vectorizer_type in ['binary', 'tf', 'tfidf']:
# for level in ['word']:
# for ngram_range in [(1, 1), (2, 2), (3, 3)]:
# for model_name in ['mnb', 'svm']:
# train_ml_model(model_name, variation+'_pos', vectorizer_type, level, ngram_range)
# train ml model with ngram input
for variation in VARIATIONS:
for model_name in ['mnb']:
for vectorizer_type in ['binary']:
# for level in ['word', 'char']:
# for n_gram in range(1, 9, 1):
# train_ml_model(model_name, variation, vectorizer_type, level, (n_gram, n_gram))
# train_ml_model(model_name, variation, vectorizer_type, 'char', (1, 3))
train_ml_model(model_name, variation, vectorizer_type, 'char', (2, 3))
# train_ml_model(model_name, variation, vectorizer_type, ['char', 'word'], [(2, 3), (1, 1)])
# train_ml_model(model_name, variation, vectorizer_type, ['char', 'char'], [(2, 3), (4, 4)])
# train_ml_model(model_name, variation, vectorizer_type, ['char', 'char'], [(1, 3), (4, 4)])
# train_ml_model(model_name, variation, vectorizer_type, ['char', 'char', 'word'], [(2, 3), (4, 4), (1, 1)])
# train_ml_model(model_name, variation, vectorizer_type, ['char', 'char', 'word'], [(1, 3), (4, 4), (1, 1)])
# train dialect matching model
# for variation in VARIATIONS:
# for encoder_type in ['lstm', 'gru', 'bilstm', 'bigru', 'bilstm_max_pool', 'bilstm_mean_pool',
# 'concat_attention', 'self_attention', 'h_cnn', 'cnn', 'dot_attention', 'mul_attention',
# 'add_attention']:
# for metrics in ['sigmoid', 'manhattan', 'euclidean']:
# train_match_model(variation, 'word', 'w2v_data', True, 64, 0.001, 'adam', encoder_type, metrics)