forked from rajpatel0909/Generative-ChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chatbot_generative_train_LSTM.py
477 lines (372 loc) · 18 KB
/
Chatbot_generative_train_LSTM.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import pandas as pd
import numpy as np
import tensorflow as tf
import re
import time
print("preprocessing...")
movieL = open('movie_lines.txt', encoding='utf-8', errors='ignore').read().split('\n')
movieC = open('movie_conversations.txt', encoding='utf-8', errors='ignore').read().split('\n')
idToLine = {}
for l in movieL:
w = l.split(' +++$+++ ')
if len(w) == 5:
idToLine[w[0]] = w[4]
conversations = [ ]
for l in movieC[:-1]:
w = l.split(' +++$+++ ')[-1][1:-1].replace("'","").replace(" ","")
conversations.append(w.split(','))
context = []
responses = []
for conversation in conversations:
for i in range(len(conversation)-1):
context.append(idToLine[conversation[i]])
responses.append(idToLine[conversation[i+1]])
replacements = open('replacements.txt', 'r').read().split('\n')
def replaceText(txt):
txt = txt.lower()
for replace in replacements:
replacement = replace.split(',')
txt = re.sub(replacement[0], replacement[1], txt)
txt = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,]", "", txt)
return txt
replacedQues = []
for que in context:
replacedQues.append(replaceText(que))
replacedAns = []
for ans in responses:
replacedAns.append(replaceText(ans))
queTemp = []
ansTemp = []
i = 0
for que in replacedQues:
if len(que.split()) >= 2 and len(que.split()) <= 20:
queTemp.append(que)
ansTemp.append(replacedAns[i])
i += 1
smallQue = []
smallAns = []
i = 0
for ans in ansTemp:
if len(ans.split()) >= 2 and len(ans.split()) <= 20:
smallAns.append(ans)
smallQue.append(queTemp[i])
i += 1
vocabulary = {}
for que in smallQue:
for w in que.split():
if w not in vocabulary:
vocabulary[w] = 1
else:
vocabulary[w] += 1
for ans in smallAns:
for w in ans.split():
if w not in vocabulary:
vocabulary[w] = 1
else:
vocabulary[w] += 1
count = 0
for k,v in vocabulary.items():
if v >= 10:
count += 1
quev2int = {}
word_num = 0
for word, count in vocabulary.items():
if count >= 10:
quev2int[word] = word_num
word_num += 1
ansv2int = {}
word_num = 0
for word, count in vocabulary.items():
if count >= 10:
ansv2int[word] = word_num
word_num += 1
wilds = ['<PAD>','<EOS>','<UNK>','<GO>']
for wild in wilds:
quev2int[wild] = len(quev2int)+1
for wild in wilds:
ansv2int[wild] = len(ansv2int)+1
queint2v = {v_i: v for v, v_i in quev2int.items()}
ansint2v = {v_i: v for v, v_i in ansv2int.items()}
for i in range(len(smallAns)):
smallAns[i] += ' <EOS>'
queInt = []
for que in smallQue:
ints = []
for w in que.split():
if w not in quev2int:
ints.append(quev2int['<UNK>'])
else:
ints.append(quev2int[w])
queInt.append(ints)
ansInt = []
for ans in smallAns:
ints = []
for w in ans.split():
if w not in ansv2int:
ints.append(ansv2int['<UNK>'])
else:
ints.append(ansv2int[w])
ansInt.append(ints)
def model_inputs():
'''Create palceholders for inputs to the model'''
input_data = tf.placeholder(tf.int32, [None, None], name='input')
targets = tf.placeholder(tf.int32, [None, None], name='targets')
lr = tf.placeholder(tf.float32, name='learning_rate')
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
return input_data, targets, lr, keep_prob
# In[32]:
def process_encoding_input(target_data, vocab_to_int, batch_size):
'''Remove the last word id from each batch and concat the <GO> to the begining of each batch'''
ending = tf.strided_slice(target_data, [0, 0], [batch_size, -1], [1, 1])
dec_input = tf.concat([tf.fill([batch_size, 1], vocab_to_int['<GO>']), ending], 1)
return dec_input
# In[33]:
def encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob, sequence_length):
'''Create the encoding layer'''
lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
drop = tf.contrib.rnn.DropoutWrapper(lstm, input_keep_prob = keep_prob)
enc_cell = tf.contrib.rnn.MultiRNNCell([drop] * num_layers)
_, enc_state = tf.nn.bidirectional_dynamic_rnn(cell_fw = enc_cell,
cell_bw = enc_cell,
sequence_length = sequence_length,
inputs = rnn_inputs,
dtype=tf.float32)
return enc_state
# In[34]:
def decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope,
output_fn, keep_prob, batch_size):
'''Decode the training data'''
attention_states = tf.zeros([batch_size, 1, dec_cell.output_size])
att_keys, att_vals, att_score_fn, att_construct_fn = tf.contrib.seq2seq.prepare_attention(attention_states,
attention_option="bahdanau",
num_units=dec_cell.output_size)
train_decoder_fn = tf.contrib.seq2seq.attention_decoder_fn_train(encoder_state[0],
att_keys,
att_vals,
att_score_fn,
att_construct_fn,
name = "attn_dec_train")
train_pred, _, _ = tf.contrib.seq2seq.dynamic_rnn_decoder(dec_cell,
train_decoder_fn,
dec_embed_input,
sequence_length,
scope=decoding_scope)
train_pred_drop = tf.nn.dropout(train_pred, keep_prob)
return output_fn(train_pred_drop)
# In[35]:
def decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id,
maximum_length, vocab_size, decoding_scope, output_fn, keep_prob, batch_size):
'''Decode the prediction data'''
attention_states = tf.zeros([batch_size, 1, dec_cell.output_size])
att_keys, att_vals, att_score_fn, att_construct_fn = tf.contrib.seq2seq.prepare_attention(attention_states,
attention_option="bahdanau",
num_units=dec_cell.output_size)
infer_decoder_fn = tf.contrib.seq2seq.attention_decoder_fn_inference(output_fn,
encoder_state[0],
att_keys,
att_vals,
att_score_fn,
att_construct_fn,
dec_embeddings,
start_of_sequence_id,
end_of_sequence_id,
maximum_length,
vocab_size,
name = "attn_dec_inf")
infer_logits, _, _ = tf.contrib.seq2seq.dynamic_rnn_decoder(dec_cell,
infer_decoder_fn,
scope=decoding_scope)
return infer_logits
# In[36]:
def decoding_layer(dec_embed_input, dec_embeddings, encoder_state, vocab_size, sequence_length, rnn_size,
num_layers, vocab_to_int, keep_prob, batch_size):
'''Create the decoding cell and input the parameters for the training and inference decoding layers'''
with tf.variable_scope("decoding") as decoding_scope:
lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
drop = tf.contrib.rnn.DropoutWrapper(lstm, input_keep_prob = keep_prob)
dec_cell = tf.contrib.rnn.MultiRNNCell([drop] * num_layers)
weights = tf.truncated_normal_initializer(stddev=0.1)
biases = tf.zeros_initializer()
output_fn = lambda x: tf.contrib.layers.fully_connected(x,
vocab_size,
None,
scope=decoding_scope,
weights_initializer = weights,
biases_initializer = biases)
train_logits = decoding_layer_train(encoder_state,
dec_cell,
dec_embed_input,
sequence_length,
decoding_scope,
output_fn,
keep_prob,
batch_size)
decoding_scope.reuse_variables()
infer_logits = decoding_layer_infer(encoder_state,
dec_cell,
dec_embeddings,
vocab_to_int['<GO>'],
vocab_to_int['<EOS>'],
sequence_length - 1,
vocab_size,
decoding_scope,
output_fn, keep_prob,
batch_size)
return train_logits, infer_logits
# In[37]:
def seq2seq_model(input_data, target_data, keep_prob, batch_size, sequence_length, answers_vocab_size,
questions_vocab_size, enc_embedding_size, dec_embedding_size, rnn_size, num_layers,
questions_vocab_to_int):
'''Use the previous functions to create the training and inference logits'''
enc_embed_input = tf.contrib.layers.embed_sequence(input_data,
answers_vocab_size+1,
enc_embedding_size,
initializer = tf.random_uniform_initializer(0,1))
enc_state = encoding_layer(enc_embed_input, rnn_size, num_layers, keep_prob, sequence_length)
dec_input = process_encoding_input(target_data, questions_vocab_to_int, batch_size)
dec_embeddings = tf.Variable(tf.random_uniform([questions_vocab_size+1, dec_embedding_size], 0, 1))
dec_embed_input = tf.nn.embedding_lookup(dec_embeddings, dec_input)
train_logits, infer_logits = decoding_layer(dec_embed_input,
dec_embeddings,
enc_state,
questions_vocab_size,
sequence_length,
rnn_size,
num_layers,
questions_vocab_to_int,
keep_prob,
batch_size)
return train_logits, infer_logits
# In[38]:
# Set the Hyperparameters
epochs = 100
batch_size = 128
rnn_size = 512
num_layers = 2
encoding_embedding_size = 512
decoding_embedding_size = 512
learning_rate = 0.005
learning_rate_decay = 0.9
min_learning_rate = 0.0001
keep_probability = 0.75
# In[39]:
# Reset the graph to ensure that it is ready for training
tf.reset_default_graph()
# Start the session
sess = tf.InteractiveSession()
# Load the model inputs
input_data, targets, lr, keep_prob = model_inputs()
# Sequence length will be the max line length for each batch
sequence_length = tf.placeholder_with_default(max_line_length, None, name='sequence_length')
# Find the shape of the input data for sequence_loss
input_shape = tf.shape(input_data)
# Create the training and inference logits
train_logits, inference_logits = seq2seq_model(
tf.reverse(input_data, [-1]), targets, keep_prob, batch_size, sequence_length, len(answers_vocab_to_int),
len(questions_vocab_to_int), encoding_embedding_size, decoding_embedding_size, rnn_size, num_layers,
questions_vocab_to_int)
# Create a tensor for the inference logits, needed if loading a checkpoint version of the model
tf.identity(inference_logits, 'logits')
with tf.name_scope("optimization"):
# Loss function
cost = tf.contrib.seq2seq.sequence_loss(
train_logits,
targets,
tf.ones([input_shape[0], sequence_length]))
# Optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
# Gradient Clipping
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)
# In[40]:
def pad_sentence_batch(sentence_batch, vocab_to_int):
"""Pad sentences with <PAD> so that each sentence of a batch has the same length"""
max_sentence = max([len(sentence) for sentence in sentence_batch])
return [sentence + [vocab_to_int['<PAD>']] * (max_sentence - len(sentence)) for sentence in sentence_batch]
# In[41]:
def batch_data(questions, answers, batch_size):
"""Batch questions and answers together"""
for batch_i in range(0, len(questions)//batch_size):
start_i = batch_i * batch_size
questions_batch = questions[start_i:start_i + batch_size]
answers_batch = answers[start_i:start_i + batch_size]
pad_questions_batch = np.array(pad_sentence_batch(questions_batch, questions_vocab_to_int))
pad_answers_batch = np.array(pad_sentence_batch(answers_batch, answers_vocab_to_int))
yield pad_questions_batch, pad_answers_batch
# In[42]:
# Validate the training with 10% of the data
train_valid_split = int(len(sorted_questions)*0.15)
# Split the questions and answers into training and validating data
train_questions = sorted_questions[train_valid_split:]
train_answers = sorted_answers[train_valid_split:]
valid_questions = sorted_questions[:train_valid_split]
valid_answers = sorted_answers[:train_valid_split]
print(len(train_questions))
print(len(valid_questions))
# In[43]:
display_step = 100 # Check training loss after every 100 batches
stop_early = 0
stop = 5 # If the validation loss does decrease in 5 consecutive checks, stop training
validation_check = ((len(train_questions))//batch_size//2)-1 # Modulus for checking validation loss
total_train_loss = 0 # Record the training loss for each display step
summary_valid_loss = [] # Record the validation loss for saving improvements in the model
checkpoint = "/output/best_model.ckpt"
sess.run(tf.global_variables_initializer())
for epoch_i in range(1, epochs+1):
for batch_i, (questions_batch, answers_batch) in enumerate(
batch_data(train_questions, train_answers, batch_size)):
start_time = time.time()
_, loss = sess.run(
[train_op, cost],
{input_data: questions_batch,
targets: answers_batch,
lr: learning_rate,
sequence_length: answers_batch.shape[1],
keep_prob: keep_probability})
total_train_loss += loss
end_time = time.time()
batch_time = end_time - start_time
if batch_i % display_step == 0:
print('Epoch {:>3}/{} Batch {:>4}/{} - Loss: {:>6.3f}, Seconds: {:>4.2f}'
.format(epoch_i,
epochs,
batch_i,
len(train_questions) // batch_size,
total_train_loss / display_step,
batch_time*display_step))
total_train_loss = 0
if batch_i % validation_check == 0 and batch_i > 0:
total_valid_loss = 0
start_time = time.time()
for batch_ii, (questions_batch, answers_batch) in \
enumerate(batch_data(valid_questions, valid_answers, batch_size)):
valid_loss = sess.run(
cost, {input_data: questions_batch,
targets: answers_batch,
lr: learning_rate,
sequence_length: answers_batch.shape[1],
keep_prob: 1})
total_valid_loss += valid_loss
end_time = time.time()
batch_time = end_time - start_time
avg_valid_loss = total_valid_loss / (len(valid_questions) / batch_size)
print('Valid Loss: {:>6.3f}, Seconds: {:>5.2f}'.format(avg_valid_loss, batch_time))
# Reduce learning rate, but not below its minimum value
learning_rate *= learning_rate_decay
if learning_rate < min_learning_rate:
learning_rate = min_learning_rate
summary_valid_loss.append(avg_valid_loss)
if avg_valid_loss <= min(summary_valid_loss):
print('New Record!')
stop_early = 0
saver = tf.train.Saver()
saver.save(sess, checkpoint)
else:
print("No Improvement.")
stop_early += 1
if stop_early == stop:
break
if stop_early == stop:
print("Stopping Training.")
break