-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil_functions.py
275 lines (254 loc) · 10 KB
/
util_functions.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
import dgl
import numpy as np
import networkx as nx #导入networkx包
import matplotlib.pyplot as plt
import torch
import random
import os
import time
import torch.nn.functional as F
import dgl.function as fn
import multiprocessing as mp
from dgl.data.utils import save_graphs, load_graphs
from tqdm import tqdm
from torch.utils.data import Dataset
from collections import Counter
def kl_categorical(p_logit, q_logit):
p = F.softmax(p_logit, dim=-1)
_kl = torch.sum(p * (F.log_softmax(p_logit, dim=-1)
- F.log_softmax(q_logit, dim=-1)), 1)
return torch.mean(_kl)
def get_norm(edges):
indexs_select = torch.zeros_like(edges.data['rel_mod'])
indexs = (torch.arange(0, edges.data['rel_mod'].shape[0]).long(), edges.data["rel_type"].long().reshape(-1))
indexs_select.index_put_(indexs, torch.ones(edges.data['rel_type'].shape[0]))
node_norm = 1.0 / torch.sum(edges.dst['node_norm'] * indexs_select, -1)
return node_norm
def node_norm_to_edge_norm(g):
g = g.local_var()
g.apply_edges(lambda edges : {'norm' : get_norm(edges)})
return g.edata['norm']
def message_func(edges):
indexs = (torch.arange(0, edges.data['rel_mod'].shape[0]).long(), edges.data['rel_type'].long().reshape(-1))
edges.data['rel_mod'].index_put_(indexs, torch.ones(edges.data['rel_type'].shape[0]))
return {'msg': edges.data['rel_mod']}
def apply_func(nodes):
node_norm = nodes.data['o']
return {'node_norm': node_norm}
def build_all_graph(adj_train, num_user, class_values):
src = np.array(adj_train[0])
dst = np.array(adj_train[1]) + num_user
u = np.concatenate([src, dst])
v = np.concatenate([dst, src])
G = dgl.DGLGraph((u, v))
G.edata['rel_type'] = torch.tensor(np.concatenate([adj_train[2], adj_train[2]])).long()
'''
G.edata['rel_mod'] = torch.zeros(G.edata['rel_type'].shape[0], len(class_values))
G.update_all(message_func, fn.sum(msg='msg', out='o'), apply_func)
G.edata['norm'] = node_norm_to_edge_norm(G).reshape(-1,1)
'''
return G
class LocalGraphDataset(object):
def __init__(self, G, links, labels, class_values, num_user, dataset = None, parallel = False, pre_save = False, testing = False):
self.G = G
self.links = links
self.data_len = len(self.links[0])
self.labels = labels
self.num_user = num_user
self.testing = testing
self.count = 0
self.class_values = class_values
self.pre_save = pre_save
self.all_indexs = torch.arange(self.data_len)
self.parallel = parallel
self.dataset = dataset
if self.pre_save:
self.g_lists, self.labels = self.load_subgraphs(dataset)
def len(self):
return self.data_len
def get_graph_tool(self, indexs):
g_list = []
labels = []
index_out = []
for index in indexs:
g = self.extract_graph(self.G, self.links[0][index], self.links[1][index])
index_out.append([self.links[0][index], self.links[1][index]])
label = self.class_values[self.labels[index]]
g_list.append(g)
labels.append(label)
return g_list, torch.FloatTensor(labels)
def get_graph_tool_save(self, indexs):
g_list = []
labels = []
index_out = []
S = []
if not self.parallel:
pbar = tqdm(range(len(indexs)))
for index in pbar:
#g = self.extract_graph_new(self.G, self.links[0][index], self.links[1][index])
#print(g.edges(form='all', order='srcdst')[0].shape)
g = self.extract_graph(self.G, self.links[0][index], self.links[1][index])
#print(g.edges(form='all', order='srcdst')[0].shape)
#dd
index_out.append([self.links[0][index], self.links[1][index]])
#degree_l.append(g.number_of_edges())
label = self.class_values[self.labels[index]]
g_list.append(g)
labels.append(label)
return g_list, torch.FloatTensor(labels)
else:
pool = mp.Pool(mp.cpu_count())
results = pool.starmap_async(
self.extract_graph,
[
(self.G, self.links[0][index], self.links[1][index])
for index in indexs
]
)
remaining = results._number_left
pbar = tqdm(total=remaining)
while True:
pbar.update(remaining - results._number_left)
if results.ready(): break
remaining = results._number_left
time.sleep(1)
results = results.get()
pool.close()
pbar.close()
while results:
tmp = results.pop()
g_list.append(*tmp)
labels = self.class_values[self.labels[indexs]]
return g_list, torch.FloatTensor(labels)
def get_graphs(self, indexs):
if not self.pre_save:
g_list, labels = self.get_graph_tool(indexs)
else:
g_list, labels = self.g_lists[indexs], self.labels[indexs]
return dgl.batch(g_list), labels
def extract_graph_n(self, G, u_id, v_id):
subg = dgl.sampling.sample_neighbors(G, [u_id, v_id], 1)
subg.ndata['node_label'] = torch.zeros([subg.num_nodes(), 4])
pid = subg.ndata[dgl.NID]
for i in range(pid.shape[0]):
if pid[i] == u_id:
e_u = i
subg.ndata['node_label'][i, 0] = 1
elif pid[i] == v_id:
e_v = i
subg.ndata['node_label'][i, 1] = 1
elif pid[i] in u:
subg.ndata['node_label'][i, 2] = 1
elif pid[i] in v:
subg.ndata['node_label'][i, 3] = 1
if subg.has_edges_between(e_u, e_v):
e_ids = subg.edge_ids([e_u, e_v], [e_v, e_u])
subg.remove_edges(e_ids)
return subg
def save_subgraphs(self):
if self.testing:
file_path = "./data/" + self.dataset + "/test.bin"
if os.path.exists(file_path):
return
else:
file_path = "./data/" + self.dataset + "/train.bin"
if os.path.exists(file_path):
return
g_list, labels = self.get_graph_tool_save(self.all_indexs)
graph_labels = {"glabel": labels}
save_graphs(file_path, g_list, graph_labels)
def load_subgraphs(self):
if self.testing:
g_list, label_dict = load_graphs("./data/" + self.dataset + "/test/")
else:
g_list, label_dict = load_graphs("./data/" + self.dataset + "/train/")
return g_list, label_dict["glabel"]
def extract_graph_new(self, G, u_id, v_id):
v_id += self.num_user
static_u = torch.zeros(len(self.class_values))
static_v = torch.zeros(len(self.class_values))
start0 = time.time()
u_nodes, v, e_ids_1 = G.in_edges(v_id, "all")
u, v_nodes, e_ids_2 = G.out_edges(u_id, "all")
e_ids = []
nodes = torch.cat([u_nodes, v_nodes])
for i in range(u_nodes.shape[0]):
if u_nodes[i] == u_id:
e_ids.append(e_ids_1[i])
for i in range(v_nodes.shape[0]):
if v_nodes[i] == v_id:
e_ids.append(e_ids_2[i])
#start1 = time.time()
#print(start1 - start0)
subg = dgl.node_subgraph(G, nodes)
#start2 = time.time()
#print(start2 - start1)
subg.ndata['node_label'] = torch.zeros([subg.num_nodes(), 4])
pid = subg.ndata[dgl.NID]
#start3 = time.time()
#print(start3 - start2)
for i in range(pid.shape[0]):
if pid[i] == u_id:
e_u = i
subg.ndata['node_label'][i, 0] = 1
elif pid[i] == v_id:
e_v = i
subg.ndata['node_label'][i, 1] = 1
elif pid[i] in u:
subg.ndata['node_label'][i, 2] = 1
elif pid[i] in v:
subg.ndata['node_label'][i, 3] = 1
subg = dgl.remove_edges(subg, e_ids)
start6 = time.time()
print(start6 - start0)
print()
return subg
def extract_graph(self, G, u_id, v_id):
v_id += self.num_user
static_u = torch.zeros(len(self.class_values))
static_v = torch.zeros(len(self.class_values))
#start0 = time.time()
u_nodes, v, e_ids = G.out_edges(u_id, "all")
u, v_nodes, e_ids = G.in_edges(v_id, "all")
nodes = torch.cat([u, v])
if self.testing:
nodes = torch.cat([nodes, torch.tensor([u_id, v_id])])
#start1 = time.time()
#print(start1 - start0)
subg = G.subgraph(nodes)
#start2 = time.time()
#print(start2 - start1)
subg.ndata['node_label'] = torch.zeros([nodes.shape[0], 4])
pid = subg.ndata[dgl.NID]
#start3 = time.time()
#print(start3 - start2)
for i in range(pid.shape[0]):
if pid[i] == u_id:
e_u = i
subg.ndata['node_label'][i, 0] = 1
elif pid[i] == v_id:
e_v = i
subg.ndata['node_label'][i, 1] = 1
elif pid[i] in u:
subg.ndata['node_label'][i, 2] = 1
elif pid[i] in v:
subg.ndata['node_label'][i, 3] = 1
#start4 = time.time()
#print(start4 - start3)
if not self.testing:
e_ids = subg.edge_ids([e_u, e_v], [e_v, e_u])
#start5 = time.time()
#print(start5 - start4)
subg = dgl.remove_edges(subg, e_ids)
#start6 = time.time()
#print(start6 - start0)
#print()
return subg
if __name__=='__main__':
adj_train = ([0, 1, 1, 1], [3, 0, 1, 2], [7, 1, 1, 9])
num_user = 2
class_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
G = build_all_graph(adj_train, num_user, class_values)
print(G.edata['rel_type'])
print(G.edges(form='all', order='srcdst'))
print(G.edata['norm'])