-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_seq2seq.py
365 lines (255 loc) · 14.8 KB
/
basic_seq2seq.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import tensorflow as tf
from tensorflow.python.layers.core import Dense
import numpy as np
import time
# Number of Epochs
epochs = 1000
# Batch Size
batch_size = 50
# RNN Size
rnn_size = 50
# Number of Layers
num_layers = 2
# Embedding Size
encoding_embedding_size = 15
decoding_embedding_size = 15
# Learning Rate
learning_rate = 0.001
source = open("data/source.txt",'w')
target = open("data/target.txt",'w')
with open("data/对联.txt",'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip().split(" ")
source.write(line[0]+'\n')
target.write(line[1]+'\n')
source.close()
target.close()
with open('data/source.txt','r',encoding='utf-8') as f:
source_data = f.read()
with open('data/target.txt','r',encoding='utf-8') as f:
target_data = f.read()
print(source_data.split('\n')[:10])
print(target_data.split('\n')[:10])
def extract_character_vocab(data):
special_words = ['<PAD>','<UNK>','<GO>','<EOS>']
set_words = list(set([character for line in data.split('\n') for character in line]))
int_to_vocab = {idx:word for idx,word in enumerate(special_words + set_words)}
vocab_to_int = {word:idx for idx,word in int_to_vocab.items()}
return int_to_vocab,vocab_to_int
source_int_to_letter,source_letter_to_int = extract_character_vocab(source_data+target_data)
target_int_to_letter,target_letter_to_int = extract_character_vocab(source_data+target_data)
source_int = [[source_letter_to_int.get(letter,source_letter_to_int['<UNK>'])
for letter in line] for line in source_data.split('\n')]
target_int = [[target_letter_to_int.get(letter, target_letter_to_int['<UNK>'])
for letter in line] + [target_letter_to_int['<EOS>']] for line in target_data.split('\n')]
print(source_int)
print(target_int)
def get_inputs():
inputs = tf.placeholder(tf.int32,[None,None],name='inputs')
targets = tf.placeholder(tf.int32,[None,None],name='targets')
learning_rate = tf.placeholder(tf.float32,name='learning_rate')
target_sequence_length = tf.placeholder(tf.int32,(None,),name='target_sequence_length')
max_target_sequence_length = tf.reduce_max(target_sequence_length,name='max_target_len')
source_sequence_length = tf.placeholder(tf.int32,(None,),name='source_sequence_length')
return inputs,targets,learning_rate,target_sequence_length,max_target_sequence_length,source_sequence_length
def get_encoder_layer(input_data,rnn_size,num_layers,source_sequence_length,source_vocab_size,encoding_embedding_size):
embed_sequence(
ids,
vocab_size=None,
embed_dim=None,
unique=False,
initializer=None,
regularizer=None,
trainable=True,
scope=None,
reuse=None
)
ids: [batch_size, doc_length] Tensor of type int32 or int64 with symbol ids.
return : Tensor of [batch_size, doc_length, embed_dim] with embedded sequences.
"""
encoder_embed_input = tf.contrib.layers.embed_sequence(input_data,source_vocab_size,encoding_embedding_size)
def get_lstm_cell(rnn_size):
lstm_cell = tf.contrib.rnn.LSTMCell(rnn_size,initializer=tf.random_uniform_initializer(-0.1,0.1,seed=2))
return lstm_cell
cell = tf.contrib.rnn.MultiRNNCell([get_lstm_cell(rnn_size) for _ in range(num_layers)])
encoder_output , encoder_state = tf.nn.dynamic_rnn(cell,encoder_embed_input,sequence_length=source_sequence_length,dtype=tf.float32)
return encoder_output,encoder_state
def process_decoder_input(data,vocab_to_int,batch_size):
ending = tf.strided_slice(data,[0,0],[batch_size,-1],[1,1])
decoder_input = tf.concat([tf.fill([batch_size,1],vocab_to_int['<GO>']),ending],1)
return decoder_input
def decoding_layer(target_letter_to_int,decoding_embedding_size,num_layers,rnn_size,
target_sequence_length,max_target_sequence_length,encoder_state,decoder_input):
# 1. Embedding
target_vocab_size = len(target_letter_to_int)
decoder_embeddings = tf.Variable(tf.random_uniform([target_vocab_size,decoding_embedding_size]))
decoder_embed_input = tf.nn.embedding_lookup(decoder_embeddings,decoder_input)
def get_decoder_cell(rnn_size):
decoder_cell = tf.contrib.rnn.LSTMCell(rnn_size,initializer=tf.random_uniform_initializer(-0.1,0.1,seed=2))
return decoder_cell
cell = tf.contrib.rnn.MultiRNNCell([get_decoder_cell(rnn_size) for _ in range(num_layers)])
# Output
# target_vocab_size定义了输出层的大小
output_layer = Dense(target_vocab_size,kernel_initializer=tf.truncated_normal_initializer(mean=0.1,stddev=0.1))
# 4. Training decoder
with tf.variable_scope("decode"):
training_helper = tf.contrib.seq2seq.TrainingHelper(inputs = decoder_embed_input,
sequence_length = target_sequence_length,
time_major = False)
training_decoder = tf.contrib.seq2seq.BasicDecoder(cell,training_helper,encoder_state,output_layer)
training_decoder_output,_,_ = tf.contrib.seq2seq.dynamic_decode(training_decoder,impute_finished=True,
maximum_iterations = max_target_sequence_length)
# 5. Predicting decoder
with tf.variable_scope("decode",reuse=True):
start_tokens = tf.tile(tf.constant([target_letter_to_int['<GO>']],dtype=tf.int32),[batch_size],name='start_token')
predicting_helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(decoder_embeddings,start_tokens,target_letter_to_int['<EOS>'])
predicting_decoder = tf.contrib.seq2seq.BasicDecoder(cell,
predicting_helper,
encoder_state,
output_layer)
predicting_decoder_output,_,_ = tf.contrib.seq2seq.dynamic_decode(predicting_decoder,impute_finished = True,
maximum_iterations = max_target_sequence_length)
return training_decoder_output,predicting_decoder_output
def seq2seq_model(input_data,targets,lr,target_sequence_length,max_target_sequence_length,
source_sequence_length,source_vocab_size,target_vocab_size,encoder_embedding_size,
decoder_embedding_size,rnn_size,num_layers):
_,encoder_state = get_encoder_layer(input_data,
rnn_size,
num_layers,
source_sequence_length,
source_vocab_size,
encoding_embedding_size)
decoder_input = process_decoder_input(targets,target_letter_to_int,batch_size)
training_decoder_output,predicting_decoder_output = decoding_layer(target_letter_to_int,
decoding_embedding_size,
num_layers,
rnn_size,
target_sequence_length,
max_target_sequence_length,
encoder_state,
decoder_input)
return training_decoder_output,predicting_decoder_output
train_graph = tf.Graph()
with train_graph.as_default():
input_data, targets, lr, target_sequence_length, max_target_sequence_length, source_sequence_length = get_inputs()
training_decoder_output, predicting_decoder_output = seq2seq_model(input_data,
targets,
lr,
target_sequence_length,
max_target_sequence_length,
source_sequence_length,
len(source_letter_to_int),
len(target_letter_to_int),
encoding_embedding_size,
decoding_embedding_size,
rnn_size,
num_layers)
training_logits = tf.identity(training_decoder_output.rnn_output,'logits')
predicting_logits = tf.identity(predicting_decoder_output.sample_id,name='predictions')
#tf.sequence_mask([1, 3, 2], 5) # [[True, False, False, False, False],
# [True, True, True, False, False],
# [True, True, False, False, False]]
masks = tf.sequence_mask(target_sequence_length,max_target_sequence_length,dtype=tf.float32,name="masks")
# logits: A Tensor of shape [batch_size, sequence_length, num_decoder_symbols] and dtype float.
# The logits correspond to the prediction across all classes at each timestep.
#targets: A Tensor of shape [batch_size, sequence_length] and dtype int.
# The target represents the true class at each timestep.
#weights: A Tensor of shape [batch_size, sequence_length] and dtype float.
# weights constitutes the weighting of each prediction in the sequence. When using weights as masking,
# set all valid timesteps to 1 and all padded timesteps to 0, e.g. a mask returned by tf.sequence_mask.
with tf.name_scope("optimization"):
cost = tf.contrib.seq2seq.sequence_loss(
training_logits,
targets,
masks
)
optimizer = tf.train.AdamOptimizer(lr)
gradients = optimizer.compute_gradients(cost)
capped_gradients = [(tf.clip_by_value(grad, -5., 5.), var) for grad, var in gradients if grad is not None]
train_op = optimizer.apply_gradients(capped_gradients)
def pad_sentence_batch(sentence_batch,pad_int):
max_sentence = max([len(sentence) for sentence in sentence_batch])
return [sentence + [pad_int] * (max_sentence - len(sentence)) for sentence in sentence_batch]
def get_batches(targets,sources,batch_size,source_pad_int,target_pad_int):
for batch_i in range(0,len(sources)//batch_size):
start_i = batch_i * batch_size
sources_batch = sources[start_i : start_i + batch_size]
targets_batch = targets[start_i : start_i + batch_size]
pad_sources_batch = np.array(pad_sentence_batch(sources_batch,source_pad_int))
pad_targets_batch = np.array(pad_sentence_batch(targets_batch,target_pad_int))
targets_lengths = []
for target in targets_batch:
targets_lengths.append(len(target))
source_lengths = []
for source in sources_batch:
source_lengths.append(len(source))
yield pad_targets_batch,pad_sources_batch,targets_lengths,source_lengths
# Train
train_source = source_int[batch_size:]
train_target = target_int[batch_size:]
valid_source = source_int[:batch_size]
valid_target = target_int[:batch_size]
(valid_targets_batch, valid_sources_batch, valid_targets_lengths, valid_sources_lengths) = next(get_batches(valid_target, valid_source, batch_size,
source_letter_to_int['<PAD>'],
target_letter_to_int['<PAD>']))
display_step = 50
checkpoint = "data/trained_model.ckpt"
with tf.Session(graph=train_graph) as sess:
sess.run(tf.global_variables_initializer())
print()
for epoch_i in range(1,epochs+1):
for batch_i,(targets_batch, sources_batch, targets_lengths, sources_lengths) in enumerate(get_batches(
train_target,train_source,batch_size,source_letter_to_int['<PAD>'],
target_letter_to_int['<PAD>']
)):
_,loss = sess.run([train_op,cost],feed_dict={
input_data:sources_batch,
targets:targets_batch,
lr:learning_rate,
target_sequence_length:targets_lengths,
source_sequence_length:sources_lengths
})
if batch_i % display_step == 0:
validation_loss = sess.run(
[cost],
{input_data: valid_sources_batch,
targets: valid_targets_batch,
lr: learning_rate,
target_sequence_length: valid_targets_lengths,
source_sequence_length: valid_sources_lengths})
print('Epoch {:>3}/{} Batch {:>4}/{} - Training Loss: {:>6.3f} - Validation loss: {:>6.3f}'
.format(epoch_i,
epochs,
batch_i,
len(train_source) // batch_size,
loss,
validation_loss[0]))
saver = tf.train.Saver()
saver.save(sess, checkpoint)
print('Model Trained and Saved')
def source_to_seq(text):
sequence_length = 7
return [source_letter_to_int.get(word,source_letter_to_int['<UNK>']) for word in text] + [source_letter_to_int['<PAD>']] * (sequence_length - len(text))
input_word = ' '
text = source_to_seq(input_word)
checkpoint = "data/trained_model.ckpt"
loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
loader = tf.train.import_meta_graph(checkpoint+'.meta')
loader.restore(sess,checkpoint)
input_data = loaded_graph.get_tensor_by_name('inputs:0')
logits = loaded_graph.get_tensor_by_name('predictions:0')
source_sequence_length = loaded_graph.get_tensor_by_name('source_sequence_length:0')
target_sequence_length = loaded_graph.get_tensor_by_name('target_sequence_length:0')
answer_logits = sess.run(logits, {input_data: [text] * batch_size,
target_sequence_length: [len(input_word)] * batch_size,
source_sequence_length: [len(input_word)] * batch_size})[0]
pad = source_letter_to_int["<PAD>"]
print('原始输入:', input_word)
print('\nSource')
print(' Word 编号: {}'.format([i for i in text]))
print(' Input Words: {}'.format(" ".join([source_int_to_letter[i] for i in text])))
print('\nTarget')
print(' Word 编号: {}'.format([i for i in answer_logits if i != pad]))
print(' Response Words: {}'.format(" ".join([target_int_to_letter[i] for i in answer_logits if i != pad])))