-
Notifications
You must be signed in to change notification settings - Fork 2
/
bAbI_completed.py
203 lines (146 loc) · 6.28 KB
/
bAbI_completed.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
from __future__ import print_function
from functools import reduce
import re
import numpy as np
from keras.layers.embeddings import Embedding
from keras import layers
from keras.layers import recurrent
from keras.models import Model
from keras.preprocessing.sequence import pad_sequences
def tokenize(sent):
return [x.strip() for x in re.split('(\W+)?', sent) if x.strip()]
def parse_stories(lines, only_supporting=False):
data = []
story = []
for line in lines:
line = line.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)
substory = None
if only_supporting:
# Only select the related substory
supporting = map(int, supporting.split())
substory = [story[i - 1] for i in supporting]
else:
# 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, only_supporting=False, max_length=None):
data = parse_stories(f.readlines(), only_supporting=only_supporting)
flatten = lambda data: reduce(lambda x, y: x + y, data)
data = [(flatten(story), q, answer) for story, q, answer in data if not max_length or len(flatten(story)) < max_length]
return data
def vectorize_stories(data, word_idx, story_maxlen, query_maxlen):
xs = []
xqs = []
ys = []
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
xs.append(x)
xqs.append(xq)
ys.append(y)
return pad_sequences(xs, maxlen=story_maxlen), pad_sequences(xqs, maxlen=query_maxlen), np.array(ys)
def vectorize_question(story, query, word_idx, story_maxlen, query_maxlen):
xs = []
xqs = []
stories = tokenize(story)
x = [word_idx[w] for w in stories]
# let's not forget that index 0 is reserved
xs.append(x)
xq = [word_idx[w] for w in query.split()]
xqs.append(xq)
return pad_sequences(xs, maxlen=story_maxlen), pad_sequences(xqs, maxlen=query_maxlen)
def categorize_stories(data, word_idx ):
rev_word_idx = []
for item in data:
if item != 0:
rev_word_idx.append(list(word_idx.keys())[list(word_idx.values()).index(item)])
return rev_word_idx
def get_model(vocab_size, story_maxlen, query_maxlen):
RNN = recurrent.LSTM
EMBED_HIDDEN_SIZE = 50
BATCH_SIZE = 16
EPOCHS = 400
sentence = layers.Input(shape=(story_maxlen,), dtype='int32')
encoded_sentence = layers.Embedding(vocab_size, EMBED_HIDDEN_SIZE)(sentence)
encoded_sentence = layers.Dropout(0.3)(encoded_sentence)
question = layers.Input(shape=(query_maxlen,), dtype='int32')
encoded_question = layers.Embedding(vocab_size, EMBED_HIDDEN_SIZE)(question)
encoded_question = layers.Dropout(0.3)(encoded_question)
encoded_question = RNN(EMBED_HIDDEN_SIZE)(encoded_question)
encoded_question = layers.RepeatVector(story_maxlen)(encoded_question)
merged = layers.add([encoded_sentence, encoded_question])
merged = RNN(EMBED_HIDDEN_SIZE)(merged)
merged = layers.Dropout(0.3)(merged)
preds = layers.Dense(vocab_size, activation='softmax')(merged)
model = Model([sentence, question], preds)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model, BATCH_SIZE, EPOCHS
def train_model(model, x, xq, y, tx, txq, ty, BATCH_SIZE, EPOCHS ):
model.fit([x, xq], y,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
validation_split=0.05)
loss, acc = model.evaluate([tx, txq], ty, batch_size=BATCH_SIZE)
print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc))
model.save_weights('data/model_weights.h5')
print('Model Saved !!!')
return
def get_data():
train = get_stories(open('tasks/en/qa2_two-supporting-facts_train.txt'))
test = get_stories(open('tasks/en/qa2_two-supporting-facts_test.txt'))
vocab = set()
for story, q, answer in train + test:
vocab |= set(story + q + [answer])
vocab = sorted(vocab)
# Reserve 0 for masking via pad_sequences
vocab_size = len(vocab) + 1
word_idx = dict((c, i + 1) for i, c in enumerate(vocab))
story_maxlen = max(map(len, (x for x, _, _ in train + test)))
query_maxlen = max(map(len, (x for _, x, _ in train + test)))
x, xq, y = vectorize_stories(train, word_idx, story_maxlen, query_maxlen)
tx, txq, ty = vectorize_stories(test, word_idx, story_maxlen, query_maxlen)
return x, xq, y, tx, txq, ty, vocab_size, word_idx, story_maxlen, query_maxlen
def load_model():
x, xq, y, tx, txq, ty, vocab_size, word_idx, story_maxlen, query_maxlen = get_data()
model, BATCH_SIZE, EPOCHS = get_model(vocab_size, story_maxlen, query_maxlen)
model.load_weights('data/model_weights.h5')
predicted = []
stories = input("Enter the story: ")
query = input("Enter your query : ")
testx, testxq = vectorize_question(stories, query, word_idx, story_maxlen, query_maxlen)
prediction = model.predict([testx, testxq])
predicted.append(prediction.argmax())
predicted = categorize_stories(predicted, word_idx)
print('Answer : ', predicted[0])
train = input('Do you want to train the model? (yes/no)')
if train.strip() == 'yes':
x, xq, y, tx, txq, ty, vocab_size, word_idx, story_maxlen, query_maxlen = get_data()
model, BATCH_SIZE, EPOCHS = get_model(vocab_size, story_maxlen, query_maxlen)
train_model(model, x, xq, y, tx, txq, ty, BATCH_SIZE, EPOCHS)
load_model()
elif train.strip() == 'no':
load_model()
# Sample Input : Mary got the milk there. John moved to the bedroom. Sandra went back to the kitchen. Mary travelled to the hallway.
# Sample Query : Where is the milk
#
# Corresponding Out : 'hallway'.
# More samples can be found in tasks/en/qa2_two-supporting_facts_test.txt
else:
print('Invalid Input')