-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
289 lines (215 loc) · 11.1 KB
/
utils.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
import random
import torch
import os
import tqdm
import pickle
import numpy as np
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
# Data load and Preprocess
def load_data(file_path):
'''
argument:
file_path: ./Dataset/raw_data/FB15k-237
return:
entity2id, relation2id, train_triplets, valid_triplets, test_triplets
'''
print("load data from {}".format(file_path))
with open(os.path.join(file_path, 'entity2id.txt')) as f:
entity2id = dict()
for line in f:
entity, eid = line.strip().split('\t')
entity2id[entity] = int(eid)
with open(os.path.join(file_path, 'relation2id.txt')) as f:
relation2id = dict()
for line in f:
relation, rid = line.strip().split('\t')
relation2id[relation] = int(rid)
train_triplets = read_triplets(os.path.join(file_path, 'train.txt'), entity2id, relation2id)
valid_triplets = read_triplets(os.path.join(file_path, 'valid.txt'), entity2id, relation2id)
test_triplets = read_triplets(os.path.join(file_path, 'test.txt'), entity2id, relation2id)
print('num_entity: {}'.format(len(entity2id)))
print('num_relation: {}'.format(len(relation2id)))
print('num_train_triples: {}'.format(len(train_triplets)))
print('num_valid_triples: {}'.format(len(valid_triplets)))
print('num_test_triples: {}'.format(len(test_triplets)))
return entity2id, relation2id, train_triplets, valid_triplets, test_triplets
def read_triplets(file_path, entity2id, relation2id):
triplets = []
with open(file_path) as f:
for line in f:
head, relation, tail = line.strip().split('\t')
triplets.append((entity2id[head], relation2id[relation], entity2id[tail]))
return np.array(triplets)
def load_processed_data(file_path):
with open(os.path.join(file_path, 'filtered_triplets.pickle'), 'rb') as f:
filtered_triplets = pickle.load(f)
with open(os.path.join(file_path, 'meta_train_task_triplets.pickle'), 'rb') as f:
meta_train_task_triplets = pickle.load(f)
with open(os.path.join(file_path, 'meta_valid_task_triplets.pickle'), 'rb') as f:
meta_valid_task_triplets = pickle.load(f)
with open(os.path.join(file_path, 'meta_test_task_triplets.pickle'), 'rb') as f:
meta_test_task_triplets = pickle.load(f)
with open(os.path.join(file_path, 'meta_train_task_entity_to_triplets.pickle'), 'rb') as f:
meta_train_task_entity_to_triplets = pickle.load(f)
with open(os.path.join(file_path, 'meta_valid_task_entity_to_triplets.pickle'), 'rb') as f:
meta_valid_task_entity_to_triplets = pickle.load(f)
with open(os.path.join(file_path, 'meta_test_task_entity_to_triplets.pickle'), 'rb') as f:
meta_test_task_entity_to_triplets = pickle.load(f)
# return filtered_triplets, meta_train_task_triplets, meta_valid_task_triplets, meta_test_task_triplets, \
# meta_train_task_entity_to_triplets, meta_valid_task_entity_to_triplets, meta_test_task_entity_to_triplets
return meta_train_task_entity_to_triplets, meta_valid_task_entity_to_triplets, meta_test_task_entity_to_triplets
def sort_and_rank(score, target):
x, indices = torch.sort(score, dim=1, descending=True) # 按行降序排列
indices = torch.nonzero(indices == target.view(-1, 1))
indices = indices[:, 1].view(-1)
return indices
def calc_induc_mrr(pool, args, rw_func, rw_encoder, decoder, z, unseen_entity, unseen_entity_embedding, all_entity_embeddings, all_relation_embeddings, test_triplets, all_triplets, use_cuda, score_function='DistMult'):
num_entity = len(all_entity_embeddings)
subject_count = 0
object_count = 0
ranks = []
ranks_s = []
ranks_o = []
ranks_un = []
total_ranks = []
subject_ranks = []
object_ranks = []
total_induc_ranks = []
subject_induc_ranks = []
object_induc_ranks = []
total_trans_ranks = []
subject_trans_ranks = []
object_trans_ranks = []
head_relation_triplets = all_triplets[:, :2]
tail_relation_triplets = torch.stack((all_triplets[:, 2], all_triplets[:, 1])).transpose(0, 1)
d = 'cuda:{}'.format(args.gpu)
device = torch.device(d)
for test_triplet in test_triplets:
# import time
# s = time.time()
is_trans = False
is_subject = False
is_object = False
if (test_triplet[0] in pool) and (test_triplet[2] in pool):
is_trans = True
if test_triplet[0] == unseen_entity:
subject_count += 1
is_subject = True
subject = test_triplet[0]
relation = test_triplet[1]
object_ = test_triplet[2]
subject_relation = torch.LongTensor([subject, relation])
if use_cuda:
subject_relation = subject_relation.to(device)
delete_index = torch.sum(head_relation_triplets == subject_relation, dim = 1)
delete_index = torch.nonzero(delete_index == 2).squeeze()
if use_cuda:
# device = torch.device('cuda')
delete_entity_index = all_triplets[delete_index, 2].view(-1).cpu().numpy()
perturb_entity_index = np.array(list(set(np.arange(num_entity)) - set(delete_entity_index)))
perturb_entity_index = torch.from_numpy(perturb_entity_index).to(device)
perturb_entity_index = torch.cat((object_.view(-1), perturb_entity_index))
else:
delete_entity_index = all_triplets[delete_index, 2].view(-1).numpy()
perturb_entity_index = np.array(list(set(np.arange(num_entity)) - set(delete_entity_index)))
perturb_entity_index = torch.from_numpy(perturb_entity_index)
perturb_entity_index = torch.cat((object_.view(-1), perturb_entity_index))
# rnn - candidate
arw = rw_func(perturb_entity_index)
arw_emb, _ = rw_encoder(arw.to(device))
# arw_emb, _ = rw_encoder(arw.to(device))
arw_emb = arw_emb.view(-1, 5, 10, args.rw).mean(1).mean(1)
# Score
if score_function == 'DistMult':
emb_ar = decoder(unseen_entity_embedding, z, arw_emb) * all_relation_embeddings[relation]
emb_ar = emb_ar.view(-1, 1, 1)
emb_c = decoder(all_entity_embeddings[perturb_entity_index], z, arw_emb)
emb_c = emb_c.transpose(0, 1).unsqueeze(1)
out_prod = torch.bmm(emb_ar, emb_c)
score = torch.sum(out_prod, dim = 0)
elif score_function == 'TransE':
# one decoder h/t = h/t + mlp(z)
head_embedding = decoder(unseen_entity_embedding, z, arw_emb)
relation_embedding = all_relation_embeddings[relation]
tail_embeddings = decoder(all_entity_embeddings[perturb_entity_index], z, arw_emb)
score = - torch.norm((head_embedding + relation_embedding - tail_embeddings), p = 2, dim = 1)
score = score.view(1, -1)
else:
raise TypeError
elif test_triplet[2] == unseen_entity:
object_count += 1
is_object = True
subject = test_triplet[0]
relation = test_triplet[1]
object_ = test_triplet[2]
object_relation = torch.LongTensor([object_, relation])
if use_cuda:
object_relation = object_relation.to(device)
delete_index = torch.sum(tail_relation_triplets == object_relation, dim = 1)
delete_index = torch.nonzero(delete_index == 2).squeeze()
if use_cuda:
# device = torch.device('cuda')
delete_entity_index = all_triplets[delete_index, 0].view(-1).cpu().numpy()
perturb_entity_index = np.array(list(set(np.arange(num_entity)) - set(delete_entity_index)))
perturb_entity_index = torch.from_numpy(perturb_entity_index).to(device)
perturb_entity_index = torch.cat((subject.view(-1), perturb_entity_index))
else:
delete_entity_index = all_triplets[delete_index, 0].view(-1).numpy()
perturb_entity_index = np.array(list(set(np.arange(num_entity)) - set(delete_entity_index)))
perturb_entity_index = torch.from_numpy(perturb_entity_index)
perturb_entity_index = torch.cat((subject.view(-1), perturb_entity_index))
# rnn - candidate
arw = rw_func(perturb_entity_index)
arw_emb, _ = rw_encoder(arw.to(device))
# arw_emb, _ = rw_encoder(arw.to(device))
arw_emb = arw_emb.view(-1, 5, 10, args.rw).mean(1).mean(1)
if score_function == 'DistMult':
emb_ar = decoder(unseen_entity_embedding, z, arw_emb) * all_relation_embeddings[relation]
emb_ar = emb_ar.view(-1, 1, 1)
emb_c = decoder(all_entity_embeddings[perturb_entity_index], z)
emb_c = emb_c.transpose(0, 1).unsqueeze(1)
out_prod = torch.bmm(emb_ar, emb_c)
score = torch.sum(out_prod, dim = 0)
elif score_function == 'TransE':
# one decoder h/t = h/t + mlp(z)
head_embeddings = decoder(all_entity_embeddings[perturb_entity_index], z, arw_emb)
relation_embedding = all_relation_embeddings[relation]
tail_embedding = decoder(unseen_entity_embedding, z, arw_emb)
score = head_embeddings + relation_embedding - tail_embedding
score = - torch.norm(score, p = 2, dim = 1)
score = score.view(1, -1)
else:
raise TypeError
else:
raise TypeError
# Cal Rank
if use_cuda:
target = torch.tensor(0).to(device)
rank = sort_and_rank(score, target)
else:
target = torch.tensor(0)
rank = sort_and_rank(score, target)
rank += 1
total_ranks.append(rank)
if is_subject:
subject_ranks.append(rank)
elif is_object:
object_ranks.append(rank)
if is_trans:
total_trans_ranks.append(rank)
if is_subject:
subject_trans_ranks.append(rank)
elif is_object:
object_trans_ranks.append(rank)
else:
total_induc_ranks.append(rank)
if is_subject:
subject_induc_ranks.append(rank)
elif is_object:
object_induc_ranks.append(rank)
return total_ranks, total_induc_ranks, total_trans_ranks