-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataset_load.py
625 lines (535 loc) · 25.3 KB
/
dataset_load.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import json
import numpy as np
import re
from tqdm import tqdm
import torch
from collections import Counter
import random
import warnings
import pickle
warnings.filterwarnings("ignore")
from modules.question_encoding.tokenizers import LSTMTokenizer#, BERTTokenizer
from transformers import AutoTokenizer
import time
import os
try:
os.environ['TRANSFORMERS_CACHE'] = '/export/scratch/costas/home/mavro016/.cache'
except:
pass
class BasicDataLoader(object):
"""
Basic Dataloader contains all the functions to read questions and KGs from json files and
create mappings between global entity ids and local ids that are used during GNN updates.
"""
def __init__(self, config, word2id, relation2id, entity2id, tokenize, data_type="train"):
self.tokenize = tokenize
self._parse_args(config, word2id, relation2id, entity2id)
self._load_file(config, data_type)
self._load_data()
def _load_file(self, config, data_type="train"):
"""
Loads lines (questions + KG subgraphs) from json files.
"""
data_file = config['data_folder'] + data_type + ".json"
self.data_file = data_file
print('loading data from', data_file)
self.data_type = data_type
self.data = []
skip_index = set()
index = 0
with open(data_file) as f_in:
for line in tqdm(f_in):
if index == config['max_train'] and data_type == "train": break #break if we reach max_question_size
line = json.loads(line)
if len(line['entities']) == 0:
skip_index.add(index)
continue
self.data.append(line)
self.max_facts = max(self.max_facts, 2 * len(line['subgraph']['tuples']))
index += 1
print("skip", skip_index)
print('max_facts: ', self.max_facts)
self.num_data = len(self.data)
self.batches = np.arange(self.num_data)
def _load_data(self):
"""
Creates mappings between global entity ids and local entity ids that are used during GNN updates.
"""
print('converting global to local entity index ...')
self.global2local_entity_maps = self._build_global2local_entity_maps()
if self.use_self_loop:
self.max_facts = self.max_facts + self.max_local_entity
self.question_id = []
self.candidate_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.q_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.query_entities = np.zeros((self.num_data, self.max_local_entity), dtype=float)
self.seed_list = np.empty(self.num_data, dtype=object)
self.seed_distribution = np.zeros((self.num_data, self.max_local_entity), dtype=float)
# self.query_texts = np.full((self.num_data, self.max_query_word), len(self.word2id), dtype=int)
self.answer_dists = np.zeros((self.num_data, self.max_local_entity), dtype=float)
self.answer_lists = np.empty(self.num_data, dtype=object)
self._prepare_data()
def _parse_args(self, config, word2id, relation2id, entity2id):
"""
Builds necessary dictionaries and stores arguments.
"""
self.data_eff = config['data_eff']
self.data_name = config['name']
if 'use_inverse_relation' in config:
self.use_inverse_relation = config['use_inverse_relation']
else:
self.use_inverse_relation = False
if 'use_self_loop' in config:
self.use_self_loop = config['use_self_loop']
else:
self.use_self_loop = False
self.rel_word_emb = config['relation_word_emb']
#self.num_step = config['num_step']
self.max_local_entity = 0
self.max_relevant_doc = 0
self.max_facts = 0
print('building word index ...')
self.word2id = word2id
self.id2word = {i: word for word, i in word2id.items()}
self.relation2id = relation2id
self.entity2id = entity2id
self.id2entity = {i: entity for entity, i in entity2id.items()}
self.q_type = config['q_type']
if self.use_inverse_relation:
self.num_kb_relation = 2 * len(relation2id)
else:
self.num_kb_relation = len(relation2id)
if self.use_self_loop:
self.num_kb_relation = self.num_kb_relation + 1
print("Entity: {}, Relation in KB: {}, Relation in use: {} ".format(len(entity2id),
len(self.relation2id),
self.num_kb_relation))
def get_quest(self, training=False):
q_list = []
sample_ids = self.sample_ids
for sample_id in sample_ids:
tp_str = self.decode_text(self.query_texts[sample_id, :])
# id2word = self.id2word
# for i in range(self.max_query_word):
# if self.query_texts[sample_id, i] in id2word:
# tp_str += id2word[self.query_texts[sample_id, i]] + " "
q_list.append(tp_str)
return q_list
def decode_text(self, np_array_x):
if self.tokenize == 'lstm':
id2word = self.id2word
tp_str = ""
for i in range(self.max_query_word):
if np_array_x[i] in id2word:
tp_str += id2word[np_array_x[i]] + " "
else:
tp_str = ""
words = self.tokenizer.convert_ids_to_tokens(np_array_x)
for w in words:
if w not in ['[CLS]', '[SEP]', '[PAD]']:
tp_str += w + " "
return tp_str
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.
"""
max_count = 0
for line in self.data:
word_list = line["question"].split(' ')
max_count = max(max_count, len(word_list))
if self.rel_word_emb:
self.build_rel_words(self.tokenize)
else:
self.rel_texts = None
self.ent_texts = None
self.max_query_word = max_count
#self.query_texts = np.full((self.num_data, self.max_query_word), len(self.word2id), dtype=int)
#self.query_texts2 = np.full((self.num_data, self.max_query_word), len(self.word2id), dtype=int)
#build tokenizers
if self.tokenize == 'lstm':
self.num_word = len(self.word2id)
self.tokenizer = LSTMTokenizer(self.word2id, self.max_query_word)
self.query_texts = np.full((self.num_data, self.max_query_word), self.num_word, dtype=int)
else:
if self.tokenize == 'bert':
tokenizer_name = 'bert-base-uncased'
elif self.tokenize == 'roberta':
tokenizer_name = 'roberta-base'
elif self.tokenize == 'sbert':
tokenizer_name = 'sentence-transformers/all-MiniLM-L6-v2'
elif self.tokenize == 'sbert2':
tokenizer_name = 'sentence-transformers/all-mpnet-base-v2'
elif self.tokenize == 't5':
tokenizer_name = 't5-small'
self.max_query_word = max_count + 2 #for cls token and sep
#self.tokenizer = AutoTokenizer(self.max_query_word)
self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
self.num_word = self.tokenizer.convert_tokens_to_ids(self.tokenizer.pad_token) #self.tokenizer.q_tokenizer.encode("[UNK]")[0]
self.query_texts = np.full((self.num_data, self.max_query_word), self.num_word, dtype=int)
next_id = 0
num_query_entity = {}
for sample in tqdm(self.data):
self.question_id.append(sample["id"])
# get a list of local entities
g2l = self.global2local_entity_maps[next_id]
#print(g2l)
if len(g2l) == 0:
#print(next_id)
continue
# build connection between question and entities in it
tp_set = set()
seed_list = []
for j, entity in enumerate(sample['entities']):
# if entity['text'] not in self.entity2id:
# continue
try:
global_entity = self.entity2id[entity['text']]
except:
global_entity = entity #self.entity2id[entity['text']]
if global_entity not in g2l:
continue
local_ent = g2l[global_entity]
self.query_entities[next_id, local_ent] = 1.0
seed_list.append(local_ent)
tp_set.add(local_ent)
self.seed_list[next_id] = seed_list
num_query_entity[next_id] = len(tp_set)
for global_entity, local_entity in g2l.items():
if self.data_name != 'cwq':
if local_entity not in tp_set: # skip entities in question
#print(global_entity)
#print(local_entity)
self.candidate_entities[next_id, local_entity] = global_entity
elif self.data_name == 'cwq':
self.candidate_entities[next_id, local_entity] = global_entity
# if local_entity != 0: # skip question node
# self.candidate_entities[next_id, local_entity] = global_entity
# relations in local KB
head_list = []
rel_list = []
tail_list = []
for i, tpl in enumerate(sample['subgraph']['tuples']):
sbj, rel, obj = tpl
try:
head = g2l[self.entity2id[sbj['text']]]
rel = self.relation2id[rel['text']]
tail = g2l[self.entity2id[obj['text']]]
except:
head = g2l[sbj]
rel = int(rel)
tail = g2l[obj]
head_list.append(head)
rel_list.append(rel)
tail_list.append(tail)
self.kb_fact_rels[next_id, i] = rel
if self.use_inverse_relation:
head_list.append(tail)
rel_list.append(rel + len(self.relation2id))
tail_list.append(head)
self.kb_fact_rels[next_id, i] = rel + len(self.relation2id)
if len(tp_set) > 0:
for local_ent in tp_set:
self.seed_distribution[next_id, local_ent] = 1.0 / len(tp_set)
else:
for index in range(len(g2l)):
self.seed_distribution[next_id, index] = 1.0 / len(g2l)
try:
assert np.sum(self.seed_distribution[next_id]) > 0.0
except:
print(next_id, len(tp_set))
exit(-1)
#tokenize question
if self.tokenize == 'lstm':
self.query_texts[next_id] = self.tokenizer.tokenize(sample['question'])
else:
tokens = self.tokenizer.encode_plus(text=sample['question'], max_length=self.max_query_word, \
pad_to_max_length=True, return_attention_mask = False, truncation=True)
self.query_texts[next_id] = np.array(tokens['input_ids'])
# construct distribution for answers
answer_list = []
for answer in sample['answers']:
keyword = 'text' if type(answer['kb_id']) == int else 'kb_id'
answer_ent = self.entity2id[answer[keyword]]
answer_list.append(answer_ent)
if answer_ent in g2l:
self.answer_dists[next_id, g2l[answer_ent]] = 1.0
self.answer_lists[next_id] = answer_list
if not self.data_eff:
self.kb_adj_mats[next_id] = (np.array(head_list, dtype=int),
np.array(rel_list, dtype=int),
np.array(tail_list, dtype=int))
next_id += 1
num_no_query_ent = 0
num_one_query_ent = 0
num_multiple_ent = 0
for i in range(next_id):
ct = num_query_entity[i]
if ct == 1:
num_one_query_ent += 1
elif ct == 0:
num_no_query_ent += 1
else:
num_multiple_ent += 1
print("{} cases in total, {} cases without query entity, {} cases with single query entity,"
" {} cases with multiple query entities".format(next_id, num_no_query_ent,
num_one_query_ent, num_multiple_ent))
def build_rel_words(self, tokenize):
"""
Tokenizes relation surface forms.
"""
max_rel_words = 0
rel_words = []
if 'metaqa' in self.data_file:
for rel in self.relation2id:
words = rel.split('_')
max_rel_words = max(len(words), max_rel_words)
rel_words.append(words)
#print(rel_words)
else:
for rel in self.relation2id:
rel = rel.strip()
fields = rel.split('.')
try:
words = fields[-2].split('_') + fields[-1].split('_')
max_rel_words = max(len(words), max_rel_words)
rel_words.append(words)
#print(rel, words)
except:
words = ['UNK']
rel_words.append(words)
pass
#words = fields[-2].split('_') + fields[-1].split('_')
self.max_rel_words = max_rel_words
if tokenize == 'lstm':
self.rel_texts = np.full((self.num_kb_relation + 1, self.max_rel_words), len(self.word2id), dtype=int)
self.rel_texts_inv = np.full((self.num_kb_relation + 1, self.max_rel_words), len(self.word2id), dtype=int)
for rel_id,tokens in enumerate(rel_words):
for j, word in enumerate(tokens):
if j < self.max_rel_words:
if word in self.word2id:
self.rel_texts[rel_id, j] = self.word2id[word]
self.rel_texts_inv[rel_id, j] = self.word2id[word]
else:
self.rel_texts[rel_id, j] = len(self.word2id)
self.rel_texts_inv[rel_id, j] = len(self.word2id)
else:
if tokenize == 'bert':
tokenizer_name = 'bert-base-uncased'
elif tokenize == 'roberta':
tokenizer_name = 'roberta-base'
elif tokenize == 'sbert':
tokenizer_name = 'sentence-transformers/all-MiniLM-L6-v2'
elif tokenize == 'sbert2':
tokenizer_name = 'sentence-transformers/all-mpnet-base-v2'
elif tokenize == 't5':
tokenizer_name = 't5-small'
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
pad_val = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
self.rel_texts = np.full((self.num_kb_relation + 1, self.max_rel_words), pad_val, dtype=int)
self.rel_texts_inv = np.full((self.num_kb_relation + 1, self.max_rel_words), pad_val, dtype=int)
for rel_id,words in enumerate(rel_words):
tokens = tokenizer.encode_plus(text=' '.join(words), max_length=self.max_rel_words, \
pad_to_max_length=True, return_attention_mask = False, truncation=True)
tokens_inv = tokenizer.encode_plus(text=' '.join(words[::-1]), max_length=self.max_rel_words, \
pad_to_max_length=True, return_attention_mask = False, truncation=True)
self.rel_texts[rel_id] = np.array(tokens['input_ids'])
self.rel_texts_inv[rel_id] = np.array(tokens_inv['input_ids'])
#print(rel_words)
#print(len(rel_words), len(self.relation2id))
assert len(rel_words) == len(self.relation2id)
#print(self.rel_texts, self.max_rel_words)
def create_kb_adj_mats(self, sample_id):
"""
Re-build local adj mats if we have data_eff == True (they are not pre-stored).
"""
sample = self.data[sample_id]
g2l = self.global2local_entity_maps[sample_id]
# build connection between question and entities in it
head_list = []
rel_list = []
tail_list = []
for i, tpl in enumerate(sample['subgraph']['tuples']):
sbj, rel, obj = tpl
try:
head = g2l[self.entity2id[sbj['text']]]
rel = self.relation2id[rel['text']]
tail = g2l[self.entity2id[obj['text']]]
except:
head = g2l[sbj]
rel = int(rel)
tail = g2l[obj]
head_list.append(head)
rel_list.append(rel)
tail_list.append(tail)
if self.use_inverse_relation:
head_list.append(tail)
rel_list.append(rel + len(self.relation2id))
tail_list.append(head)
return np.array(head_list, dtype=int), np.array(rel_list, dtype=int), np.array(tail_list, dtype=int)
def _build_fact_mat(self, sample_ids, fact_dropout):
"""
Creates local adj mats that contain entities, relations, and structure.
"""
batch_heads = np.array([], dtype=int)
batch_rels = np.array([], dtype=int)
batch_tails = np.array([], dtype=int)
batch_ids = np.array([], dtype=int)
#print(sample_ids)
for i, sample_id in enumerate(sample_ids):
index_bias = i * self.max_local_entity
if self.data_eff:
head_list, rel_list, tail_list = self.create_kb_adj_mats(sample_id) #kb_adj_mats[sample_id]
else:
(head_list, rel_list, tail_list) = self.kb_adj_mats[sample_id]
num_fact = len(head_list)
num_keep_fact = int(np.floor(num_fact * (1 - fact_dropout)))
mask_index = np.random.permutation(num_fact)[: num_keep_fact]
real_head_list = head_list[mask_index] + index_bias
real_tail_list = tail_list[mask_index] + index_bias
real_rel_list = rel_list[mask_index]
batch_heads = np.append(batch_heads, real_head_list)
batch_rels = np.append(batch_rels, real_rel_list)
batch_tails = np.append(batch_tails, real_tail_list)
batch_ids = np.append(batch_ids, np.full(len(mask_index), i, dtype=int))
if self.use_self_loop:
num_ent_now = len(self.global2local_entity_maps[sample_id])
ent_array = np.array(range(num_ent_now), dtype=int) + index_bias
rel_array = np.array([self.num_kb_relation - 1] * num_ent_now, dtype=int)
batch_heads = np.append(batch_heads, ent_array)
batch_tails = np.append(batch_tails, ent_array)
batch_rels = np.append(batch_rels, rel_array)
batch_ids = np.append(batch_ids, np.full(num_ent_now, i, dtype=int))
fact_ids = np.array(range(len(batch_heads)), dtype=int)
head_count = Counter(batch_heads)
# tail_count = Counter(batch_tails)
weight_list = [1.0 / head_count[head] for head in batch_heads]
# entity2fact_index = torch.LongTensor([batch_heads, fact_ids])
# entity2fact_val = torch.FloatTensor(weight_list)
# entity2fact_mat = torch.sparse.FloatTensor(entity2fact_index, entity2fact_val, torch.Size(
# [len(sample_ids) * self.max_local_entity, len(batch_heads)]))
return batch_heads, batch_rels, batch_tails, batch_ids, fact_ids, weight_list
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 _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
self._add_entity_to_map(self.entity2id, sample['subgraph']['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)
print('max local entity: ', self.max_local_entity)
return global2local_entity_maps
@staticmethod
def _add_entity_to_map(entity2id, entities, g2l):
#print(entities)
#print(entity2id)
for entity_global_id in entities:
try:
ent = entity2id[entity_global_id['text']]
if ent not in g2l:
g2l[ent] = len(g2l)
except:
if entity_global_id not in g2l:
g2l[entity_global_id] = len(g2l)
def deal_q_type(self, q_type=None):
sample_ids = self.sample_ids
if q_type is None:
q_type = self.q_type
if q_type == "seq":
q_input = self.query_texts[sample_ids]
else:
raise NotImplementedError
return q_input
class SingleDataLoader(BasicDataLoader):
"""
Single Dataloader creates training/eval batches during KGQA.
"""
def __init__(self, config, word2id, relation2id, entity2id, tokenize, data_type="train"):
super(SingleDataLoader, self).__init__(config, word2id, relation2id, entity2id, tokenize, data_type)
def get_batch(self, iteration, batch_size, fact_dropout, q_type=None, test=False):
start = batch_size * iteration
end = min(batch_size * (iteration + 1), self.num_data)
sample_ids = self.batches[start: end]
self.sample_ids = sample_ids
# true_batch_id, sample_ids, seed_dist = self.deal_multi_seed(ori_sample_ids)
# self.sample_ids = sample_ids
# self.true_sample_ids = ori_sample_ids
# self.batch_ids = true_batch_id
true_batch_id = None
seed_dist = self.seed_distribution[sample_ids]
q_input = self.deal_q_type(q_type)
kb_adj_mats = self._build_fact_mat(sample_ids, fact_dropout=fact_dropout)
if test:
return self.candidate_entities[sample_ids], \
self.query_entities[sample_ids], \
kb_adj_mats, \
q_input, \
seed_dist, \
true_batch_id, \
self.answer_dists[sample_ids], \
self.answer_lists[sample_ids],\
return self.candidate_entities[sample_ids], \
self.query_entities[sample_ids], \
kb_adj_mats, \
q_input, \
seed_dist, \
true_batch_id, \
self.answer_dists[sample_ids]
def load_dict(filename):
word2id = dict()
with open(filename, encoding='utf-8') as f_in:
for line in f_in:
word = line.strip()
word2id[word] = len(word2id)
return word2id
def load_data(config, tokenize):
"""
Creates train/val/test dataloaders (seperately).
"""
entity2id = load_dict(config['data_folder'] + config['entity2id'])
word2id = load_dict(config['data_folder'] + config['word2id'])
relation2id = load_dict(config['data_folder'] + config['relation2id'])
if config["is_eval"]:
train_data = None
valid_data = SingleDataLoader(config, word2id, relation2id, entity2id, tokenize, data_type="dev")
test_data = SingleDataLoader(config, word2id, relation2id, entity2id, tokenize, data_type="test")
num_word = test_data.num_word
else:
train_data = SingleDataLoader(config, word2id, relation2id, entity2id, tokenize, data_type="train")
valid_data = SingleDataLoader(config, word2id, relation2id, entity2id, tokenize, data_type="dev")
test_data = SingleDataLoader(config, word2id, relation2id, entity2id, tokenize, data_type="test")
num_word = train_data.num_word
relation_texts = test_data.rel_texts
relation_texts_inv = test_data.rel_texts_inv
entities_texts = None
dataset = {
"train": train_data,
"valid": valid_data,
"test": test_data, #test_data,
"entity2id": entity2id,
"relation2id": relation2id,
"word2id": word2id,
"num_word": num_word,
"rel_texts": relation_texts,
"rel_texts_inv": relation_texts_inv,
"ent_texts": entities_texts
}
return dataset
if __name__ == "__main__":
st = time.time()
#args = get_config()
load_data(args)