-
Notifications
You must be signed in to change notification settings - Fork 80
/
config.py
157 lines (125 loc) · 5.08 KB
/
config.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
# -*- coding: utf-8 -*-
"""
@author: alexyang
@contact: [email protected]
@file: config.py
@time: 2019/5/8 20:37
@desc:
"""
from os import path
from keras.optimizers import Adam
RAW_DATA_DIR = './raw_data'
PROCESSED_DATA_DIR = './data'
LOG_DIR = './log'
MODEL_SAVED_DIR = './ckpt'
SUBMIT_DIR = './submit'
IMG_DIR = './img'
CCKS_DIR = path.join(RAW_DATA_DIR, 'ccks2019_el')
CCKS_TRAIN_FILENAME = path.join(CCKS_DIR, 'train.json')
CCKS_TEST_FILENAME = path.join(CCKS_DIR, 'develop.json')
CCKS_TEST_FINAL_FILENAME = path.join(CCKS_DIR, 'eval722.json')
KB_FILENAME = path.join(CCKS_DIR, 'kb_data')
TRAIN_DATA_FILENAME = 'erl_train.pkl'
DEV_DATA_FILENAME = 'erl_dev.pkl'
TEST_DATA_FILENAME = 'erl_test.pkl'
TEST_FINAL_DATA_FILENAME = 'erl_test_final.pkl'
ENTITY_DESC_FILENAME = 'entity_desc.pkl'
MENTION_TO_ENTITY_FILENAME = 'mention_to_entity.pkl'
ENTITY_TO_MENTION_FILENAME = 'entity_to_mention.pkl'
ENTITY_TYPE_FILENAME = 'entity_type.pkl'
VOCABULARY_TEMPLATE = '{level}_vocab.pkl'
IDX2TOKEN_TEMPLATE = 'idx2{level}.pkl'
EMBEDDING_MATRIX_TEMPLATE = '{type}_embeddings.npy'
PERFORMANCE_LOG = '{model_type}_performance.log'
EXTERNAL_EMBEDDINGS_DIR = path.join(RAW_DATA_DIR, 'embeddings')
EXTERNAL_EMBEDDINGS_FILENAME = {
'bert': path.join(EXTERNAL_EMBEDDINGS_DIR, 'chinese_L-12_H-768_A-12'),
'ernie': path.join(EXTERNAL_EMBEDDINGS_DIR, 'baidu_ernie'),
'bert_wwm': path.join(EXTERNAL_EMBEDDINGS_DIR, 'chinese_wwm_L-12_H-768_A-12')
}
class ModelConfig(object):
def __init__(self):
# input base config
self.embed_dim = 300
self.embed_trainable = True
self.embeddings = None
self.vocab = None # character embedding as base input
self.vocab_size = None
self.mention_to_entity = None
self.entity_desc = None
# input config for entity recognition model
self.label_schema = 'BIO' # 'BIO' or 'BIOES'
self.label_to_one_hot = {'BIO': {'O': [1, 0, 0], 'B': [0, 1, 0], 'I': [0, 0, 1], 'P': [0, 0, 0]},
'BIOES': {'O': [1, 0, 0, 0, 0], 'B': [0, 1, 0, 0, 0], 'I': [0, 0, 1, 0, 0],
'E': [0, 0, 0, 1, 0], 'S': [0, 0, 0, 0, 1], 'P': [0, 0, 0, 0, 0]}}
self.idx2label = {'BIO': {0: 'O', 1: 'B', 2: 'I'},
'BIOES': {0: 'O', 1: 'B', 2: 'I', 3: 'E', 4: 'S'}}
self.n_class = {'BIO': 3, 'BIOES': 5}
self.use_char_input = True
self.use_bert_input = False
self.bert_type = 'bert'
self.bert_model_file = lambda x: path.join(EXTERNAL_EMBEDDINGS_FILENAME[x], 'bert_model.ckpt')
self.bert_config_file = lambda x: path.join(EXTERNAL_EMBEDDINGS_FILENAME[x], 'bert_config.json')
self.bert_vocab_file = lambda x: path.join(EXTERNAL_EMBEDDINGS_FILENAME[x], 'vocab.txt')
self.bert_layer_num = 1
self.bert_seq_len = 50
self.bert_trainable = True
self.use_bichar_input = False
self.bichar_vocab = None
self.bichar_embed_dim = 50
self.bichar_embed_trainable = False
self.bichar_embeddings = None
self.bichar_vocab_size = None
self.use_word_input = False
self.word_vocab = None
self.word_embed_dim = 300
self.word_embed_trainable = False
self.word_embeddings = None
self.word_vocab_size = None
self.use_charpos_input = False
self.charpos_vocab = None
self.charpos_embed_dim = 300
self.charpos_embed_trainable = False
self.charpos_embeddings = None
self.charpos_vocab_size = None
self.use_softword_input = False
self.softword_embed_dim = 50
self.use_dictfeat_input = False
self.use_maxmatch_input = False
self.maxmatch_embed_dim = 50
# input config for entity linking model
self.max_cand_mention = 10
self.max_cand_entity = 10
self.max_desc_len = 400
self.use_relative_pos = False # use relative position (to mention) as input
self.max_erl_len = 30
self.n_rel_pos_embed = 60
self.rel_pos_embed_dim = 50
self.omit_one_cand = True
self.n_neg = 1
# model structure configuration
self.exp_name = None
self.model_name = None
self.rnn_units = 300
self.dense_units = 128
# model training configuration
self.batch_size = 64
self.n_epoch = 50
self.learning_rate = 0.001
self.optimizer = Adam(self.learning_rate)
self.threshold = 0.5
# checkpoint configuration
self.checkpoint_dir = MODEL_SAVED_DIR
self.checkpoint_monitor = 'val_f1'
self.checkpoint_save_best_only = True
self.checkpoint_save_weights_only = True
self.checkpoint_save_weights_mode = 'max'
self.checkpoint_verbose = 1
# early_stoping configuration
self.early_stopping_monitor = 'val_f1'
self.early_stopping_mode = 'max'
self.early_stopping_patience = 5
self.early_stopping_verbose = 1
self.callbacks_to_add = None
# config for learning rating scheduler and ensembler
self.swa_start = 3