forked from Currie32/Chatbot-from-Movie-Dialogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chatbot_Attention.py
777 lines (565 loc) · 26.2 KB
/
Chatbot_Attention.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# coding: utf-8
# # Building a Chatbot
# In this project, we will build a chatbot using conversations from Cornell University's [Movie Dialogue Corpus](https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html). The main features of our model are LSTM cells, a bidirectional dynamic RNN, and decoders with attention.
#
# The conversations will be cleaned rather extensively to help the model to produce better responses. As part of the cleaning process, punctuation will be removed, rare words will be replaced with "UNK" (our "unknown" token), longer sentences will not be used, and all letters will be in the lowercase.
#
# With a larger amount of data, it would be more practical to keep features, such as punctuation. However, I am using FloydHub's GPU services and I don't want to get carried away with too training for too long.
# In[1]:
import pandas as pd
import numpy as np
import tensorflow as tf
import re
import time
tf.__version__
# Most of the code to load the data is courtesy of https://github.com/suriyadeepan/practical_seq2seq/blob/master/datasets/cornell_corpus/data.py.
# ### Inspect and Load the Data
# In[2]:
# Load the data
lines = open('movie_lines.txt', encoding='utf-8', errors='ignore').read().split('\n')
conv_lines = open('movie_conversations.txt', encoding='utf-8', errors='ignore').read().split('\n')
# In[3]:
# The sentences that we will be using to train our model.
lines[:10]
# In[4]:
# The sentences' ids, which will be processed to become our input and target data.
conv_lines[:10]
# In[5]:
# Create a dictionary to map each line's id with its text
id2line = {}
for line in lines:
_line = line.split(' +++$+++ ')
if len(_line) == 5:
id2line[_line[0]] = _line[4]
# In[6]:
# Create a list of all of the conversations' lines' ids.
convs = [ ]
for line in conv_lines[:-1]:
_line = line.split(' +++$+++ ')[-1][1:-1].replace("'","").replace(" ","")
convs.append(_line.split(','))
# In[7]:
convs[:10]
# In[8]:
# Sort the sentences into questions (inputs) and answers (targets)
questions = []
answers = []
for conv in convs:
for i in range(len(conv)-1):
questions.append(id2line[conv[i]])
answers.append(id2line[conv[i+1]])
# In[9]:
# Check if we have loaded the data correctly
limit = 0
for i in range(limit, limit+5):
print(questions[i])
print(answers[i])
print()
# In[10]:
# Compare lengths of questions and answers
print(len(questions))
print(len(answers))
# In[11]:
def clean_text(text):
'''Clean text by removing unnecessary characters and altering the format of words.'''
text = text.lower()
text = re.sub(r"i'm", "i am", text)
text = re.sub(r"he's", "he is", text)
text = re.sub(r"she's", "she is", text)
text = re.sub(r"it's", "it is", text)
text = re.sub(r"that's", "that is", text)
text = re.sub(r"what's", "that is", text)
text = re.sub(r"where's", "where is", text)
text = re.sub(r"how's", "how is", text)
text = re.sub(r"\'ll", " will", text)
text = re.sub(r"\'ve", " have", text)
text = re.sub(r"\'re", " are", text)
text = re.sub(r"\'d", " would", text)
text = re.sub(r"\'re", " are", text)
text = re.sub(r"won't", "will not", text)
text = re.sub(r"can't", "cannot", text)
text = re.sub(r"n't", " not", text)
text = re.sub(r"n'", "ng", text)
text = re.sub(r"'bout", "about", text)
text = re.sub(r"'til", "until", text)
text = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,]", "", text)
return text
# In[12]:
# Clean the data
clean_questions = []
for question in questions:
clean_questions.append(clean_text(question))
clean_answers = []
for answer in answers:
clean_answers.append(clean_text(answer))
# In[13]:
# Take a look at some of the data to ensure that it has been cleaned well.
limit = 0
for i in range(limit, limit+5):
print(clean_questions[i])
print(clean_answers[i])
print()
# In[14]:
# Find the length of sentences
lengths = []
for question in clean_questions:
lengths.append(len(question.split()))
for answer in clean_answers:
lengths.append(len(answer.split()))
# Create a dataframe so that the values can be inspected
lengths = pd.DataFrame(lengths, columns=['counts'])
# In[15]:
lengths.describe()
# In[16]:
print(np.percentile(lengths, 80))
print(np.percentile(lengths, 85))
print(np.percentile(lengths, 90))
print(np.percentile(lengths, 95))
print(np.percentile(lengths, 99))
# In[17]:
# Remove questions and answers that are shorter than 2 words and longer than 20 words.
min_line_length = 2
max_line_length = 20
# Filter out the questions that are too short/long
short_questions_temp = []
short_answers_temp = []
i = 0
for question in clean_questions:
if len(question.split()) >= min_line_length and len(question.split()) <= max_line_length:
short_questions_temp.append(question)
short_answers_temp.append(clean_answers[i])
i += 1
# Filter out the answers that are too short/long
short_questions = []
short_answers = []
i = 0
for answer in short_answers_temp:
if len(answer.split()) >= min_line_length and len(answer.split()) <= max_line_length:
short_answers.append(answer)
short_questions.append(short_questions_temp[i])
i += 1
# In[18]:
# Compare the number of lines we will use with the total number of lines.
print("# of questions:", len(short_questions))
print("# of answers:", len(short_answers))
print("% of data used: {}%".format(round(len(short_questions)/len(questions),4)*100))
# In[19]:
# Create a dictionary for the frequency of the vocabulary
vocab = {}
for question in short_questions:
for word in question.split():
if word not in vocab:
vocab[word] = 1
else:
vocab[word] += 1
for answer in short_answers:
for word in answer.split():
if word not in vocab:
vocab[word] = 1
else:
vocab[word] += 1
# In[20]:
# Remove rare words from the vocabulary.
# We will aim to replace fewer than 5% of words with <UNK>
# You will see this ratio soon.
threshold = 10
count = 0
for k,v in vocab.items():
if v >= threshold:
count += 1
# In[21]:
print("Size of total vocab:", len(vocab))
print("Size of vocab we will use:", count)
# In[22]:
# In case we want to use a different vocabulary sizes for the source and target text,
# we can set different threshold values.
# Nonetheless, we will create dictionaries to provide a unique integer for each word.
questions_vocab_to_int = {}
word_num = 0
for word, count in vocab.items():
if count >= threshold:
questions_vocab_to_int[word] = word_num
word_num += 1
answers_vocab_to_int = {}
word_num = 0
for word, count in vocab.items():
if count >= threshold:
answers_vocab_to_int[word] = word_num
word_num += 1
# In[23]:
# Add the unique tokens to the vocabulary dictionaries.
codes = ['<PAD>','<EOS>','<UNK>','<GO>']
for code in codes:
questions_vocab_to_int[code] = len(questions_vocab_to_int)+1
for code in codes:
answers_vocab_to_int[code] = len(answers_vocab_to_int)+1
# In[24]:
# Create dictionaries to map the unique integers to their respective words.
# i.e. an inverse dictionary for vocab_to_int.
questions_int_to_vocab = {v_i: v for v, v_i in questions_vocab_to_int.items()}
answers_int_to_vocab = {v_i: v for v, v_i in answers_vocab_to_int.items()}
# In[25]:
# Check the length of the dictionaries.
print(len(questions_vocab_to_int))
print(len(questions_int_to_vocab))
print(len(answers_vocab_to_int))
print(len(answers_int_to_vocab))
# In[26]:
# Add the end of sentence token to the end of every answer.
for i in range(len(short_answers)):
short_answers[i] += ' <EOS>'
# In[27]:
# Convert the text to integers.
# Replace any words that are not in the respective vocabulary with <UNK>
questions_int = []
for question in short_questions:
ints = []
for word in question.split():
if word not in questions_vocab_to_int:
ints.append(questions_vocab_to_int['<UNK>'])
else:
ints.append(questions_vocab_to_int[word])
questions_int.append(ints)
answers_int = []
for answer in short_answers:
ints = []
for word in answer.split():
if word not in answers_vocab_to_int:
ints.append(answers_vocab_to_int['<UNK>'])
else:
ints.append(answers_vocab_to_int[word])
answers_int.append(ints)
# In[28]:
# Check the lengths
print(len(questions_int))
print(len(answers_int))
# In[29]:
# Calculate what percentage of all words have been replaced with <UNK>
word_count = 0
unk_count = 0
for question in questions_int:
for word in question:
if word == questions_vocab_to_int["<UNK>"]:
unk_count += 1
word_count += 1
for answer in answers_int:
for word in answer:
if word == answers_vocab_to_int["<UNK>"]:
unk_count += 1
word_count += 1
unk_ratio = round(unk_count/word_count,4)*100
print("Total number of words:", word_count)
print("Number of times <UNK> is used:", unk_count)
print("Percent of words that are <UNK>: {}%".format(round(unk_ratio,3)))
# In[30]:
# Sort questions and answers by the length of questions.
# This will reduce the amount of padding during training
# Which should speed up training and help to reduce the loss
sorted_questions = []
sorted_answers = []
for length in range(1, max_line_length+1):
for i in enumerate(questions_int):
if len(i[1]) == length:
sorted_questions.append(questions_int[i[0]])
sorted_answers.append(answers_int[i[0]])
print(len(sorted_questions))
print(len(sorted_answers))
print()
for i in range(3):
print(sorted_questions[i])
print(sorted_answers[i])
print()
# In[31]:
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 = "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
# In[44]:
def question_to_seq(question, vocab_to_int):
'''Prepare the question for the model'''
question = clean_text(question)
return [vocab_to_int.get(word, vocab_to_int['<UNK>']) for word in question.split()]
# In[60]:
# Create your own input question
#input_question = 'How are you?'
# Use a question from the data as your input
random = np.random.choice(len(short_questions))
input_question = short_questions[random]
# Prepare the question
input_question = question_to_seq(input_question, questions_vocab_to_int)
# Pad the questions until it equals the max_line_length
input_question = input_question + [questions_vocab_to_int["<PAD>"]] * (max_line_length - len(input_question))
# Add empty questions so the the input_data is the correct shape
batch_shell = np.zeros((batch_size, max_line_length))
# Set the first question to be out input question
batch_shell[0] = input_question
# Run the model with the input question
answer_logits = sess.run(inference_logits, {input_data: batch_shell,
keep_prob: 1.0})[0]
# Remove the padding from the Question and Answer
pad_q = questions_vocab_to_int["<PAD>"]
pad_a = answers_vocab_to_int["<PAD>"]
print('Question')
print(' Word Ids: {}'.format([i for i in input_question if i != pad_q]))
print(' Input Words: {}'.format([questions_int_to_vocab[i] for i in input_question if i != pad_q]))
print('\nAnswer')
print(' Word Ids: {}'.format([i for i in np.argmax(answer_logits, 1) if i != pad_a]))
print(' Response Words: {}'.format([answers_int_to_vocab[i] for i in np.argmax(answer_logits, 1) if i != pad_a]))
# In[ ]: