/
utils.py
237 lines (200 loc) · 6.85 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
import numpy as np
import pickle as pkl
import networkx as nx
import scipy.sparse as sp
from scipy.sparse.linalg.eigen.arpack import eigsh
import sys
import tensorflow as tf
import math
flags = tf.app.flags
FLAGS = flags.FLAGS
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
def normalize_adj(adj):
"""Symmetrically normalize adjacency matrix."""
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
def preprocess_adj(adj):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
adj_normalized = normalize_adj(adj + sp.eye(adj.shape[0]))
return sparse_to_tuple(adj_normalized)
def construct_feed_dict(features, support, placeholders):
"""Construct feed dictionary for GCN-Align."""
feed_dict = dict()
feed_dict.update({placeholders['features']: features})
feed_dict.update({placeholders['support'][i]: support[i] for i in range(len(support))})
return feed_dict
def chebyshev_polynomials(adj, k):
"""Calculate Chebyshev polynomials up to order k. Return a list of sparse matrices (tuple representation)."""
print("Calculating Chebyshev polynomials up to order {}...".format(k))
adj_normalized = normalize_adj(adj)
laplacian = sp.eye(adj.shape[0]) - adj_normalized
largest_eigval, _ = eigsh(laplacian, 1, which='LM')
scaled_laplacian = (2. / largest_eigval[0]) * laplacian - sp.eye(adj.shape[0])
t_k = list()
t_k.append(sp.eye(adj.shape[0]))
t_k.append(scaled_laplacian)
def chebyshev_recurrence(t_k_minus_one, t_k_minus_two, scaled_lap):
s_lap = sp.csr_matrix(scaled_lap, copy=True)
return 2 * s_lap.dot(t_k_minus_one) - t_k_minus_two
for i in range(2, k+1):
t_k.append(chebyshev_recurrence(t_k[-1], t_k[-2], scaled_laplacian))
return sparse_to_tuple(t_k)
def loadfile(fn, num=1):
"""Load a file and return a list of tuple containing $num integers in each line."""
print('loading a file...' + fn)
ret = []
with open(fn, encoding='utf-8') as f:
for line in f:
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(fns):
ent2id = {}
for fn in fns:
with open(fn, 'r', encoding='utf-8') as f:
for line in f:
th = line[:-1].split('\t')
ent2id[th[1]] = int(th[0])
return ent2id
def loadattr(fns, e, ent2id):
"""The most frequent attributes are selected to save space."""
cnt = {}
for fn in fns:
with open(fn, 'r', encoding='utf-8') as f:
for line in f:
th = line[:-1].split('\t')
if th[0] not in ent2id:
continue
for i in range(1, len(th)):
if th[i] not in cnt:
cnt[th[i]] = 1
else:
cnt[th[i]] += 1
fre = [(k, cnt[k]) for k in sorted(cnt, key=cnt.get, reverse=True)]
num_features = min(len(fre), 2000)
attr2id = {}
for i in range(num_features):
attr2id[fre[i][0]] = i
M = {}
for fn in fns:
with open(fn, 'r', encoding='utf-8') as f:
for line in f:
th = line[:-1].split('\t')
if th[0] in ent2id:
for i in range(1, len(th)):
if th[i] in attr2id:
M[(ent2id[th[0]], attr2id[th[i]])] = 1.0
row = []
col = []
data = []
for key in M:
row.append(key[0])
col.append(key[1])
data.append(M[key])
return sp.coo_matrix((data, (row, col)), shape=(e, num_features)) # attr
def get_dic_list(e, KG):
M = {}
for tri in KG:
if tri[0] == tri[2]:
continue
M[(tri[0], tri[2])] = 1
M[(tri[2], tri[0])] = 1
dic_list = {}
for i in range(e):
dic_list[i] = []
for pair in M:
dic_list[pair[0]].append(pair[1])
return dic_list
def func(KG):
head = {}
cnt = {}
for tri in KG:
if tri[1] not in cnt:
cnt[tri[1]] = 1
head[tri[1]] = set([tri[0]])
else:
cnt[tri[1]] += 1
head[tri[1]].add(tri[0])
r2f = {}
for r in cnt:
r2f[r] = len(head[r]) / cnt[r]
return r2f
def ifunc(KG):
tail = {}
cnt = {}
for tri in KG:
if tri[1] not in cnt:
cnt[tri[1]] = 1
tail[tri[1]] = set([tri[2]])
else:
cnt[tri[1]] += 1
tail[tri[1]].add(tri[2])
r2if = {}
for r in cnt:
r2if[r] = len(tail[r]) / cnt[r]
return r2if
def get_weighted_adj(e, KG):
r2f = func(KG)
r2if = ifunc(KG)
M = {}
for tri in KG:
if tri[0] == tri[2]:
continue
if (tri[0], tri[2]) not in M:
M[(tri[0], tri[2])] = max(r2if[tri[1]], 0.3)
else:
M[(tri[0], tri[2])] += max(r2if[tri[1]], 0.3)
if (tri[2], tri[0]) not in M:
M[(tri[2], tri[0])] = max(r2f[tri[1]], 0.3)
else:
M[(tri[2], tri[0])] += max(r2f[tri[1]], 0.3)
row = []
col = []
data = []
for key in M:
row.append(key[1])
col.append(key[0])
data.append(M[key])
return sp.coo_matrix((data, (row, col)), shape=(e, e))
def get_ae_input(attr):
return sparse_to_tuple(sp.coo_matrix(attr))
def load_data(dataset_str):
names = [['ent_ids_1', 'ent_ids_2'], ['training_attrs_1', 'training_attrs_2'], ['triples_1', 'triples_2'], ['ref_ent_ids']]
for fns in names:
for i in range(len(fns)):
fns[i] = 'data/'+dataset_str+'/'+fns[i]
Es, As, Ts, ill = names
ill = ill[0]
e = len(set(loadfile(Es[0], 1)) | set(loadfile(Es[1], 1)))
ILL = loadfile(ill, 2)
illL = len(ILL)
np.random.shuffle(ILL)
train = np.array(ILL[:illL // 10 * FLAGS.seed])
test = ILL[illL // 10 * FLAGS.seed:]
KG = loadfile(Ts[0], 3) + loadfile(Ts[1], 3)
ent2id = get_ent2id([Es[0], Es[1]])
attr = loadattr([As[0], As[1]], e, ent2id)
ae_input = get_ae_input(attr)
adj = get_weighted_adj(e, KG) # nx.adjacency_matrix(nx.from_dict_of_lists(get_dic_list(e, KG)))
return adj, ae_input, train, test