-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
179 lines (151 loc) · 7.94 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
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
import os
import csv
from tqdm import tqdm
import torch
from torch.utils import data as torch_data
from torchdrug import data, datasets, utils
from torchdrug.core import Registry as R
class InductiveKnowledgeGraphDataset(data.KnowledgeGraphDataset):
def load_inductive_tsvs(self, transductive_files, inductive_files, verbose=0):
assert len(transductive_files) == len(inductive_files) == 3
inv_transductive_vocab = {}
inv_inductive_vocab = {}
inv_relation_vocab = {}
triplets = []
num_samples = []
for txt_file in transductive_files:
with open(txt_file, "r") as fin:
reader = csv.reader(fin, delimiter="\t")
if verbose:
reader = tqdm(reader, "Loading %s" % txt_file, utils.get_line_count(txt_file))
num_sample = 0
for tokens in reader:
h_token, r_token, t_token = tokens
if h_token not in inv_transductive_vocab:
inv_transductive_vocab[h_token] = len(inv_transductive_vocab)
h = inv_transductive_vocab[h_token]
if r_token not in inv_relation_vocab:
inv_relation_vocab[r_token] = len(inv_relation_vocab)
r = inv_relation_vocab[r_token]
if t_token not in inv_transductive_vocab:
inv_transductive_vocab[t_token] = len(inv_transductive_vocab)
t = inv_transductive_vocab[t_token]
triplets.append((h, t, r))
num_sample += 1
num_samples.append(num_sample)
for txt_file in inductive_files:
with open(txt_file, "r") as fin:
reader = csv.reader(fin, delimiter="\t")
if verbose:
reader = tqdm(reader, "Loading %s" % txt_file, utils.get_line_count(txt_file))
num_sample = 0
for tokens in reader:
h_token, r_token, t_token = tokens
if h_token not in inv_inductive_vocab:
inv_inductive_vocab[h_token] = len(inv_inductive_vocab)
h = inv_inductive_vocab[h_token]
assert r_token in inv_relation_vocab
r = inv_relation_vocab[r_token]
if t_token not in inv_inductive_vocab:
inv_inductive_vocab[t_token] = len(inv_inductive_vocab)
t = inv_inductive_vocab[t_token]
triplets.append((h, t, r))
num_sample += 1
num_samples.append(num_sample)
transductive_vocab, inv_transductive_vocab = self._standarize_vocab(None, inv_transductive_vocab)
inductive_vocab, inv_inductive_vocab = self._standarize_vocab(None, inv_inductive_vocab)
relation_vocab, inv_relation_vocab = self._standarize_vocab(None, inv_relation_vocab)
self.fact_graph = data.Graph(triplets[:num_samples[0]],
num_node=len(transductive_vocab), num_relation=len(relation_vocab))
self.graph = data.Graph(triplets[:sum(num_samples[:3])],
num_node=len(transductive_vocab), num_relation=len(relation_vocab))
self.inductive_fact_graph = data.Graph(triplets[sum(num_samples[:3]): sum(num_samples[:4])],
num_node=len(inductive_vocab), num_relation=len(relation_vocab))
self.inductive_graph = data.Graph(triplets[sum(num_samples[:3]):],
num_node=len(inductive_vocab), num_relation=len(relation_vocab))
self.triplets = torch.tensor(triplets[:sum(num_samples[:2])] + triplets[sum(num_samples[:4]):])
self.num_samples = num_samples[:2] + [sum(num_samples[4:])]
self.transductive_vocab = transductive_vocab
self.inductive_vocab = inductive_vocab
self.relation_vocab = relation_vocab
self.inv_transductive_vocab = inv_transductive_vocab
self.inv_inductive_vocab = inv_inductive_vocab
self.inv_relation_vocab = inv_relation_vocab
def __getitem__(self, index):
return self.triplets[index]
def split(self):
offset = 0
splits = []
for num_sample in self.num_samples:
split = torch_data.Subset(self, range(offset, offset + num_sample))
splits.append(split)
offset += num_sample
return splits
@R.register("dataset.FB15k237Inductive")
class FB15k237Inductive(InductiveKnowledgeGraphDataset):
transductive_urls = [
"https://raw.githubusercontent.com/kkteru/grail/master/data/fb237_%s/train.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/fb237_%s/valid.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/fb237_%s/test.txt",
]
inductive_urls = [
"https://raw.githubusercontent.com/kkteru/grail/master/data/fb237_%s_ind/train.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/fb237_%s_ind/valid.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/fb237_%s_ind/test.txt",
]
def __init__(self, path, version="v1", verbose=1):
path = 'data/datasets'
if not os.path.exists(path):
os.makedirs(path)
self.path = path
transductive_files = []
for url in self.transductive_urls:
url = url % version
save_file = "fb15k237_%s_%s" % (version, os.path.basename(url))
txt_file = os.path.join(path, save_file)
if not os.path.exists(txt_file):
txt_file = utils.download(url, self.path, save_file=save_file)
transductive_files.append(txt_file)
inductive_files = []
for url in self.inductive_urls:
url = url % version
save_file = "fb15k237_%s_ind_%s" % (version, os.path.basename(url))
txt_file = os.path.join(path, save_file)
if not os.path.exists(txt_file):
txt_file = utils.download(url, self.path, save_file=save_file)
inductive_files.append(txt_file)
self.load_inductive_tsvs(transductive_files, inductive_files, verbose=verbose)
@R.register("dataset.WN18RRInductive")
class WN18RRInductive(InductiveKnowledgeGraphDataset):
transductive_urls = [
"https://raw.githubusercontent.com/kkteru/grail/master/data/WN18RR_%s/train.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/WN18RR_%s/valid.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/WN18RR_%s/test.txt",
]
inductive_urls = [
"https://raw.githubusercontent.com/kkteru/grail/master/data/WN18RR_%s_ind/train.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/WN18RR_%s_ind/valid.txt",
"https://raw.githubusercontent.com/kkteru/grail/master/data/WN18RR_%s_ind/test.txt",
]
def __init__(self, path, version="v1", verbose=1):
path = os.path.expanduser(path)
if not os.path.exists(path):
os.makedirs(path)
self.path = path
transductive_files = []
for url in self.transductive_urls:
url = url % version
save_file = "wn18rr_%s_%s" % (version, os.path.basename(url))
txt_file = os.path.join(path, save_file)
if not os.path.exists(txt_file):
txt_file = utils.download(url, self.path, save_file=save_file)
transductive_files.append(txt_file)
inductive_files = []
for url in self.inductive_urls:
url = url % version
save_file = "wn18rr_%s_ind_%s" % (version, os.path.basename(url))
txt_file = os.path.join(path, save_file)
if not os.path.exists(txt_file):
txt_file = utils.download(url, self.path, save_file=save_file)
inductive_files.append(txt_file)
self.load_inductive_tsvs(transductive_files, inductive_files, verbose=verbose)