This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 274
/
s2s.py
executable file
·351 lines (332 loc) · 10.2 KB
/
s2s.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
__author__ = '[email protected]'
import os
import sys
import math
import time
import numpy as np
import tensorflow as tf
import data_utils
import s2s_model
tf.app.flags.DEFINE_float(
'learning_rate',
0.0003,
'学习率'
)
tf.app.flags.DEFINE_float(
'max_gradient_norm',
5.0,
'梯度最大阈值'
)
tf.app.flags.DEFINE_float(
'dropout',
1.0,
'每层输出DROPOUT的大小'
)
tf.app.flags.DEFINE_integer(
'batch_size',
64,
'批量梯度下降的批量大小'
)
tf.app.flags.DEFINE_integer(
'size',
512,
'LSTM每层神经元数量'
)
tf.app.flags.DEFINE_integer(
'num_layers',
2,
'LSTM的层数'
)
tf.app.flags.DEFINE_integer(
'num_epoch',
5,
'训练几轮'
)
tf.app.flags.DEFINE_integer(
'num_samples',
512,
'分批softmax的样本量'
)
tf.app.flags.DEFINE_integer(
'num_per_epoch',
10000,
'每轮训练多少随机样本'
)
tf.app.flags.DEFINE_string(
'buckets_dir',
'./bucket_dbs',
'sqlite3数据库所在文件夹'
)
tf.app.flags.DEFINE_string(
'model_dir',
'./model',
'模型保存的目录'
)
tf.app.flags.DEFINE_string(
'model_name',
'model',
'模型保存的名称'
)
tf.app.flags.DEFINE_boolean(
'use_fp16',
False,
'是否使用16位浮点数(默认32位)'
)
tf.app.flags.DEFINE_integer(
'bleu',
-1,
'是否测试bleu'
)
tf.app.flags.DEFINE_boolean(
'test',
False,
'是否在测试'
)
FLAGS = tf.app.flags.FLAGS
buckets = data_utils.buckets
def create_model(session, forward_only):
"""建立模型"""
dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
model = s2s_model.S2SModel(
data_utils.dim,
data_utils.dim,
buckets,
FLAGS.size,
FLAGS.dropout,
FLAGS.num_layers,
FLAGS.max_gradient_norm,
FLAGS.batch_size,
FLAGS.learning_rate,
FLAGS.num_samples,
forward_only,
dtype
)
return model
def train():
"""训练模型"""
# 准备数据
print("train mode")
print('准备数据')
if not os.path.exists(FLAGS.model_dir):
os.makedirs(FLAGS.model_dir)
bucket_dbs = data_utils.read_bucket_dbs(FLAGS.buckets_dir)
bucket_sizes = []
for i in range(len(buckets)):
bucket_size = bucket_dbs[i].size
bucket_sizes.append(bucket_size)
print('bucket {} 中有数据 {} 条'.format(i, bucket_size))
total_size = sum(bucket_sizes)
print('共有数据 {} 条'.format(total_size))
# 开始建模与训练
with tf.Session() as sess:
# 构建模型
model = create_model(sess, False)
# 初始化变量
sess.run(tf.initialize_all_variables())
ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir)
#print("ckpt path : ", ckpt.model_checkpoint_path)
if ckpt != None:
print("load old model : ", ckpt.model_checkpoint_path)
model.saver.restore(sess, ckpt.model_checkpoint_path)
else:
print("not exist old model")
buckets_scale = [
sum(bucket_sizes[:i + 1]) / total_size
for i in range(len(bucket_sizes))
]
# 开始训练
metrics = ' '.join([
'\r[{}]',
'{:.1f}%',
'{}/{}',
'loss={:.3f}',
'{}/{}'
])
bars_max = 20
for epoch_index in range(1, FLAGS.num_epoch + 1):
print('Epoch {}:'.format(epoch_index))
time_start = time.time()
epoch_trained = 0
batch_loss = []
while True:
# 选择一个要训练的bucket
random_number = np.random.random_sample()
bucket_id = min([
i for i in range(len(buckets_scale))
if buckets_scale[i] > random_number
])
data, data_in = model.get_batch_data(
bucket_dbs,
bucket_id
)
encoder_inputs, decoder_inputs, decoder_weights = model.get_batch(
bucket_dbs,
bucket_id,
data
)
_, step_loss, output = model.step(
sess,
encoder_inputs,
decoder_inputs,
decoder_weights,
bucket_id,
False
)
epoch_trained += FLAGS.batch_size
batch_loss.append(step_loss)
time_now = time.time()
time_spend = time_now - time_start
time_estimate = time_spend / (epoch_trained / FLAGS.num_per_epoch)
percent = min(100, epoch_trained / FLAGS.num_per_epoch) * 100
bars = math.floor(percent / 100 * bars_max)
sys.stdout.write(metrics.format(
'=' * bars + '-' * (bars_max - bars),
percent,
epoch_trained, FLAGS.num_per_epoch,
np.mean(batch_loss),
data_utils.time(time_spend), data_utils.time(time_estimate)
))
sys.stdout.flush()
if epoch_trained >= FLAGS.num_per_epoch:
model.saver.save(sess, os.path.join(FLAGS.model_dir, FLAGS.model_name), global_step=epoch_index)
break
print('\n')
#if not os.path.exists(FLAGS.model_dir):
# os.makedirs(FLAGS.model_dir)
#model.saver.save(sess, os.path.join(FLAGS.model_dir, FLAGS.model_name))
def test_bleu(count):
"""测试bleu"""
print("bleu test mode")
from nltk.translate.bleu_score import sentence_bleu
from tqdm import tqdm
# 准备数据
print('准备数据')
bucket_dbs = data_utils.read_bucket_dbs(FLAGS.buckets_dir)
bucket_sizes = []
for i in range(len(buckets)):
bucket_size = bucket_dbs[i].size
bucket_sizes.append(bucket_size)
print('bucket {} 中有数据 {} 条'.format(i, bucket_size))
total_size = sum(bucket_sizes)
print('共有数据 {} 条'.format(total_size))
# bleu设置0的话,默认对所有样本采样
if count <= 0:
count = total_size
buckets_scale = [
sum(bucket_sizes[:i + 1]) / total_size
for i in range(len(bucket_sizes))
]
with tf.Session() as sess:
# 构建模型
model = create_model(sess, True)
model.batch_size = 1
# 初始化变量
sess.run(tf.initialize_all_variables())
model.saver.restore(sess, os.path.join(FLAGS.model_dir, FLAGS.model_name))
total_score = 0.0
for i in tqdm(range(count)):
# 选择一个要训练的bucket
random_number = np.random.random_sample()
bucket_id = min([
i for i in range(len(buckets_scale))
if buckets_scale[i] > random_number
])
data, _ = model.get_batch_data(
bucket_dbs,
bucket_id
)
encoder_inputs, decoder_inputs, decoder_weights = model.get_batch(
bucket_dbs,
bucket_id,
data
)
_, _, output_logits = model.step(
sess,
encoder_inputs,
decoder_inputs,
decoder_weights,
bucket_id,
True
)
outputs = [int(np.argmax(logit, axis=1)) for logit in output_logits]
ask, _ = data[0]
all_answers = bucket_dbs[bucket_id].all_answers(ask)
ret = data_utils.indice_sentence(outputs)
if not ret:
continue
references = [list(x) for x in all_answers]
score = sentence_bleu(
references,
list(ret),
weights=(1.0,)
)
total_score += score
print('BLUE: {:.2f} in {} samples'.format(total_score / count * 10, count))
def test():
print("test mode")
class TestBucket(object):
def __init__(self, sentence):
self.sentence = sentence
def random(self):
return sentence, ''
with tf.Session() as sess:
# 构建模型
model = create_model(sess, True)
model.batch_size = 1
# 初始化变量
sess.run(tf.initialize_all_variables())
ckpt =tf.train.get_checkpoint_state(FLAGS.model_dir)
if ckpt == None or ckpt.model_checkpoint_path == None:
print('restore model fail')
return
print('restore model file %s' % ckpt.model_checkpoint_path)
print(ckpt.model_checkpoint_path)
model.saver.restore(sess,ckpt.model_checkpoint_path)
print("Input 'exit()' to exit test mode!")
sys.stdout.write("me > ")
sys.stdout.flush()
sentence = sys.stdin.readline()
if "exit()" in sentence:
sentence = False
while sentence:
bucket_id = min([
b for b in range(len(buckets))
if buckets[b][0] > len(sentence)
])
data, _ = model.get_batch_data(
{bucket_id: TestBucket(sentence)},
bucket_id
)
encoder_inputs, decoder_inputs, decoder_weights = model.get_batch(
{bucket_id: TestBucket(sentence)},
bucket_id,
data
)
_, _, output_logits = model.step(
sess,
encoder_inputs,
decoder_inputs,
decoder_weights,
bucket_id,
True
)
outputs = [int(np.argmax(logit, axis=1)) for logit in output_logits]
ret = data_utils.indice_sentence(outputs)
print("AI >", ret)
print("me > ", end="")
sys.stdout.flush()
sentence = sys.stdin.readline()
if "exit()" in sentence:
break
def main(_):
if FLAGS.bleu > -1:
test_bleu(FLAGS.bleu)
elif FLAGS.test:
test()
else:
train()
if __name__ == '__main__':
np.random.seed(0)
tf.set_random_seed(0)
tf.app.run()