forked from TamSiuhin/KRACL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataset.py
398 lines (330 loc) · 16.9 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
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
from itertools import chain
import torch
from torch.utils.data import Dataset
import torch
import torch.utils.data as Data
import json
from utils import add_inverse_triples, add_noise
# class WN18RR_Inverse(Dataset):
# def __init__(self, name, batch_size, path="./data/WN18RR/"):
# self.name = name
# self.batch_size = batch_size
# dataset = WN18RR(create_inverse_triples=True)
# if name in ["train" ,"valid"]:
# data = dataset.training.mapped_triples
# h, r, t = data.t()
# r = r*2
# data = torch.cat([torch.stack([h, r, t], dim=-1), torch.stack([t, r+1, h], dim=1)], dim=0)
# self.src, self.edge_type, self.dst = data.t()
# self.edge_index = torch.stack((self.src, self.dst), dim=0)
# if name == "valid":
# self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# self.valid_data = add_inverse_triples(dataset.validation.mapped_triples)
# if name in ["train", "valid"]:
# transe_pre = torch.load(path)
# self.x = transe_pre["entity_embeddings._embeddings.weight"]
# self.rel_emb = transe_pre["relation_embeddings._embeddings.weight"]
# self.rel_emb = torch.cat((self.rel_emb, -self.rel_emb), dim=1).view(-1, 200)
# if name in ["test"]:
# self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# self.valid_data = add_inverse_triples(dataset.validation.mapped_triples)
# self.test_data = add_inverse_triples(dataset.testing.mapped_triples)
# def __len__(self):
# if self.name == "train":
# return int(self.edge_index.size(1) / self.batch_size)
# else:
# return 1
# def __getitem__(self, index):
# if self.name in ["train", "train_get_emb"]:
# return self.x, self.edge_index, self.rel_emb, self.edge_type
# elif self.name == "valid":
# return self.train_data, self.valid_data, self.x, self.edge_index, self.edge_type
# elif self.name == "test":
# return self.train_data, self.valid_data, self.test_data
# class FB15k237_Inverse(Dataset):
# def __init__(self, name, batch_size, dim=100, path="./data/WN18RR/"):
# self.name = name
# self.batch_size = batch_size
# dataset = FB15k237(create_inverse_triples=False)
# self.dim = dim
# if name in ["train" ,"train_get_emb"]:
# data = dataset.training.mapped_triples
# h, r, t = data.t()
# r = r*2
# data = torch.cat([torch.stack([h, r, t], dim=-1), torch.stack([t, r+1, h], dim=1)], dim=0)
# self.src, self.edge_type, self.dst = data.t()
# self.edge_index = torch.stack((self.src, self.dst), dim=0)
# if name == "valid":
# self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# self.valid_data = add_inverse_triples(dataset.validation.mapped_triples)
# self.test_data = add_inverse_triples(dataset.testing.mapped_triples)
# if name in ["train", "train_get_emb"]:
# if self.dim == 100:
# transe_pre = torch.load(path + "TransE_FB15k237_100dim.ckpt")
# if self.dim == 200:
# transe_pre = torch.load(path + "TransE_FB15k237_200dim.ckpt")
# self.x = transe_pre["entity_embeddings._embeddings.weight"]
# self.rel_emb = transe_pre["relation_embeddings._embeddings.weight"]
# self.rel_emb = torch.cat((self.rel_emb, -self.rel_emb), dim=1).view(-1, 200)
# def __len__(self):
# if self.name == "train":
# return int(self.edge_index.size(1) / self.batch_size)
# else:
# return 1
# def __getitem__(self, index):
# if self.name in ["train", "train_get_emb"]:
# return self.x, self.edge_index, self.rel_emb, self.edge_type
# elif self.name == "valid":
# return self.train_data, self.valid_data, self.test_data
# class WN18RR_Triples(Dataset):
# def __init__(self, name, batch_size):
# self.name = name
# self.batch_size = batch_size
# dataset = WN18RR()
# if name in ["train"]:
# self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# src, self.edge_type, dst = self.train_data.t()
# self.edge_index = torch.stack((src, dst), dim=0)
# if name in ["valid"]:
# self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# self.valid_data = add_inverse_triples(dataset.validation.mapped_triples)
# self.src, self.edge_type, self.dst = self.train_data.t()
# self.edge_index = torch.stack((self.src, self.dst), dim=0)
# if name in ["test"]:
# self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# self.valid_data = add_inverse_triples(dataset.validation.mapped_triples)
# self.test_data = add_inverse_triples(dataset.testing.mapped_triples)
# def __len__(self):
# if self.name in ["train"]:
# return int(self.edge_index.size(1) / self.batch_size)
# else:
# return 1
# def __getitem__(self, index):
# if self.name in ["train"]:
# return self.edge_index, self.edge_type, self.train_data
# elif self.name == "valid":
# return self.train_data, self.valid_data, self.edge_index, self.edge_type
# elif self.name == "test":
# return self.train_data, self.valid_data, self.test_data
# # class KG_Triples(Dataset):
# # def __init__(self, name, batch_size, data_name, per=1.0):
# # self.name = name
# # self.batch_size = batch_size
# # if data_name.lower() in ["wn18rr"]:
# # dataset = WN18RR()
# # elif data_name.lower() in ["fb15k237"]:
# # dataset = FB15k237()
# # elif data_name.lower() in ["db100k"]:
# # dataset = DB100K()
# # elif data_name.lower() in ["wn18"]:
# # dataset = WN18()
# # elif data_name.lower() in ["fb15k"]:
# # dataset = FB15k()
# # elif data_name.lower() in ["yago310"]:
# # dataset = YAGO310()
# # elif data_name.lower() in ["kinship"]:
# # dataset = Kinships()
# # elif data_name.lower() in ["umls"]:
# # dataset = UMLS()
# # else:
# # raise ValueError("there is no such dataset!")
# # if (per != 1.0) and (name=="train"):
# # mask = torch.rand(dataset.training.mapped_triples.size(0))< per
# # dataset.training.mapped_triples = dataset.training.mapped_triples[mask]
# # if name in ["train"]:
# # self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# # src, self.edge_type, dst = self.train_data.t()
# # self.edge_index = torch.stack((src, dst), dim=0)
# # if name in ["valid"]:
# # self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# # self.valid_data = add_inverse_triples(dataset.validation.mapped_triples)
# # self.src, self.edge_type, self.dst = self.train_data.t()
# # self.edge_index = torch.stack((self.src, self.dst), dim=0)
# # if name in ["test"]:
# # self.train_data = add_inverse_triples(dataset.training.mapped_triples)
# # self.valid_data = add_inverse_triples(dataset.validation.mapped_triples)
# # self.test_data = add_inverse_triples(dataset.testing.mapped_triples)
# # def __len__(self):
# # if self.name in ["train"]:
# # return int(self.edge_index.size(1) / self.batch_size)
# # else:
# # return 1
# # def __getitem__(self, index):
# # if self.name in ["train"]:
# # return self.edge_index, self.edge_type, self.train_data
# # elif self.name == "valid":
# # return self.train_data, self.valid_data, self.edge_index, self.edge_type
# # elif self.name == "test":
# # return self.train_data, self.valid_data, self.test_data
def txt2triples(path):
with open(path, 'r') as f:
data = f.read().split()
src = data[1::3]
dst = data[2::3]
edge_type = data[3::3]
src = torch.tensor([int(i) for i in src])
dst = torch.tensor([int(i) for i in dst])
rel = torch.tensor([int(i) for i in edge_type])
data = add_inverse_triples(torch.stack((src, rel, dst), dim=1))
src, rel, dst = data.t()
return data, src, rel, dst
class NELL_txt(Dataset):
def __init__(self, name, batch_size, path):
self.name = name
self.batch_size = batch_size
if self.name == "train":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
if self.name == "valid":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(path + "valid2id.txt")
if self.name == "test":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(path + "valid2id.txt")
self.test_data, _, _, _ = txt2triples(path + "test2id.txt")
def __len__(self):
if self.name == "train":
return int(self.train_data.size(0) / self.batch_size)
else:
return 1
def __getitem__(self, index):
if self.name == "train":
return self.edge_index, self.edge_type, self.train_data
elif self.name == "valid":
return self.train_data, self.valid_data, self.edge_index, self.edge_type
elif self.name == "test":
return self.train_data, self.valid_data, self.test_data
class KG_Triples_txt(Dataset):
def __init__(self, name, batch_size, path="/data2/whr/tzx/OpenKE/benchmarks/WN18RR/"):
self.name = name
self.batch_size = batch_size
if self.name == "train":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
if self.name == "valid":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(path + "valid2id.txt")
if self.name == "test":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(path + "valid2id.txt")
self.test_data, _, _, _ = txt2triples(path + "test2id.txt")
def __len__(self):
if self.name == "train":
return int(self.train_data.size(0) / self.batch_size)
else:
return 1
def __getitem__(self, index):
if self.name == "train":
return self.edge_index, self.edge_type, self.train_data
elif self.name == "valid":
return self.train_data, self.valid_data, self.edge_index, self.edge_type
elif self.name == "test":
return self.train_data, self.valid_data, self.test_data
class Triple_Category(Dataset):
def __init__(self, name, batch_size, path):
self.name = name
self.batch_size = batch_size
if self.name == "train":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
if self.name == "valid":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(path + "valid2id.txt")
if self.name == "test":
self.train_data, src, rel, dst = txt2triples(path + "train2id.txt")
self.edge_type = rel
self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(path + "valid2id.txt")
self.relation1to1, _, _, _ = txt2triples(path + "1-1.txt")
self.relation1ton, _, _, _ = txt2triples(path + "1-n.txt")
self.relationnto1, _, _, _ = txt2triples(path + "n-1.txt")
self.relationnton, _, _, _ = txt2triples(path + "n-n.txt")
def __len__(self):
if self.name == "train":
return int(self.train_data.size(0) / self.batch_size)
else:
return 1
def __getitem__(self, index):
if self.name == "train":
return self.edge_index, self.edge_type, self.train_data
elif self.name == "valid":
return self.train_data, self.valid_data, self.edge_index, self.edge_type
elif self.name == "test":
return self.train_data, self.valid_data, self.relation1to1,self.relation1ton, self.relationnto1, self.relationnton
class KG_Triples(Dataset):
def __init__(self, name, num_relations, num_ent, train_path="./data/FB15K237/", test_path="./data/FB15K237/", noise_path=None, num_negs_per_pos=5):
self.name = name
self.num_relations = num_relations
self.num_entities = num_ent
self.num_negs_per_pos = num_negs_per_pos
self.noise_path = noise_path
self.train_data, _, _, _ = txt2triples(train_path + "train2id.txt")
if self.name == "train":
self.train_data, _, _, _ = txt2triples(train_path + "train2id.txt")
if self.noise_path is not None:
noise_data, _, _, _ = txt2triples(self.noise_path + "train2id.txt")
self.train_data, _, _, _ = add_noise(self.train_data, noise_data)
if self.name == "valid":
self.train_data, _, _, _ = txt2triples(test_path + "train2id.txt")
# self.edge_type = rel
# self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(test_path + "valid2id.txt")
self.test_data, _, _, _ = txt2triples(test_path + "test2id.txt")
if self.name == "test":
self.train_data, _, _, _ = txt2triples(test_path + "train2id.txt")
# self.edge_type = rel
# self.edge_index = torch.stack((src, dst), dim=0)
self.valid_data, _, _, _ = txt2triples(test_path + "valid2id.txt")
self.test_data, _, _, _ = txt2triples(test_path + "test2id.txt")
def corrupt_batch(self, positive_batch: torch.LongTensor) -> torch.LongTensor: # noqa: D102
batch_shape = positive_batch.shape[:-1]
# Copy positive batch for corruption.
# Do not detach, as no gradients should flow into the indices.
negative_batch = positive_batch.clone()
negative_batch = negative_batch.unsqueeze(dim=-2).repeat(*(1 for _ in batch_shape), self.num_negs_per_pos, 1)
corruption_index = torch.randint(1, size=(*batch_shape, self.num_negs_per_pos))
index_max = self.num_relations
mask = corruption_index == 1
# To make sure we don't replace the {head, relation, tail} by the
# original value we shift all values greater or equal than the original value by one up
# for that reason we choose the random value from [0, num_{heads, relations, tails} -1]
negative_indices = torch.randint(
high=index_max,
size=(mask.sum().item(),),
device=positive_batch.device,
)
# determine shift *before* writing the negative indices
shift = (negative_indices >= negative_batch[mask][:, 1]).long()
negative_indices += shift
# write the negative indices
negative_batch[
mask.unsqueeze(dim=-1) & (torch.arange(3) == 1).view(*(1 for _ in batch_shape), 1, 3)
] = negative_indices
return negative_batch
def __len__(self):
if self.name == "train":
return self.train_data.size(0)
else:
return 1
def __getitem__(self, index):
if self.name == "train":
pos_batch = self.train_data[index]
neg_batch = self.corrupt_batch(pos_batch)
return pos_batch, neg_batch
elif self.name == "valid":
return self.train_data, self.valid_data, self.test_data
elif self.name == "test":
return self.train_data, self.valid_data, self.test_data