-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathbabi_input.py
315 lines (266 loc) · 12.2 KB
/
babi_input.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
from __future__ import division
from __future__ import print_function
import sys
import os as os
import numpy as np
# can be sentence or word
input_mask_mode = "sentence"
# adapted from https://github.com/YerevaNN/Dynamic-memory-networks-in-Theano/
def init_babi(fname):
print("==> Loading test from %s" % fname)
tasks = []
task = None
for i, line in enumerate(open(fname)):
id = int(line[0:line.find(' ')])
if id == 1:
task = {"C": "", "Q": "", "A": "", "S": ""}
counter = 0
id_map = {}
line = line.strip()
line = line.replace('.', ' . ')
line = line[line.find(' ')+1:]
# if not a question
if line.find('?') == -1:
task["C"] += line
id_map[id] = counter
counter += 1
else:
idx = line.find('?')
tmp = line[idx+1:].split('\t')
task["Q"] = line[:idx]
task["A"] = tmp[1].strip()
task["S"] = []
for num in tmp[2].split():
task["S"].append(id_map[int(num.strip())])
tasks.append(task.copy())
return tasks
def get_babi_raw(id, test_id):
babi_map = {
"1": "qa1_single-supporting-fact",
"2": "qa2_two-supporting-facts",
"3": "qa3_three-supporting-facts",
"4": "qa4_two-arg-relations",
"5": "qa5_three-arg-relations",
"6": "qa6_yes-no-questions",
"7": "qa7_counting",
"8": "qa8_lists-sets",
"9": "qa9_simple-negation",
"10": "qa10_indefinite-knowledge",
"11": "qa11_basic-coreference",
"12": "qa12_conjunction",
"13": "qa13_compound-coreference",
"14": "qa14_time-reasoning",
"15": "qa15_basic-deduction",
"16": "qa16_basic-induction",
"17": "qa17_positional-reasoning",
"18": "qa18_size-reasoning",
"19": "qa19_path-finding",
"20": "qa20_agents-motivations",
"MCTest": "MCTest",
"19changed": "19changed",
"joint": "all_shuffled",
"sh1": "../shuffled/qa1_single-supporting-fact",
"sh2": "../shuffled/qa2_two-supporting-facts",
"sh3": "../shuffled/qa3_three-supporting-facts",
"sh4": "../shuffled/qa4_two-arg-relations",
"sh5": "../shuffled/qa5_three-arg-relations",
"sh6": "../shuffled/qa6_yes-no-questions",
"sh7": "../shuffled/qa7_counting",
"sh8": "../shuffled/qa8_lists-sets",
"sh9": "../shuffled/qa9_simple-negation",
"sh10": "../shuffled/qa10_indefinite-knowledge",
"sh11": "../shuffled/qa11_basic-coreference",
"sh12": "../shuffled/qa12_conjunction",
"sh13": "../shuffled/qa13_compound-coreference",
"sh14": "../shuffled/qa14_time-reasoning",
"sh15": "../shuffled/qa15_basic-deduction",
"sh16": "../shuffled/qa16_basic-induction",
"sh17": "../shuffled/qa17_positional-reasoning",
"sh18": "../shuffled/qa18_size-reasoning",
"sh19": "../shuffled/qa19_path-finding",
"sh20": "../shuffled/qa20_agents-motivations",
}
if (test_id == ""):
test_id = id
babi_name = babi_map[id]
babi_test_name = babi_map[test_id]
babi_train_raw = init_babi(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/en-10k/%s_train.txt' % babi_name))
babi_test_raw = init_babi(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/en-10k/%s_test.txt' % babi_test_name))
return babi_train_raw, babi_test_raw
def load_glove(dim):
word2vec = {}
print("==> loading glove")
with open(("./data/glove/glove.6B/glove.6B." + str(dim) + "d.txt")) as f:
for line in f:
l = line.split()
word2vec[l[0]] = map(float, l[1:])
print("==> glove is loaded")
return word2vec
def create_vector(word, word2vec, word_vector_size, silent=True):
# if the word is missing from Glove, create some fake vector and store in glove!
vector = np.random.uniform(0.0,1.0,(word_vector_size,))
word2vec[word] = vector
if (not silent):
print("utils.py::create_vector => %s is missing" % word)
return vector
def process_word(word, word2vec, vocab, ivocab, word_vector_size, to_return="word2vec", silent=True):
if not word in word2vec:
create_vector(word, word2vec, word_vector_size, silent)
if not word in vocab:
next_index = len(vocab)
vocab[word] = next_index
ivocab[next_index] = word
if to_return == "word2vec":
return word2vec[word]
elif to_return == "index":
return vocab[word]
elif to_return == "onehot":
raise Exception("to_return = 'onehot' is not implemented yet")
def process_input(data_raw, floatX, word2vec, vocab, ivocab, embed_size, split_sentences=False):
questions = []
inputs = []
answers = []
input_masks = []
for x in data_raw:
if split_sentences:
inp = x["C"].lower().split(' . ')
inp = [w for w in inp if len(w) > 0]
inp = [i.split() for i in inp]
else:
inp = x["C"].lower().split(' ')
inp = [w for w in inp if len(w) > 0]
q = x["Q"].lower().split(' ')
q = [w for w in q if len(w) > 0]
if split_sentences:
inp_vector = [[process_word(word = w,
word2vec = word2vec,
vocab = vocab,
ivocab = ivocab,
word_vector_size = embed_size,
to_return = "index") for w in s] for s in inp]
else:
inp_vector = [process_word(word = w,
word2vec = word2vec,
vocab = vocab,
ivocab = ivocab,
word_vector_size = embed_size,
to_return = "index") for w in inp]
q_vector = [process_word(word = w,
word2vec = word2vec,
vocab = vocab,
ivocab = ivocab,
word_vector_size = embed_size,
to_return = "index") for w in q]
if split_sentences:
inputs.append(inp_vector)
else:
inputs.append(np.vstack(inp_vector).astype(floatX))
questions.append(np.vstack(q_vector).astype(floatX))
answers.append(process_word(word = x["A"],
word2vec = word2vec,
vocab = vocab,
ivocab = ivocab,
word_vector_size = embed_size,
to_return = "index"))
# NOTE: here we assume the answer is one word!
if not split_sentences:
if input_mask_mode == 'word':
input_masks.append(np.array([index for index, w in enumerate(inp)], dtype=np.int32))
elif input_mask_mode == 'sentence':
input_masks.append(np.array([index for index, w in enumerate(inp) if w == '.'], dtype=np.int32))
else:
raise Exception("invalid input_mask_mode")
return inputs, questions, answers, input_masks
def get_lens(inputs, split_sentences=False):
lens = np.zeros((len(inputs)), dtype=int)
for i, t in enumerate(inputs):
lens[i] = t.shape[0]
return lens
def get_sentence_lens(inputs):
lens = np.zeros((len(inputs)), dtype=int)
sen_lens = []
max_sen_lens = []
for i, t in enumerate(inputs):
sentence_lens = np.zeros((len(t)), dtype=int)
for j, s in enumerate(t):
sentence_lens[j] = len(s)
lens[i] = len(t)
sen_lens.append(sentence_lens)
max_sen_lens.append(np.max(sentence_lens))
return lens, sen_lens, max(max_sen_lens)
def pad_inputs(inputs, lens, max_len, mode="", sen_lens=None, max_sen_len=None):
if mode == "mask":
padded = [np.pad(inp, (0, max_len - lens[i]), 'constant', constant_values=0) for i, inp in enumerate(inputs)]
return np.vstack(padded)
elif mode == "split_sentences":
padded = np.zeros((len(inputs), max_len, max_sen_len))
for i, inp in enumerate(inputs):
padded_sentences = [np.pad(s, (0, max_sen_len - sen_lens[i][j]), 'constant', constant_values=0) for j, s in enumerate(inp)]
# trim array according to max allowed inputs
if len(padded_sentences) > max_len:
padded_sentences = padded_sentences[(len(padded_sentences)-max_len):]
lens[i] = max_len
padded_sentences = np.vstack(padded_sentences)
padded_sentences = np.pad(padded_sentences, ((0, max_len - lens[i]),(0,0)), 'constant', constant_values=0)
padded[i] = padded_sentences
return padded
padded = [np.pad(np.squeeze(inp, axis=1), (0, max_len - lens[i]), 'constant', constant_values=0) for i, inp in enumerate(inputs)]
return np.vstack(padded)
def create_embedding(word2vec, ivocab, embed_size):
embedding = np.zeros((len(ivocab), embed_size))
for i in range(len(ivocab)):
word = ivocab[i]
embedding[i] = word2vec[word]
return embedding
def load_babi(config, split_sentences=False):
vocab = {}
ivocab = {}
babi_train_raw, babi_test_raw = get_babi_raw(config.babi_id, config.babi_test_id)
if config.word2vec_init:
assert config.embed_size == 100
word2vec = load_glove(config.embed_size)
else:
word2vec = {}
# set word at index zero to be end of sentence token so padding with zeros is consistent
process_word(word = "<eos>",
word2vec = word2vec,
vocab = vocab,
ivocab = ivocab,
word_vector_size = config.embed_size,
to_return = "index")
print('==> get train inputs')
train_data = process_input(babi_train_raw, config.floatX, word2vec, vocab, ivocab, config.embed_size, split_sentences)
print('==> get test inputs')
test_data = process_input(babi_test_raw, config.floatX, word2vec, vocab, ivocab, config.embed_size, split_sentences)
if config.word2vec_init:
assert config.embed_size == 100
word_embedding = create_embedding(word2vec, ivocab, config.embed_size)
else:
word_embedding = np.random.uniform(-config.embedding_init, config.embedding_init, (len(ivocab), config.embed_size))
inputs, questions, answers, input_masks = train_data if config.train_mode else test_data
if split_sentences:
input_lens, sen_lens, max_sen_len = get_sentence_lens(inputs)
max_mask_len = max_sen_len
else:
input_lens = get_lens(inputs)
mask_lens = get_lens(input_masks)
max_mask_len = np.max(mask_lens)
q_lens = get_lens(questions)
max_q_len = np.max(q_lens)
max_input_len = min(np.max(input_lens), config.max_allowed_inputs)
#pad out arrays to max
if split_sentences:
inputs = pad_inputs(inputs, input_lens, max_input_len, "split_sentences", sen_lens, max_sen_len)
input_masks = np.zeros(len(inputs))
else:
inputs = pad_inputs(inputs, input_lens, max_input_len)
input_masks = pad_inputs(input_masks, mask_lens, max_mask_len, "mask")
questions = pad_inputs(questions, q_lens, max_q_len)
answers = np.stack(answers)
if config.train_mode:
train = questions[:config.num_train], inputs[:config.num_train], q_lens[:config.num_train], input_lens[:config.num_train], input_masks[:config.num_train], answers[:config.num_train]
valid = questions[config.num_train:], inputs[config.num_train:], q_lens[config.num_train:], input_lens[config.num_train:], input_masks[config.num_train:], answers[config.num_train:]
return train, valid, word_embedding, max_q_len, max_input_len, max_mask_len, len(vocab)
else:
test = questions, inputs, q_lens, input_lens, input_masks, answers
return test, word_embedding, max_q_len, max_input_len, max_mask_len, len(vocab)