-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.py
223 lines (208 loc) · 9.75 KB
/
model.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
import logging
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import random
import pickle
import math
import collections
import itertools
import time
from tqdm import tqdm
import os
import sys
import json
sys.path.append('rp')
from src.models import ComplEx
def load_kbc(model_path, device, nentity, nrelation):
model = ComplEx(sizes=[nentity, nrelation, nentity], rank=1000, init_size=1e-3)
state_dict = torch.load(model_path)
model.load_state_dict(state_dict)
model.to(device)
return model
@torch.no_grad()
def kge_forward(model, h, r, device, nentity):
bsz = h.size(0)
r = r.unsqueeze(-1).repeat(bsz, 1)
h = h.unsqueeze(-1)
positive_sample = torch.cat((h, r, h), dim=1)
score = model(positive_sample, score_rhs=True, score_rel=False, score_lhs=False)
return score[0]
@torch.no_grad()
def neural_adj_matrix(model, rel, nentity, device, thrshd, adj_list):
bsz = 100
softmax = nn.Softmax(dim=1)
relation_embedding = torch.zeros(nentity, nentity).to(torch.float)
r = torch.LongTensor([rel]).to(device)
num = torch.zeros(nentity, 1).to(torch.float).to(device)
for (h, t) in adj_list:
num[h, 0] += 1
num = torch.maximum(num, torch.ones(nentity, 1).to(torch.float).to(device))
for s in range(0, nentity, bsz):
t = min(nentity, s+bsz)
h = torch.arange(s, t).to(device)
score = kge_forward(model, h, r, device, nentity)
normalized_score = softmax(score) * num[s:t, :]
mask = (normalized_score >= thrshd).to(torch.float)
normalized_score = mask * normalized_score
relation_embedding[s:t, :] = normalized_score.to('cpu')
return relation_embedding
class KGReasoning(nn.Module):
def __init__(self, args, device, adj_list, query_name_dict, name_answer_dict):
super(KGReasoning, self).__init__()
self.nentity = args.nentity
self.nrelation = args.nrelation
self.device = device
self.relation_embeddings = list()
self.fraction = args.fraction
self.query_name_dict = query_name_dict
self.name_answer_dict = name_answer_dict
self.neg_scale = args.neg_scale
dataset_name = args.data_path.split('/')[1].split('-')[0]
if args.data_path.split('/')[1].split('-')[1] == "237":
dataset_name += "-237"
filename = 'neural_adj/'+dataset_name+'_'+str(args.fraction)+'_'+str(args.thrshd)+'.pt'
if os.path.exists(filename):
self.relation_embeddings = torch.load(filename, map_location=device)
else:
kbc_model = load_kbc(args.kbc_path, device, args.nentity, args.nrelation)
for i in tqdm(range(args.nrelation)):
relation_embedding = neural_adj_matrix(kbc_model, i, args.nentity, device, args.thrshd, adj_list[i])
relation_embedding = (relation_embedding>=1).to(torch.float) * 0.9999 + (relation_embedding<1).to(torch.float) * relation_embedding
for (h, t) in adj_list[i]:
relation_embedding[h, t] = 1.
# add fractional
fractional_relation_embedding = []
dim = args.nentity // args.fraction
rest = args.nentity - args.fraction * dim
for i in range(args.fraction):
s = i * dim
t = (i+1) * dim
if i == args.fraction - 1:
t += rest
fractional_relation_embedding.append(relation_embedding[s:t, :].to_sparse().to(self.device))
self.relation_embeddings.append(fractional_relation_embedding)
torch.save(self.relation_embeddings, filename)
def relation_projection(self, embedding, r_embedding, is_neg=False):
dim = self.nentity // self.fraction
rest = self.nentity - self.fraction * dim
new_embedding = torch.zeros_like(embedding).to(self.device)
r_argmax = torch.zeros(self.nentity).to(self.device)
for i in range(self.fraction):
s = i * dim
t = (i+1) * dim
if i == self.fraction - 1:
t += rest
fraction_embedding = embedding[:, s:t]
if fraction_embedding.sum().item() == 0:
continue
nonzero = torch.nonzero(fraction_embedding, as_tuple=True)[1]
fraction_embedding = fraction_embedding[:, nonzero]
fraction_r_embedding = r_embedding[i].to_dense()[nonzero, :].unsqueeze(0)
if is_neg:
fraction_r_embedding = torch.minimum(torch.ones_like(fraction_r_embedding).to(torch.float), self.neg_scale*fraction_r_embedding)
fraction_r_embedding = 1. - fraction_r_embedding
fraction_embedding_premax = fraction_r_embedding * fraction_embedding.unsqueeze(-1)
fraction_embedding, tmp_argmax = torch.max(fraction_embedding_premax, dim=1)
tmp_argmax = nonzero[tmp_argmax.squeeze()] + s
new_argmax = (fraction_embedding > new_embedding).to(torch.long).squeeze()
r_argmax = new_argmax * tmp_argmax + (1-new_argmax) * r_argmax
new_embedding = torch.maximum(new_embedding, fraction_embedding)
return new_embedding, r_argmax.cpu().numpy()
def intersection(self, embeddings):
return torch.prod(embeddings, dim=0)
def union(self, embeddings):
return (1. - torch.prod(1.-embeddings, dim=0))
def embed_query(self, queries, query_structure, idx):
'''
Iterative embed a batch of queries with same structure
queries: a flattened batch of queries
'''
all_relation_flag = True
exec_query = []
for ele in query_structure[-1]: # whether the current query tree has merged to one branch and only need to do relation traversal, e.g., path queries or conjunctive queries after the intersection
if ele not in ['r', 'n']:
all_relation_flag = False
break
if all_relation_flag:
if query_structure[0] == 'e':
bsz = queries.size(0)
embedding = torch.zeros(bsz, self.nentity).to(torch.float).to(self.device)
embedding.scatter_(-1, queries[:, idx].unsqueeze(-1), 1)
exec_query.append(queries[:, idx].item())
idx += 1
else:
embedding, idx, pre_exec_query = self.embed_query(queries, query_structure[0], idx)
exec_query.append(pre_exec_query)
r_exec_query = []
for i in range(len(query_structure[-1])):
if query_structure[-1][i] == 'n':
assert (queries[:, idx] == -2).all()
r_exec_query.append('n')
else:
r_embedding = self.relation_embeddings[queries[0, idx]]
if (i < len(query_structure[-1]) - 1) and query_structure[-1][i+1] == 'n':
embedding, r_argmax = self.relation_projection(embedding, r_embedding, True)
else:
embedding, r_argmax = self.relation_projection(embedding, r_embedding, False)
r_exec_query.append((queries[0, idx].item(), r_argmax))
r_exec_query.append('e')
idx += 1
r_exec_query.pop()
exec_query.append(r_exec_query)
exec_query.append('e')
else:
embedding_list = []
union_flag = False
for ele in query_structure[-1]:
if ele == 'u':
union_flag = True
query_structure = query_structure[:-1]
break
for i in range(len(query_structure)):
embedding, idx, pre_exec_query = self.embed_query(queries, query_structure[i], idx)
embedding_list.append(embedding)
exec_query.append(pre_exec_query)
if union_flag:
embedding = self.union(torch.stack(embedding_list))
idx += 1
exec_query.append(['u'])
else:
embedding = self.intersection(torch.stack(embedding_list))
exec_query.append('e')
return embedding, idx, exec_query
def find_ans(self, exec_query, query_structure, anchor):
ans_structure = self.name_answer_dict[self.query_name_dict[query_structure]]
return self.backward_ans(ans_structure, exec_query, anchor)
def backward_ans(self, ans_structure, exec_query, anchor):
if ans_structure == 'e': # 'e'
return exec_query, exec_query
elif ans_structure[0] == 'u': # 'u'
return ['u'], 'u'
elif ans_structure[0] == 'r': # ['r', 'e', 'r']
cur_ent = anchor
ans = []
for ele, query_ele in zip(ans_structure[::-1], exec_query[::-1]):
if ele == 'r':
r_id, r_argmax = query_ele
ans.append(r_id)
cur_ent = int(r_argmax[cur_ent])
elif ele == 'n':
ans.append('n')
else:
ans.append(cur_ent)
return ans[::-1], cur_ent
elif ans_structure[1][0] == 'r': # [[...], ['r', ...], 'e']
r_ans, r_ent = self.backward_ans(ans_structure[1], exec_query[1], anchor)
e_ans, e_ent = self.backward_ans(ans_structure[0], exec_query[0], r_ent)
ans = [e_ans, r_ans, anchor]
return ans, e_ent
else: # [[...], [...], 'e']
ans = []
for ele, query_ele in zip(ans_structure[:-1], exec_query[:-1]):
ele_ans, ele_ent = self.backward_ans(ele, query_ele, anchor)
ans.append(ele_ans)
ans.append(anchor)
return ans, ele_ent