-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
223 lines (193 loc) · 7.78 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
import numpy as np
import pickle as pkl
import scipy.sparse as sp
import sys
import tensorflow as tf
import math
import os
import random
from collections import Counter
flags = tf.app.flags
FLAGS = flags.FLAGS
def create_exp_dir(path, scripts_to_save=None):
path_split = path.split("/")
path_i = "."
for one_path in path_split:
path_i += "/" + one_path
if not os.path.exists(path_i):
os.mkdir(path_i)
print('Experiment dir : {}'.format(path_i))
if scripts_to_save is not None:
os.mkdir(os.path.join(path, 'scripts'))
for script in scripts_to_save:
dst_file = os.path.join(path, 'scripts', os.path.basename(script))
shutil.copyfile(script, dst_file)
def inverse_sum(adj):
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, -1).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
return d_inv_sqrt.reshape((-1, 1))
def preprocess_adj(adj):
ent_adj_invsum = inverse_sum(adj[0])
rel_adj_invsum = inverse_sum(adj[1])
return [ent_adj_invsum, rel_adj_invsum, adj[2]]
def construct_feed_dict(features, support, placeholders):
feed_dict = dict()
feed_dict.update({placeholders['features']: features})
if isinstance(support[0], list):
for i in range(len(support)):
feed_dict.update({placeholders['support'][i][j]: support[i][j] \
for j in range(len(support[i]))})
else:
feed_dict.update({placeholders['support'][i]: support[i] \
for i in range(len(support))})
return feed_dict
def loadfile(file, num=1):
'''
num: number of elements per row
'''
print('loading file ' + file)
ret = []
with open(file, "r", encoding='utf-8') as rf:
for line in rf:
th = line[:-1].split('\t')
x = []
for i in range(num):
x.append(int(th[i]))
ret.append(tuple(x))
return ret
def get_ent2id(files):
ent2id = {}
for file in files:
with open(file, 'r', encoding='utf-8') as rf:
for line in rf:
th = line[:-1].split('\t')
ent2id[th[1]] = int(th[0])
return ent2id
def get_extended_adj_auto(e, KG):
nei_list = []
ent_row, rel_row = [], []
ent_col, rel_col = [], []
ent_data, rel_data = [], []
count = 0
for tri in KG:
nei_list.append([tri[0], tri[1], tri[2]])
ent_row.append(tri[0])
ent_col.append(count)
ent_data.append(1.)
ent_row.append(tri[2])
ent_col.append(count)
ent_data.append(1.)
rel_row.append(tri[1])
rel_col.append(count)
rel_data.append(1.)
count += 1
ent_adj_ind = sp.coo_matrix((ent_data, (ent_row, ent_col)), shape=(e, count))
rel_adj_ind = sp.coo_matrix((rel_data, (rel_row, rel_col)), shape=(max(rel_row)+1, count))
return [ent_adj_ind, rel_adj_ind, np.array(nei_list)]
def load_data_class(FLAGS):
def analysis(A, y, train, test):
for A_i in A:
print(A_i.nonzero())
exit()
def to_KG(A):
KG = []
count = 0
for A_i in A:
idx = A_i.nonzero()
for head, tail in zip(idx[0], idx[1]):
KG.append([head, count, tail])
if len(idx[0]) > 0:
count += 1
# print(KG[:100])
return KG
dirname = os.path.dirname(os.path.realpath(sys.argv[0]))
raw_file = dirname + '/data/class/' + FLAGS.dataset + '.pickle'
pro_file = dirname + '/data/class/' + FLAGS.dataset + 'pro.pickle'
if not os.path.exists(pro_file):
with open(dirname + '/data/class/' + FLAGS.dataset + '.pickle', 'rb') as f:
data = pkl.load(f)
A = data['A']
KG = to_KG(A)
num_ent = A[0].shape[0]
data["A"] = KG
data["e"] = num_ent
# analysis(A, y, train, test)
with open(dirname + '/data/class/' + FLAGS.dataset + 'pro.pickle', 'wb') as handle:
pkl.dump(data, handle, protocol=pkl.HIGHEST_PROTOCOL)
with open(dirname + '/data/class/' + FLAGS.dataset + 'pro.pickle', 'rb') as f:
data = pkl.load(f)
KG = data["A"]
# y: csr_sparse_matrix
y = sp.csr_matrix(data['y']).astype(np.float32)
train = data['train_idx']
test = data['test_idx']
label_ind = train + test
num_ent = data["e"]
# print("train class: ", Counter(np.argmax(np.asarray(y[train].todense()), 1).reshape(1,-1)[0]))
if FLAGS.dataset in ["wordnet", "fb15k"]:
random.shuffle(label_ind)
split = [0.1, 0.2]
train = label_ind[:int(split[0]*len(label_ind))]
valid = label_ind[int(split[0]*len(label_ind)):int(split[1]*len(label_ind))]
test = label_ind[int(split[1]*len(label_ind)):]
print("train {}, valid {}, test {}".format(len(train), len(valid), len(test)))
else:
valid = None
adj = get_extended_adj_auto(num_ent, KG)
return adj, num_ent, train, test, valid, y
def load_data_align(FLAGS):
names = [['ent_ids_1', 'ent_ids_2'], ['triples_1', 'triples_2'], ['ref_ent_ids']]
if FLAGS.rel_align:
names[1][1] = "triples_2_relaligned"
for fns in names:
for i in range(len(fns)):
fns[i] = 'data/'+FLAGS.dataset+'/'+fns[i]
Ent_files, Tri_files, align_file = names
num_ent = len(set(loadfile(Ent_files[0], 1)) | set(loadfile(Ent_files[1], 1)))
align_labels = loadfile(align_file[0], 2)
num_align_labels = len(align_labels)
np.random.shuffle(align_labels)
if not FLAGS.valid:
train = np.array(align_labels[:num_align_labels // 10 * FLAGS.seed])
valid = None
else:
train = np.array(align_labels[:int(num_align_labels // 10 * (FLAGS.seed-1))])
valid = align_labels[int(num_align_labels // 10 * (FLAGS.seed-1)): num_align_labels // 10 * FLAGS.seed]
test = align_labels[num_align_labels // 10 * FLAGS.seed:]
KG = loadfile(Tri_files[0], 3) + loadfile(Tri_files[1], 3)
ent2id = get_ent2id([Ent_files[0], Ent_files[1]])
adj = get_extended_adj_auto(num_ent, KG)
return adj, num_ent, train, test, valid
def load_data_rel_align(FLAGS):
names = [['ent_ids_1', 'ent_ids_2'], ['triples_1', 'triples_2'], ['ref_ent_ids']]
for fns in names:
for i in range(len(fns)):
fns[i] = 'data/'+FLAGS.dataset+'/'+fns[i]
Ent_files, Tri_files, align_file = names
num_ent = len(set(loadfile(Ent_files[0], 1)) | set(loadfile(Ent_files[1], 1)))
align_labels = loadfile(align_file[0], 2)
num_align_labels = len(align_labels)
np.random.shuffle(align_labels)
if not FLAGS.valid:
train = np.array(align_labels[:num_align_labels // 10 * FLAGS.seed])
valid = None
else:
train = np.array(align_labels[:int(num_align_labels // 10 * (FLAGS.seed-1))])
valid = align_labels[int(num_align_labels // 10 * (FLAGS.seed-1)): num_align_labels // 10 * FLAGS.seed]
test = align_labels[num_align_labels // 10 * FLAGS.seed:]
KG = loadfile(Tri_files[0], 3) + loadfile(Tri_files[1], 3)
ent2id = get_ent2id([Ent_files[0], Ent_files[1]])
adj = get_extended_adj_auto(num_ent, KG)
rel_align_labels = loadfile('data/'+FLAGS.dataset+"/ref_rel_ids", 2)
num_rel_align_labels = len(rel_align_labels)
np.random.shuffle(rel_align_labels)
if not FLAGS.valid:
train_rel = np.array(rel_align_labels[:num_rel_align_labels // 10 * FLAGS.rel_seed])
valid_rel = None
else:
train_rel = np.array(rel_align_labels[:int(num_rel_align_labels // 10 * (FLAGS.rel_seed-1))])
valid_rel = rel_align_labels[int(num_rel_align_labels // 10 * (FLAGS.rel_seed-1)): num_rel_align_labels // 10 * FLAGS.rel_seed]
test_rel = rel_align_labels[num_rel_align_labels // 10 * FLAGS.rel_seed:]
return adj, num_ent, train, test, valid, train_rel, test_rel, valid_rel