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
|
- from __future__ import print_function
- import keras
- from keras.models import Sequential, Model
- from keras.layers.embeddings import Embedding
- from keras.layers import Permute, dot, add, concatenate
- from keras.layers import LSTM, Dense, Dropout, Input, Activation, GRU, SimpleRNN
- from keras.utils.data_utils import get_file
- from keras.preprocessing.sequence import pad_sequences
- from functools import reduce
- import tarfile
- import numpy as np
- import re
- import IPython
- import matplotlib.pyplot as plt
- import pandas as pd
- def tokenize(sent):
- # return [x.strip() for x in re.split('(\W+)?', sent) if x.strip()]
- return sent.strip('"(),-').lower().split(" ")
-
- def parse_stories(lines):
- '''Parse stories provided in the bAbi tasks format
- '''
- data = []
- story = []
- for line in lines:
- line = line.decode('utf-8').strip()
- nid, line = line.split(' ', 1)
- nid = int(nid)
- if nid == 1:
- story = []
- if '\t' in line:
- q, a, supporting = line.split('\t')
- q = tokenize(q)
- # Provide all the substories
- substory = [x for x in story if x]
- data.append((substory, q, a))
- story.append('')
- else:
- sent = tokenize(line)
- story.append(sent)
- return data
- def get_stories(f):
- data = parse_stories(f.readlines())
- flatten = lambda data: reduce(lambda x, y: x + y, data)
- data = [(flatten(story), q, answer) for story, q, answer in data]
- return data
- def vectorize_stories(data, word_idx, story_maxlen, query_maxlen):
- X = []
- Xq = []
- Y = []
- for story, query, answer in data:
- x = [word_idx[w] for w in story]
- xq = [word_idx[w] for w in query]
- # let's not forget that index 0 is reserved
- y = np.zeros(len(word_idx) + 1)
- y[word_idx[answer]] = 1
- X.append(x)
- Xq.append(xq)
- Y.append(y)
- return (pad_sequences(X, maxlen=story_maxlen),
- pad_sequences(Xq, maxlen=query_maxlen), np.array(Y))
-
- class TrainingVisualizer(keras.callbacks.History):
- def on_epoch_end(self, epoch, logs={}):
- super().on_epoch_end(epoch, logs)
- IPython.display.clear_output(wait=True)
- pd.DataFrame({key: value for key, value in self.history.items() if key.endswith('loss')}).plot()
- axes = pd.DataFrame({key: value for key, value in self.history.items() if key.endswith('acc')}).plot()
- axes.set_ylim([0, 1])
- plt.show()
- try:
- path = get_file('babi-tasks-v1-2.tar.gz', origin='https://s3.amazonaws.com/text-datasets/babi_tasks_1-20_v1-2.tar.gz')
- except:
- print('Error downloading dataset, please download it manually:\n'
- '$ wget http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz\n'
- '$ mv tasks_1-20_v1-2.tar.gz ~/.keras/datasets/babi-tasks-v1-2.tar.gz')
- raise
- tar = tarfile.open(path)
- #path = get_file('tasks_1-20_v1-2.tar.gz')
- #tar = tarfile.open(path)
- #challenge = 'tasks_1-20_v1-2/en-10k/qa1_single-supporting-fact_{}.txt'
- challenges = {
- # QA1 with 10,000 samples
- 'single_supporting_fact_10k': 'tasks_1-20_v1-2/en-10k/qa1_single-supporting-fact_{}.txt',
- # QA2 with 10,000 samples
- 'two_supporting_facts_10k': 'tasks_1-20_v1-2/en-10k/qa2_two-supporting-facts_{}.txt',
- }
- challenge_type = 'single_supporting_fact_10k'
- challenge = challenges[challenge_type]
- print('Extracting stories for the challenge: single_supporting_fact_10k', challenge_type)
- train_stories = get_stories(tar.extractfile(challenge.format('train')))
- test_stories = get_stories(tar.extractfile(challenge.format('test')))
- len(train_stories), len(test_stories)
- print('Number of training stories:', len(train_stories))
- print('Number of test stories:', len(test_stories))
- train_stories[0]
- vocab = sorted(reduce(lambda x, y: x | y, (set(story + q + [answer]) for story, q, answer in train_stories + test_stories)))
- #vocab = set()
- #for story, q, answer in train_stories + test_stories:
- # vocab |= set(story + q + [answer])
- #vocab = sorted(vocab)
- # Reserve 0 for masking via pad_sequences
- vocab_size = len(vocab) + 1
- story_maxlen = max(map(len, (x for x, _, _ in train_stories + test_stories)))
- query_maxlen = max(map(len, (x for _, x, _ in train_stories + test_stories)))
- word_idx = dict((c, i + 1) for i, c in enumerate(vocab))
- idx_word = dict((i+1, c) for i,c in enumerate(vocab))
- inputs_train, queries_train, answers_train = vectorize_stories(train_stories,
- word_idx,
- story_maxlen,
- query_maxlen)
- inputs_test, queries_test, answers_test = vectorize_stories(test_stories,
- word_idx,
- story_maxlen,
- query_maxlen)
- print('-------------------------')
- print('Vocabulary:\n',vocab,"\n")
- print('Vocab size:', vocab_size, 'unique words')
- print('Story max length:', story_maxlen, 'words')
- print('Query max length:', query_maxlen, 'words')
- print('Number of training stories:', len(train_stories))
- print('Number of test stories:', len(test_stories))
- print('-------------------------')
- print('-------------------------')
- print('inputs: integer tensor of shape (samples, max_length)')
- print('inputs_train shape:', inputs_train.shape)
- print('inputs_test shape:', inputs_test.shape)
- print('input train sample', inputs_train[0,:])
- print('-------------------------')
- print('-------------------------')
- print('queries: integer tensor of shape (samples, max_length)')
- print('queries_train shape:', queries_train.shape)
- print('queries_test shape:', queries_test.shape)
- print('query train sample', queries_train[0,:])
- print('-------------------------')
- print('-------------------------')
- print('answers: binary (1 or 0) tensor of shape (samples, vocab_size)')
- print('answers_train shape:', answers_train.shape)
- print('answers_test shape:', answers_test.shape)
- print('answer train sample', answers_train[0,:])
- print('-------------------------')
- train_epochs = 10
- batch_size = 32
- lstm_size = 64
- gru_size=64
- rnn_size=64
- embed_size = 50
- dropout_rate = 0.3
- # placeholders
- input_sequence = Input((story_maxlen,))
- question = Input((query_maxlen,))
- print('Input sequence:', input_sequence)
- print('Question:', question)
- # encoders
- # embed the input sequence into a sequence of vectors
- input_encoder_m = Sequential()
- input_encoder_m.add(Embedding(input_dim=vocab_size,
- output_dim=embed_size))
- input_encoder_m.add(Dropout(dropout_rate))
- # output: (samples, story_maxlen, embedding_dim)
- # embed the input into a sequence of vectors of size query_maxlen
- input_encoder_c = Sequential()
- input_encoder_c.add(Embedding(input_dim=vocab_size,
- output_dim=query_maxlen))
- input_encoder_c.add(Dropout(dropout_rate))
- # output: (samples, story_maxlen, query_maxlen)
- # embed the question into a sequence of vectors
- question_encoder = Sequential()
- question_encoder.add(Embedding(input_dim=vocab_size,
- output_dim=embed_size,
- input_length=query_maxlen))
- question_encoder.add(Dropout(dropout_rate))
- # output: (samples, query_maxlen, embedding_dim)
- # encode input sequence and questions (which are indices)
- # to sequences of dense vectors
- input_encoded_m = input_encoder_m(input_sequence)
- print('Input encoded m', input_encoded_m)
- input_encoded_c = input_encoder_c(input_sequence)
- print('Input encoded c', input_encoded_c)
- question_encoded = question_encoder(question)
- print('Question encoded', question_encoded)
- # compute a 'match' between the first input vector sequence
- # and the question vector sequence
- # shape: `(samples, story_maxlen, query_maxlen)
- match = dot([input_encoded_m, question_encoded], axes=-1, normalize=False)
- print(match.shape)
- match = Activation('softmax')(match)
- print('Match shape', match.shape)
- # add the match matrix with the second input vector sequence
- response = add([match, input_encoded_c]) # (samples, story_maxlen, query_maxlen)
- response = Permute((2, 1))(response) # (samples, query_maxlen, story_maxlen)
- print('Response shape', response)
- # concatenate the response vector with the question vector sequence
- answer = concatenate([response, question_encoded])
- print('Answer shape', answer)
- #answer = LSTM(lstm_size)(answer) # Generate tensors of shape 32
- #answer = GRU(gru_size)(answer)
- answer = SimpleRNN(rnn_size)(answer)
- answer = Dropout(dropout_rate)(answer)
- answer = Dense(vocab_size)(answer) # (samples, vocab_size)
- # we output a probability distribution over the vocabulary
- answer = Activation('softmax')(answer)
- # build the final model
- model = Model([input_sequence, question], answer)
- model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
- metrics=['accuracy'])
- model.summary()
- model.fit([inputs_train, queries_train], answers_train, batch_size, train_epochs, callbacks=[TrainingVisualizer()],
- validation_data=([inputs_test, queries_test], answers_test))
- model.save('model.h5')
- for i in range(0,10):
- current_inp = test_stories[i]
- current_story, current_query, current_answer = vectorize_stories([current_inp], word_idx, story_maxlen, query_maxlen)
- current_prediction = model.predict([current_story, current_query])
- current_prediction = idx_word[np.argmax(current_prediction)]
- print(' '.join(current_inp[0]), ' '.join(current_inp[1]), '| Prediction:', current_prediction, '| Ground Truth:', current_inp[2])
- print("-----------------------------------------------------------------------------------------")
- print('-------------------------------------------------------------------------------------------')
- print('Custom User Queries (Make sure there are spaces before each word)')
- while 1:
- print('-------------------------------------------------------------------------------------------')
- print('Please input a story')
- user_story_inp = input().split(' ')
- print('Please input a query')
- user_query_inp = input().split(' ')
- user_story, user_query, user_ans = vectorize_stories([[user_story_inp, user_query_inp, '.']], word_idx, story_maxlen, query_maxlen)
- user_prediction = model.predict([user_story, user_query])
- user_prediction = idx_word[np.argmax(user_prediction)]
- print('Result')
- print(' '.join(user_story_inp), ' '.join(user_query_inp), '| Prediction:', user_prediction)
-
- # Mary went to the bathroom . John moved to the hallway . Mary travelled to the office . # Where is Mary ?
- # Sandra travelled to the office . John journeyed to the garden .
|