/
chat_bot.py
319 lines (186 loc) · 9.03 KB
/
chat_bot.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
"""
Created on Thu May 5, 2020
@author: Sayak Banerjee
"""
import pickle
import numpy as np
import pandas as pd
#Load the texts
with open("train_qa.txt", "rb") as myfile: # Unpickling
train_data = pickle.load(myfile)
with open("test_qa.txt", "rb") as myfile: # Unpickling
test_data = pickle.load(myfile)
#Explore the format of the data
type(train_data)
type(test_data)
' '.join(train_data[0][0]) # --> Statement
' '.join(train_data[0][1]) # --> Question
train_data[0][2] # --> Answer
'''Build the Vocab'''
all_data = train_data + test_data
len(all_data)
vocab = set()
for statement, question, answer in all_data:
vocab = vocab.union(set(statement))
vocab = vocab.union(set(question))
vocab.add("yes")
vocab.add("no")
len(vocab)
vocab_size = len(vocab) + 1 # --> we add an extra space to hold a 0 for Keras's pad_sequences
#Find the Story with Max Length
max_story = []
for story, question, answer in all_data:
max_story.append(len(story))
max_story_len = max(max_story)
#Find the Question with Max Length
max_question = []
for story, question, answer in all_data:
max_question.append(len(question))
max_question_len = max(max_question)
'''Vectorize the Data'''
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(filters = '')
tokenizer.fit_on_texts(vocab)
tokenizer.word_index
#train_story_text = []
#train_question_text = []
#train_answer_text = []
#for story, question, answer in train_data:
# train_story_text.append(story)
# train_question_text.append(question)
#train_story_seq = tokenizer.texts_to_sequences(train_story_text)
#Now Lets Create a Function that will Vectorize data for us
def vectorize(data, word_index = tokenizer.word_index, max_story_len = max_story_len, max_question_len = max_question_len):
X = [] # --> For Stories
Xq = [] # --> For Questions
Y = [] # --> For Answers/Target
for story, question, answer in data:
x = []
xq = []
# --> For each story
for word in story:
x.append(word_index[word.lower()])
for word in question:
xq.append(word_index[word.lower()])
y = np.zeros(len(word_index) + 1)
y[word_index[answer]] = 1
X.append(x)
Xq.append(xq)
Y.append(y)
return (pad_sequences(X, maxlen = max_story_len), pad_sequences(Xq, maxlen = max_question_len),
np.array(Y))
inputs_train, questions_train, answers_train = vectorize(train_data)
inputs_test, questions_test, answers_test = vectorize(test_data)
sum(answers_train)
sum(answers_test)
'''Build the Model following the Paper on End-To-End Memory Networks'''
from keras.models import Sequential, Model
from keras.layers.embeddings import Embedding
from keras.layers import Input, Activation, Permute, Dense, Dropout
from keras.layers import LSTM
from keras.layers import add, dot, concatenate
#Create the Input Placeholder -> (Input() is used to instantiate a Keras tensor)
input_sequence = Input((max_story_len,)) #The Second Element is the unknown batch size
question = Input((max_question_len,))
#Input encoder M (Memory Units xi)
# .add is a method of the Sequential Class
input_encoder_m = Sequential()
input_encoder_m.add(Embedding(input_dim = vocab_size, output_dim = 64))
input_encoder_m.add(Dropout(0.3))
# This encoder will output:- (samples, story_maxlen, embedding_dim)
#Input encoder C
input_encoder_c = Sequential()
input_encoder_c.add(Embedding(input_dim = vocab_size,output_dim = max_question_len))
input_encoder_c.add(Dropout(0.3))
# This encoder will output: (samples, story_maxlen, query_maxlen)
#Question Encoder
question_encoder = Sequential()
question_encoder.add(Embedding(input_dim = vocab_size,
output_dim = 64,
input_length = max_question_len))
question_encoder.add(Dropout(0.3))
#Encode INPUT_SEQUENCE & the QUESTION
input_encoded_m = input_encoder_m(input_sequence)
input_encoded_c = input_encoder_c(input_sequence)
question_encoded = question_encoder(question)
#Dot product between input_encoded_m and question_encoded followed by a Softmax Activation
match = dot([input_encoded_m, question_encoded], axes=(2, 2))
match = Activation('softmax')(match)
#Next we take the Weighted sum of Input_encoded_m and match (Permutes the dimensions of the input according to a given pattern)
response = add([match, input_encoded_c]) # (samples, story_maxlen, query_maxlen)
response = Permute((2, 1))(response) # (samples, query_maxlen, story_maxlen)
#Now we Concatenate the response with the question_encoded (Layer that concatenates a list of inputs)
answer = concatenate([response, question_encoded])
answer
#Add a LSTM(RNN) Layer
answer = LSTM(units = 32)(answer)
#Regularize with a Dropout Layer and Add a Dense Layer
#Dense Layer effective does:- output = activation(dot(input, weights/kernels) + biases)
answer = Dropout(0.5)(answer)
answer = Dense(units = vocab_size)(answer)
#Finally Add a Softmax Activation Layer
answer = Activation('softmax')(answer)
#Build the Final Model
model = Model([input_sequence, question], answer)
#Compile the Model
model.compile( optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics= ['accuracy'])
model.summary()
#Fit the Model to the Training Data
model.fit([inputs_train, questions_train], answers_train,
batch_size = 32,
epochs = 120,
verbose = 1,
validation_data = ([inputs_test, questions_test], answers_test))
#Save the Weights
myfilename = 'chatbot.h5'
model.save(myfilename)
'''Evaluate the Model and Predict Results'''
predictions = model.predict(([inputs_test, questions_test]))
predictions[0]
first_story = ' '.join(test_data[0][0])
first_question = ' '.join(test_data[0][1])
first_answer = test_data[0][2]
print("First Story:- " + first_story)
print("First Question:- " + first_question)
print("Actual Test Data Answer is " + first_answer.upper())
#Predicted Answer for 1st Story and Question
index_max = np.argmax(predictions[0])
predicted_word = tokenizer.index_word[index_max]
print("\n")
print("Predicted Answer:- " + predicted_word.upper())
print("Probability of Certainly was:- " + str(predictions[0][index_max]))
# Now Lets Check the Accuracy of our Model on the Test Set
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report
#We will not be comparing the Yes or No's but compate the word_index
pred_labels = []
for each_prediction in predictions:
pred_labels.append(np.argmax(each_prediction))
df_pred_labels = pd.DataFrame(pred_labels, columns = ['Predicted_Labels'])
df_pred_labels['Predicted_Labels'].value_counts()
true_labels = []
for story, question, answer in test_data:
true_labels.append(tokenizer.word_index[answer])
df_true_labels = pd.DataFrame(pred_labels, columns = ['True_Labels'])
df_true_labels['True_Labels'].value_counts()
print(confusion_matrix(true_labels, pred_labels))
print("Accuracy of the Model is:- ", accuracy_score(true_labels, pred_labels) * 100, "%")
#Accuracy is > 80% --> Pretty Good
print(classification_report(true_labels, pred_labels))
'''Evaluate the Model with Your Own Data'''
#Make Sure your Story and Question has Words from the Vocab
my_story = "John went to the bedroom . Daniel grabbed the football dropped the football in the garden ."
my_question = "Is the football in the garden ?"
my_answer = "yes"
#Split the Data to be fed in to vectorize function
my_data = [(my_story.split(), my_question.split(), my_answer)]
my_vec_story, my_vec_question, my_vec_answer = vectorize(my_data)
my_story_pred = model.predict(([my_vec_story, my_vec_question]))
ind_max = np.argmax(my_story_pred)
my_predicted_word = tokenizer.index_word[ind_max]
print("My story:- ", my_story)
print("My Question:- ", my_question)
print("My Answer:- ", my_answer)
print("\n")
print("Predticted Answer to my Question:- " + my_predicted_word.upper())
print("Probability of Certainty of the Prediction:- ", str(my_story_pred[0][ind_max]))