-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathdata_loader.py
270 lines (229 loc) · 12.8 KB
/
data_loader.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
import json
import numpy as np
from scipy.sparse import coo_matrix, csr_matrix
from util import load_dict
from tqdm import tqdm
class DataLoader():
def __init__(self, data_file, documents, document_entity_indices, document_texts, word2id, relation2id, entity2id, max_query_word, max_document_word, use_kb, use_doc, use_inverse_relation):
self.use_kb = use_kb
self.use_doc = use_doc
self.use_inverse_relation = use_inverse_relation
self.max_local_entity = 0
self.max_relevant_doc = 0
self.max_facts = 0
self.max_query_word = max_query_word
self.max_document_word = max_document_word
if self.use_kb:
if self.use_inverse_relation:
self.num_kb_relation = 2 * len(relation2id)
else:
self.num_kb_relation = len(relation2id)
else:
self.num_kb_relation = 0
print('loading data from', data_file)
self.data = []
with open(data_file) as f_in:
for line in tqdm(f_in):
line = json.loads(line)
self.data.append(line)
self.max_relevant_doc = max(self.max_relevant_doc, len(line['passages']))
self.max_facts = max(self.max_facts, 2 * len(line['subgraph']['tuples']))
print('max_relevant_doc: ', self.max_relevant_doc)
print('max_facts: ', self.max_facts)
self.num_data = len(self.data)
self.batches = np.arange(self.num_data)
print('building word index ...')
self.word2id = word2id
self.relation2id = relation2id
self.entity2id = entity2id
self.documents = documents
self.id2entity = {i:entity for entity, i in entity2id.items()}
print('converting global to local entity index ...')
self.global2local_entity_maps = self._build_global2local_entity_maps()
print('max_local_entity', self.max_local_entity)
# print('indexing documents ...')
self.document_entity_indices = document_entity_indices
self.document_texts = document_texts
print('preparing data ...')
self.local_entities = np.full((self.num_data, self.max_local_entity), len(self.entity2id), dtype=int)
self.kb_adj_mats = np.empty(self.num_data, dtype=object)
self.kb_fact_rels = np.full((self.num_data, self.max_facts), self.num_kb_relation, dtype=int)
self.q2e_adj_mats = np.zeros((self.num_data, self.max_local_entity, 1), dtype=float)
self.query_texts = np.full((self.num_data, self.max_query_word), len(self.word2id), dtype=int)
self.rel_document_ids = np.full((self.num_data, self.max_relevant_doc), -1, dtype=int) # the last document is empty
self.entity_poses = np.empty(self.num_data, dtype=object)
self.answer_dists = np.zeros((self.num_data, self.max_local_entity), dtype=float)
self._prepare_data()
def _prepare_data(self):
"""
global2local_entity_maps: a map from global entity id to local entity id
adj_mats: a local adjacency matrix for each relation. relation 0 is reserved for self-connection.
"""
next_id = 0
count_query_length = [0] * 50
total_num_answerable_question = 0
for sample in tqdm(self.data):
# get a list of local entities
g2l = self.global2local_entity_maps[next_id]
for global_entity, local_entity in g2l.items():
if local_entity != 0: # skip question node
self.local_entities[next_id, local_entity] = global_entity
entity2fact_e, entity2fact_f = [], []
fact2entity_f, fact2entity_e = [], []
entity_pos_local_entity_id = []
entity_pos_word_id = []
entity_pos_word_weights = []
# relations in local KB
if self.use_kb:
for i, tpl in enumerate(sample['subgraph']['tuples']):
sbj, rel, obj = tpl
if not self.use_inverse_relation:
entity2fact_e += [g2l[self.entity2id[sbj['text']]]]
entity2fact_f += [i]
fact2entity_f += [i]
fact2entity_e += [g2l[self.entity2id[obj['text']]]]
self.kb_fact_rels[next_id, i] = self.relation2id[rel['text']]
else:
entity2fact_e += [g2l[self.entity2id[sbj['text']]], g2l[self.entity2id[obj['text']]]]
entity2fact_f += [2 * i, 2 * i + 1]
fact2entity_f += [2 * i, 2 * i + 1]
fact2entity_e += [g2l[self.entity2id[obj['text']]], g2l[self.entity2id[sbj['text']]]]
self.kb_fact_rels[next_id, 2 * i] = self.relation2id[rel['text']]
self.kb_fact_rels[next_id, 2 * i + 1] = self.relation2id[rel['text']] + len(self.relation2id)
# build connection between question and entities in it
for j, entity in enumerate(sample['entities']):
self.q2e_adj_mats[next_id, g2l[self.entity2id[unicode(entity['text'])]], 0] = 1.0
# connect documents to entities occurred in it
if self.use_doc:
for j, passage in enumerate(sample['passages']):
document_id = passage['document_id']
if document_id not in self.document_entity_indices:
continue
(global_entity_ids, word_ids, word_weights) = self.document_entity_indices[document_id]
entity_pos_local_entity_id += [g2l[global_entity_id] for global_entity_id in global_entity_ids]
entity_pos_word_id += [word_id + j * self.max_document_word for word_id in word_ids]
entity_pos_word_weights += word_weights
# tokenize question
count_query_length[len(sample['question'].split())] += 1
for j, word in enumerate(sample['question'].split()):
if j < self.max_query_word:
if word in self.word2id:
self.query_texts[next_id, j] = self.word2id[word]
else:
self.query_texts[next_id, j] = self.word2id['__unk__']
# tokenize document
for pid, passage in enumerate(sample['passages']):
self.rel_document_ids[next_id, pid] = passage['document_id']
# construct distribution for answers
for answer in sample['answers']:
keyword = 'text' if type(answer['kb_id']) == int else 'kb_id'
if self.entity2id[answer[keyword]] in g2l:
self.answer_dists[next_id, g2l[self.entity2id[answer[keyword]]]] = 1.0
self.kb_adj_mats[next_id] = (np.array(entity2fact_f, dtype=int), np.array(entity2fact_e, dtype=int), np.array([1.0] * len(entity2fact_f))), (np.array(fact2entity_e, dtype=int), np.array(fact2entity_f, dtype=int), np.array([1.0] * len(fact2entity_e)))
self.entity_poses[next_id] = (entity_pos_local_entity_id, entity_pos_word_id, entity_pos_word_weights)
next_id += 1
def _build_kb_adj_mat(self, sample_ids, fact_dropout):
"""Create sparse matrix representation for batched data"""
mats0_batch = np.array([], dtype=int)
mats0_0 = np.array([], dtype=int)
mats0_1 = np.array([], dtype=int)
vals0 = np.array([], dtype=float)
mats1_batch = np.array([], dtype=int)
mats1_0 = np.array([], dtype=int)
mats1_1 = np.array([], dtype=int)
vals1 = np.array([], dtype=float)
for i, sample_id in enumerate(sample_ids):
(mat0_0, mat0_1, val0), (mat1_0, mat1_1, val1) = self.kb_adj_mats[sample_id]
assert len(val0) == len(val1)
num_fact = len(val0)
num_keep_fact = int(np.floor(num_fact * (1 - fact_dropout)))
mask_index = np.random.permutation(num_fact)[ : num_keep_fact]
# mat0
mats0_batch = np.append(mats0_batch, np.full(len(mask_index), i, dtype=int))
mats0_0 = np.append(mats0_0, mat0_0[mask_index])
mats0_1 = np.append(mats0_1, mat0_1[mask_index])
vals0 = np.append(vals0, val0[mask_index])
# mat1
mats1_batch = np.append(mats1_batch, np.full(len(mask_index), i, dtype=int))
mats1_0 = np.append(mats1_0, mat1_0[mask_index])
mats1_1 = np.append(mats1_1, mat1_1[mask_index])
vals1 = np.append(vals1, val1[mask_index])
return (mats0_batch, mats0_0, mats0_1, vals0), (mats1_batch, mats1_0, mats1_1, vals1)
def reset_batches(self, is_sequential=True):
if is_sequential:
self.batches = np.arange(self.num_data)
else:
self.batches = np.random.permutation(self.num_data)
def get_batch(self, iteration, batch_size, fact_dropout):
"""
*** return values ***
:local_entity: global_id of each entity (batch_size, max_local_entity)
:adj_mat: adjacency matrices (batch_size, num_relation, max_local_entity, max_local_entity)
:query_text: a list of words in the query (batch_size, max_query_word)
:rel_document_ids: (batch_size, max_relevant_doc)
:answer_dist: an distribution over local_entity (batch_size, max_local_entity)
"""
sample_ids = self.batches[batch_size * iteration: batch_size * (iteration + 1)]
return self.local_entities[sample_ids], \
self.q2e_adj_mats[sample_ids], \
(self._build_kb_adj_mat(sample_ids, fact_dropout=fact_dropout)), \
self.kb_fact_rels[sample_ids], \
self.query_texts[sample_ids], \
self._build_document_text(sample_ids), \
(self._build_entity_pos(sample_ids)), \
self.answer_dists[sample_ids]
def _build_document_text(self, sample_ids):
"""Index tokenized documents for each sample"""
document_text = np.full((len(sample_ids), self.max_relevant_doc, self.max_document_word), len(self.word2id), dtype=int)
for i, sample_id in enumerate(sample_ids):
for j, rel_doc_id in enumerate(self.rel_document_ids[sample_id]):
if rel_doc_id not in self.document_texts:
continue
document_text[i, j] = self.document_texts[rel_doc_id]
return document_text
def _build_entity_pos(self, sample_ids):
"""Index the position of each entity in documents"""
entity_pos_batch = np.array([], dtype=int)
entity_pos_entity_id = np.array([], dtype=int)
entity_pos_word_id = np.array([], dtype=int)
vals = np.array([], dtype=float)
for i, sample_id in enumerate(sample_ids):
(entity_id, word_id, val) = self.entity_poses[sample_id]
num_nonzero = len(val)
entity_pos_batch = np.append(entity_pos_batch, np.full(num_nonzero, i, dtype=int))
entity_pos_entity_id = np.append(entity_pos_entity_id, entity_id)
entity_pos_word_id = np.append(entity_pos_word_id, word_id)
vals = np.append(vals, val)
return (entity_pos_batch.astype(int), entity_pos_entity_id.astype(int), entity_pos_word_id.astype(int), vals)
def _build_global2local_entity_maps(self):
"""Create a map from global entity id to local entity of each sample"""
global2local_entity_maps = [None] * self.num_data
total_local_entity = 0.0
next_id = 0
for sample in tqdm(self.data):
g2l = dict()
self._add_entity_to_map(self.entity2id, sample['entities'], g2l)
# construct a map from global entity id to local entity id
if self.use_kb:
self._add_entity_to_map(self.entity2id, sample['subgraph']['entities'], g2l)
if self.use_doc:
for relevant_doc in sample['passages']:
if relevant_doc['document_id'] not in self.documents:
continue
document = self.documents[int(relevant_doc['document_id'])]
self._add_entity_to_map(self.entity2id, document['document']['entities'], g2l)
if 'title' in document:
self._add_entity_to_map(self.entity2id, document['title']['entities'], g2l)
global2local_entity_maps[next_id] = g2l
total_local_entity += len(g2l)
self.max_local_entity = max(self.max_local_entity, len(g2l))
next_id += 1
print('avg local entity: ', total_local_entity / next_id)
return global2local_entity_maps
@staticmethod
def _add_entity_to_map(entity2id, entities, g2l):
for entity in entities:
entity_text = entity['text']
entity_global_id = entity2id[entity_text]
if entity_global_id not in g2l:
g2l[entity2id[entity_text]] = len(g2l)