-
Notifications
You must be signed in to change notification settings - Fork 3
/
neg_dbpedia.py
253 lines (219 loc) · 8.74 KB
/
neg_dbpedia.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
import torch
import random
import numpy as np
import time
from collections import defaultdict
import pandas as pd
from torch.utils import data as torch_data
import pickle
from numpy import genfromtxt
import logging
import itertools
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
neg_nb = 1000
###########################################################################
def antitc_except(triple, side, num_ent):
corr = random.randint(0, num_ent - 1)
if side == 'head':
while r2id2dom2id[triple[1]] in instype_all[corr]:
corr = random.randint(0, num_ent - 1)
else:
while r2id2range2id[triple[1]] in instype_all[corr]:
corr = random.randint(0, num_ent - 1)
return int(corr)
def get_observed_triples(train2id, valid2id, test2id):
all_possible_hs = defaultdict(dict)
all_possible_ts = defaultdict(dict)
train2id = train2id[(train2id['r'].isin(r2id2dom2id.keys())) & (
train2id['r'].isin(r2id2range2id.keys()))]
train2id = train2id[~train2id['r'].isin(rels_suppr)]
train2id = torch.as_tensor(train2id.to_numpy(), dtype=torch.int32)
valid2id = torch.as_tensor(valid2id.to_numpy(), dtype=torch.int32)
test2id = torch.as_tensor(test2id.to_numpy(), dtype=torch.int32)
all_triples = torch.cat((train2id, valid2id, test2id))
X = all_triples.detach().clone()
for triple in range(X.shape[0]):
h, r, t = X[triple][0].item(), X[triple][1].item(), X[triple][2].item()
try:
all_possible_ts[h][r].append(t)
except KeyError:
all_possible_ts[h][r] = [t]
for triple in range(X.shape[0]):
h, r, t = X[triple][0].item(), X[triple][1].item(), X[triple][2].item()
try:
all_possible_hs[t][r].append(h)
except KeyError:
all_possible_hs[t][r] = [h]
all_possible_ts = dict(all_possible_ts)
all_possible_hs = dict(all_possible_hs)
return all_possible_hs, all_possible_ts
def sem_neg_files(train2id, neg_nb):
start = time.time()
sem_hr_, sem_tr_ = defaultdict(dict), defaultdict(dict)
train2id = train2id.to_numpy()
for idx, triple in enumerate(train2id):
h, r, t = triple[0], triple[1], triple[2]
if (len(class2id2ent2id[r2id2range2id[r]]) > 1) and (
len(class2id2ent2id[r2id2dom2id[r]]) > 1):
if not (h in sem_hr_ and r in sem_hr_[h]):
sem_t = list(set(np.random.choice(
class2id2ent2id[r2id2range2id[r]], size=neg_nb)))
sem_hr_[h][r] = sem_t
if not (t in sem_tr_ and r in sem_tr_[t]):
sem_h = list(set(np.random.choice(
class2id2ent2id[r2id2dom2id[r]], size=neg_nb)))
sem_tr_[t][r] = sem_h
if idx % 50000 == 0:
print(idx, ' triples processed.')
print('total time:', time.time() - start)
sem_hr_, sem_tr_ = dict(sem_hr_), dict(sem_tr_)
start = time.time()
print('Filtering.')
for h, rts in sem_hr_.items():
for r, ts in rts.items():
intersect = set(ts).intersection(all_possible_ts[h][r])
if len(intersect) > 0:
sem_hr_[h][r] = list(set(ts) - set(all_possible_ts[h][r]))
for t, rhs in sem_tr_.items():
for r, hs in rhs.items():
intersect = set(hs).intersection(all_possible_hs[t][r])
if len(intersect) > 0:
sem_tr_[t][r] = list(set(hs) - set(all_possible_hs[t][r]))
print('total time:', time.time() - start)
return sem_hr_, sem_tr_
def dumb_neg_files(train2id, neg_nb):
start = time.time()
dumb_hr_, dumb_tr_ = defaultdict(dict), defaultdict(dict)
train2id = train2id[(train2id['r'].isin(r2id2dom2id.keys())) & (
train2id['r'].isin(r2id2range2id.keys()))]
train2id = train2id.to_numpy()
for idx, triple in enumerate(train2id):
h, r, t = triple[0], triple[1], triple[2]
dumb_hr_[h][r], dumb_tr_[t][r] = [], []
for i in range(neg_nb):
dumb_t = antitc_except(triple, side='tail', num_ent=len(ent2id))
dumb_hr_[h][r].append(dumb_t)
dumb_h = antitc_except(triple, side='head', num_ent=len(ent2id))
dumb_tr_[t][r].append(dumb_h)
dumb_hr_[h][r] = list(set(dumb_hr_[h][r]))
dumb_tr_[t][r] = list(set(dumb_tr_[t][r]))
if idx % 50000 == 0:
print(idx, ' triples processed.')
print('total time:', time.time() - start)
dumb_hr_, dumb_tr_ = dict(dumb_hr_), dict(dumb_tr_)
start = time.time()
print('Filtering.')
for h, rts in dumb_hr_.items():
for r, ts in rts.items():
intersect = set(ts).intersection(all_possible_ts[h][r])
if len(intersect) > 0:
dumb_hr_[h][r] = list(set(ts) - set(all_possible_ts[h][r]))
for t, rhs in dumb_tr_.items():
for r, hs in rhs.items():
intersect = set(hs).intersection(all_possible_hs[t][r])
if len(intersect) > 0:
dumb_tr_[t][r] = list(set(hs) - set(all_possible_hs[t][r]))
print('total time:', time.time() - start)
return dict(dumb_hr_), dict(dumb_tr_)
###########################################################################
dataset = 'datasets/DBpedia77k/'
train2id = pd.read_csv(
dataset +
"train2id.txt",
sep='\t',
header=None,
names=[
'h',
'r',
't'])
valid2id = pd.read_csv(
dataset +
"valid2id.txt",
sep='\t',
header=None,
names=[
'h',
'r',
't'])
test2id = pd.read_csv(
dataset +
"test2id.txt",
sep='\t',
header=None,
names=[
'h',
'r',
't'])
with open(dataset + 'pickle/r2id2dom2id.pkl', 'rb') as f:
r2id2dom2id = pickle.load(f)
with open(dataset + 'pickle/r2id2range2id.pkl', 'rb') as f:
r2id2range2id = pickle.load(f)
with open(dataset + 'pickle/class2id2ent2id.pkl', 'rb') as f:
class2id2ent2id = pickle.load(f)
with open(dataset + 'pickle/class2id.pkl', 'rb') as f:
class2id = pickle.load(f)
with open(dataset + 'pickle/instype_all.pkl', 'rb') as f:
instype_all = pickle.load(f)
with open(dataset + 'pickle/ent2id.pkl', 'rb') as f:
ent2id = pickle.load(f)
with open(dataset + 'pickle/rel2id.pkl', 'rb') as f:
rel2id = pickle.load(f)
train2id = train2id[(train2id['r'].isin(r2id2dom2id.keys()))
& (train2id['r'].isin(r2id2range2id.keys()))]
all_rels = train2id['r'].unique()
rels_suppr = []
pb_dom = list(set(r2id2dom2id.values()) - set(class2id2ent2id.keys()))
pb_range = list(set(r2id2range2id.values()) - set(class2id2ent2id.keys()))
for r in all_rels:
if r2id2dom2id[r] in pb_dom:
rels_suppr.append(r)
if r2id2range2id[r] in pb_range:
rels_suppr.append(r)
rels_suppr = list(set(rels_suppr))
train2id = train2id[~train2id['r'].isin(rels_suppr)]
all_possible_hs, all_possible_ts = get_observed_triples(
train2id, valid2id, test2id)
print('sem negatives.')
neg_sem = 300
if neg_sem > 150:
if neg_sem <= 300:
sem_hr_1, sem_tr_1 = sem_neg_files(train2id, neg_sem // 2)
sem_hr_2, sem_tr_2 = sem_neg_files(train2id, neg_sem // 2)
with open(dataset + 'pickle/sem_hr_1.pkl', 'wb') as f:
pickle.dump(sem_hr_1, f)
with open(dataset + 'pickle/sem_tr_1.pkl', 'wb') as f:
pickle.dump(sem_tr_1, f)
with open(dataset + 'pickle/sem_hr_2.pkl', 'wb') as f:
pickle.dump(sem_hr_2, f)
with open(dataset + 'pickle/sem_tr_2.pkl', 'wb') as f:
pickle.dump(sem_tr_2, f)
elif neg_sem <= 500:
sem_hr_1, sem_tr_1 = sem_neg_files(train2id, neg_sem // 3)
sem_hr_2, sem_tr_2 = sem_neg_files(train2id, neg_sem // 3)
sem_hr_3, sem_tr_3 = sem_neg_files(train2id, neg_sem // 3)
with open(dataset + 'pickle/sem_hr_1.pkl', 'wb') as f:
pickle.dump(sem_hr_1, f)
with open(dataset + 'pickle/sem_tr_1.pkl', 'wb') as f:
pickle.dump(sem_tr_1, f)
with open(dataset + 'pickle/sem_hr_2.pkl', 'wb') as f:
pickle.dump(sem_hr_2, f)
with open(dataset + 'pickle/sem_tr_2.pkl', 'wb') as f:
pickle.dump(sem_tr_2, f)
with open(dataset + 'pickle/sem_hr_3.pkl', 'wb') as f:
pickle.dump(sem_hr_3, f)
with open(dataset + 'pickle/sem_tr_3.pkl', 'wb') as f:
pickle.dump(sem_tr_3, f)
else:
sem_hr_, sem_tr_ = sem_neg_files(train2id, neg_sem)
with open(dataset + 'pickle/sem_hr.pkl', 'wb') as f:
pickle.dump(sem_hr_, f)
with open(dataset + 'pickle/sem_tr.pkl', 'wb') as f:
pickle.dump(sem_tr_, f)
print('dumb negatives.')
neg_dumb = 500
dumb_hr_, dumb_tr_ = dumb_neg_files(train2id, neg_dumb)
with open(dataset + 'pickle/dumb_hr.pkl', 'wb') as f:
pickle.dump(dumb_hr_, f)
with open(dataset + 'pickle/dumb_tr.pkl', 'wb') as f:
pickle.dump(dumb_tr_, f)