-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
280 lines (227 loc) · 11.9 KB
/
main.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
import json
import logging
import os
import time
import numpy as np
import tensorflow as tf
import data_utils
from as_reader_tf import AttentionSumReaderTf
from attention_sum_reader import AttentionSumReader
# 基础参数
tf.app.flags.DEFINE_bool(flag_name="debug",
default_value=False,
docstring="是否在debug模式")
tf.app.flags.DEFINE_bool(flag_name="train",
default_value=True,
docstring="进行训练")
tf.app.flags.DEFINE_bool(flag_name="test",
default_value=False,
docstring="进行测试")
tf.app.flags.DEFINE_bool(flag_name="ensemble",
default_value=False,
docstring="进行集成模型的测试")
tf.app.flags.DEFINE_integer(flag_name="random_seed",
default_value=1007,
docstring="随机数种子")
tf.app.flags.DEFINE_string(flag_name="log_file",
default_value=None,
docstring="是否将日志存储在文件中")
tf.app.flags.DEFINE_string(flag_name="weight_path",
default_value="model/",
docstring="之前训练的模型权重")
tf.app.flags.DEFINE_string(flag_name="framework",
default_value="tensorflow",
docstring="使用的模型框架,“tensorflow”或者“keras”")
# 定义数据源
tf.app.flags.DEFINE_string(flag_name="data_dir",
default_value="D:/source/data/RC-Cloze-CBT/CBTest/CBTest/data/",
docstring="CBT数据集的路径")
tf.app.flags.DEFINE_string(flag_name="output_dir",
default_value="tmp",
docstring="临时目录")
tf.app.flags.DEFINE_string(flag_name="train_file",
default_value="cbtest_NE_train.txt",
docstring="CBT的训练文件")
tf.app.flags.DEFINE_string(flag_name="valid_file",
default_value="cbtest_NE_valid_2000ex.txt",
docstring="CBT的验证文件")
tf.app.flags.DEFINE_string(flag_name="test_file",
default_value="cbtest_NE_test_2500ex.txt",
docstring="CBT的测试文件")
tf.app.flags.DEFINE_string(flag_name="embedding_file",
default_value="D:/source/data/embedding/glove.6B/glove.6B.200d.txt",
docstring="glove预训练的词向量文件")
tf.app.flags.DEFINE_integer(flag_name="max_vocab_num",
default_value=100000,
docstring="词库中存储的单词最大个数")
tf.app.flags.DEFINE_integer(flag_name="d_len_min",
default_value=0,
docstring="载入样本中文档的最小长度")
tf.app.flags.DEFINE_integer(flag_name="d_len_max",
default_value=2000,
docstring="载入样本中文档的最大长度")
tf.app.flags.DEFINE_integer(flag_name="q_len_min",
default_value=0,
docstring="载入样本中问题的最小长度")
tf.app.flags.DEFINE_integer(flag_name="q_len_max",
default_value=200,
docstring="载入样本中问题的最大长度")
# 模型超参数
tf.app.flags.DEFINE_integer(flag_name="hidden_size",
default_value=128,
docstring="RNN隐层数量")
tf.app.flags.DEFINE_integer(flag_name="num_layers",
default_value=1,
docstring="RNN层数")
tf.app.flags.DEFINE_bool(flag_name="use_lstm",
default_value="False",
docstring="RNN类型:LSTM或者GRU")
# 模型训练超参数
tf.app.flags.DEFINE_integer(flag_name="embedding_dim",
default_value=200,
docstring="词向量维度")
tf.app.flags.DEFINE_integer(flag_name="batch_size",
default_value=32,
docstring="batch_size")
tf.app.flags.DEFINE_integer(flag_name="num_epoches",
default_value=100,
docstring="epoch次数")
tf.app.flags.DEFINE_float(flag_name="dropout_rate",
default_value=0.2,
docstring="dropout比率")
tf.app.flags.DEFINE_string(flag_name="optimizer",
default_value="ADAM",
docstring="优化算法:SGD或者ADAM或者RMSprop")
tf.app.flags.DEFINE_float(flag_name="learning_rate",
default_value=0.001,
docstring="SGD的学习率")
tf.app.flags.DEFINE_integer(flag_name="grad_clipping",
default_value=10,
docstring="梯度截断的阈值,防止RNN梯度爆炸")
FLAGS = tf.app.flags.FLAGS
# bucket,用来处理序列长度方差过大问题
d_bucket = ([150, 310], [310, 400], [450, 600], [600, 750], [750, 950])
q_bucket = (20, 40)
def train_and_test():
# 准备数据
vocab_file, idx_train_file, idx_valid_file, idx_test_file = data_utils.prepare_cbt_data(
FLAGS.data_dir, FLAGS.train_file, FLAGS.valid_file, FLAGS.test_file, FLAGS.max_vocab_num,
output_dir=FLAGS.output_dir)
# 读取数据
d_len_range = (FLAGS.d_len_min, FLAGS.d_len_max)
q_len_range = (FLAGS.q_len_min, FLAGS.q_len_max)
t_documents, t_questions, t_answer, t_candidates = data_utils.read_cbt_data(idx_train_file,
d_len_range,
q_len_range,
max_count=FLAGS.max_count)
v_documents, v_questions, v_answers, v_candidates = data_utils.read_cbt_data(idx_valid_file,
d_len_range,
q_len_range,
max_count=FLAGS.max_count)
test_documents, test_questions, test_answers, test_candidates = data_utils.read_cbt_data(idx_test_file,
max_count=FLAGS.max_count)
d_len = data_utils.get_max_length(t_documents)
q_len = data_utils.get_max_length(t_questions)
logging.info("-" * 50)
logging.info("Building model with {} layers of {} units.".format(FLAGS.num_layers, FLAGS.hidden_size))
# 初始化词向量矩阵,使用(-0.1,0.1)区间内的随机均匀分布
word_dict = data_utils.load_vocab(vocab_file)
embedding_matrix = data_utils.gen_embeddings(word_dict,
FLAGS.embedding_dim,
FLAGS.embedding_file,
init=np.random.uniform)
if FLAGS.framework == "keras":
# 使用keras版本的模型
model = AttentionSumReader(word_dict, embedding_matrix, d_len, q_len,
FLAGS.embedding_dim, FLAGS.hidden_size, FLAGS.num_layers,
FLAGS.weight_path, FLAGS.use_lstm)
else:
# 使用tensorflow版本的模型
sess = tf.Session()
model = AttentionSumReaderTf(word_dict, embedding_matrix, d_len, q_len, sess,
FLAGS.embedding_dim, FLAGS.hidden_size, FLAGS.num_layers,
FLAGS.weight_path, FLAGS.use_lstm)
if FLAGS.train:
logging.info("Start training.")
model.train(train_data=(t_documents, t_questions, t_answer, t_candidates),
valid_data=(v_documents, v_questions, v_answers, v_candidates),
batch_size=FLAGS.batch_size,
epochs=FLAGS.num_epoches_new,
opt_name=FLAGS.optimizer,
lr=FLAGS.learning_rate,
grad_clip=FLAGS.grad_clipping)
if FLAGS.test:
logging.info("Start testing.\nTesting in {} samples.".format(len(test_answers)))
model.load_weight()
model.test(test_data=(test_documents, test_questions, test_answers, test_candidates),
batch_size=FLAGS.batch_size)
if FLAGS.ensemble:
logging.info("Start ensemble testing.\nTesting in {} samples.".format(len(test_answers)))
models = get_ensemble_model(word_dict, embedding_matrix, FLAGS.hidden_size, FLAGS.num_layers, FLAGS.use_lstm)
ensemble_test((test_documents, test_questions, test_answers, test_candidates), models)
def ensemble_test(test_data, models):
data = [[] for _ in d_bucket]
for test_document, test_question, test_answer, test_candidate in zip(*test_data):
if len(test_document) <= d_bucket[0][0]:
data[0].append((test_document, test_question, test_answer, test_candidate))
continue
if len(test_document) >= d_bucket[-1][-1]:
data[len(models) - 1].append((test_document, test_question, test_answer, test_candidate))
continue
for bucket_id, (d_min, d_max) in enumerate(d_bucket):
if d_min < len(test_document) < d_max:
data[bucket_id].append((test_document, test_question, test_answer, test_candidate))
continue
acc, num = 0, 0
for i in range(len(models)):
num += len(data[i])
logging.info("Start testing.\nTesting in {} samples.".format(len(data[i])))
acc_i, _ = models[i].test(zip(*data[i]), batch_size=1)
acc += acc_i
logging.critical("Ensemble test done.\nAccuracy is {}".format(acc / num))
def get_ensemble_model(word_dict,
embedding_matrix,
hidden_size,
num_layers,
use_lstm):
embedding_dim = len(embedding_matrix[0])
models = {}
for b_id, r in enumerate(d_bucket):
weight_path = "{}{}-{}-{}-{}/".format(FLAGS.weight_path, r[0], r[1], q_bucket[0], q_bucket[1])
model = AttentionSumReader(word_dict, embedding_matrix, r[1], q_bucket[1],
embedding_dim, hidden_size, num_layers,
weight_path, use_lstm)
logging.info(weight_path)
model.load_weight(weight_path)
models[b_id] = model
return models
def clear():
"""
清除所有临时文件,请谨慎使用
:return:
"""
tmp_dir = os.path.join(FLAGS.data_dir, FLAGS.output_dir)
if tf.gfile.Exists(tmp_dir):
tf.gfile.DeleteRecursively(tmp_dir)
def save_arguments(args, file):
with open(file, "w") as fp:
json.dump(args, fp, sort_keys=True, indent=4)
def main(_):
train_and_test()
if __name__ == '__main__':
if not os.path.exists(FLAGS.weight_path):
os.mkdir(FLAGS.weight_path)
# 设置随机数种子
np.random.seed(FLAGS.random_seed)
tf.set_random_seed(FLAGS.random_seed)
FLAGS.max_count = 35200 if FLAGS.debug else None
FLAGS.num_epoches_new = 2 if FLAGS.debug else FLAGS.num_epoches
# 设置Log
logging.basicConfig(filename=FLAGS.log_file,
level=logging.DEBUG,
format='%(asctime)s %(message)s', datefmt='%y-%m-%d %H:%M')
save_arguments(FLAGS.__flags, "{}args-{}.json".format(FLAGS.weight_path,
time.strftime("%Y-%m-%d-(%H-%M)",
time.localtime())))
logging.info(FLAGS.__flags)
tf.app.run()