-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
73 lines (66 loc) · 3.31 KB
/
run.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
# coding=utf-8
from models.fast_text import FastText
from models.han import HAN
from models.rcnn import RCNN
from models.rcnn_variant import RCNNVariant
from models.text_att_birnn import TextAttBiRNN
from models.text_birnn import TextBiRNN
from models.text_cnn import TextCNN
from models.text_rnn import TextRNN
from utils.model_config import Config
from utils.model_train import Trainer
MODEL_CONFIG = Config('text_classifier')
NAME2MODEL = {'fast_text': FastText(MODEL_CONFIG.maxlen,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation),
'han': HAN(MODEL_CONFIG.maxlen_sentence,
MODEL_CONFIG.maxlen_word,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation),
'rcnn': RCNN(MODEL_CONFIG.maxlen,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation),
'rcnn_variant': RCNNVariant(MODEL_CONFIG.maxlen,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation),
'text_att_birnn': TextAttBiRNN(MODEL_CONFIG.maxlen,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation),
'text_birnn': TextBiRNN(MODEL_CONFIG.maxlen,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation),
'text_cnn': TextCNN(MODEL_CONFIG.maxlen,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation),
'text_rnn': TextRNN(MODEL_CONFIG.maxlen,
MODEL_CONFIG.max_features,
MODEL_CONFIG.embedding_dims,
MODEL_CONFIG.class_num,
MODEL_CONFIG.last_activation)}
def main(model_name, cross_validation=True):
model = NAME2MODEL.get(model_name, None)
if not model:
print("We have no model named '{}'.".format(model_name))
return
model_config = Config(model_name)
model_trainer = Trainer(model_config, model)
if cross_validation:
model_trainer.train_kflods()
return
model_trainer.train_normal()
if __name__ == '__main__':
main('text_cnn')