-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
70 lines (53 loc) · 2.2 KB
/
dataset.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
import torch
from torch.utils import data
from copy import deepcopy
from graph import TemporalGraph
class Example(object):
"""Defines each triple in TKG"""
def __init__(self, triple, entity_vocab, relation_vocab, time_vocab, example_idx):
self.head_idx = entity_vocab(triple[0])
self.relation_idx = relation_vocab(triple[1])
self.tail_idx = entity_vocab(triple[2])
self.time_idx = time_vocab(triple[3])
self.example_idx = example_idx
self.graph = None
class TKGDataset(data.Dataset):
"""Temporal KG Dataset Class"""
def __init__(self, example_list, kg, device):
self.example_list = example_list
self.kg = kg
self.device = device
def __iter__(self):
return iter(self.example_list)
def __getitem__(self, idx):
example = self.example_list[idx]
return example
def __len__(self):
return len(self.example_list)
def collate(self, batched_examples):
batch_heads, batch_relations, batch_tails, batch_times, batch_graph, batch_ex_indices = [], [], [], [], [], []
for example in batched_examples:
batch_heads.append(example.head_idx)
batch_relations.append(example.relation_idx)
batch_tails.append(example.tail_idx)
batch_times.append(example.time_idx)
batch_ex_indices.append(example.example_idx)
return {
"head": torch.tensor(batch_heads),
"relation": torch.tensor(batch_relations),
"tail": torch.tensor(batch_tails),
"time": torch.tensor(batch_times),
"example_idx": torch.tensor(batch_ex_indices),
"graph": deepcopy(self.kg.graph)
}
def get_datasets(filenames, device):
KG = TemporalGraph(filenames[0], device)
datasets = []
for fname in filenames:
triples = open(fname, 'r').read().lower().splitlines()
triples = list(map(lambda x: x.split("\t"), triples))
example_list = []
for i, triple in enumerate(triples):
example_list.append(Example(triple, KG.entity_vocab, KG.relation_vocab, KG.time_vocab, i))
datasets.append(TKGDataset(example_list, KG, device))
return datasets