-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICD_M_L_f_0.6_demo.py
386 lines (364 loc) · 16.4 KB
/
ICD_M_L_f_0.6_demo.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Demonstration of ICD-M on L(f,0.6)
import torch.optim as optim
from utils import *
from modules.models import *
from modules.feat_ext import *
from modules.loss import *
import time
import numpy as np
from sklearn.cluster import KMeans
import random
#torch.cuda.set_device(0)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
#setup_seed(0)
# ====================
data_name = 'L(f,0.6)'
num_graphs = 2000 # Number of graph snapshots
feat_dim = 4096 # Dimensionality of reduced feature input
# ==========
# Layer configurations
gen_dims = [feat_dim, 2048, 512, 256] # Layer configuration of generator
hid_dim = gen_dims[-1] # Dimensionality of graph embedding
disc_dims = [hid_dim, 128, 64, 16, 1] # Layer configuration of discriminator
# ==========
# Hyper-parameter settings
alpha = 1
beta = 3
m = 1
p = 1000 # Number of training samples in each epoch
gen_lr = 1e-4 # Learning rate of generator
disc_lr = 1e-4 # Learning rate of discriminator
feat_norm_flag = True # Flag for whether to normalize the neighbor-induced feature
# =========
save_flag = True # Flag to save the check points (=True) w.r.t. each epoch
test_eva_flag = True # Flag for evaluation on test set (=True) for each epoch
# ====================
train_rate = 0.80 # Ratio of snapshots in the training set
val_rate = 0.10 # Ratio of snapshots in the validation set
num_train = int(num_graphs*train_rate) # Number of snapshots in the training set
num_val = int(num_graphs*val_rate) # Number of snapshots in the validation set
num_test = num_graphs-(num_train+num_val) # Number of snapshots in the test set
# ==========
dropout_rate = 0.0 # Dropout rate
# ===================
# Read the dataset
edge_list = np.load('data/%s_edge_list.npy' % (data_name), allow_pickle=True)
gnd_list = np.load('data/%s_gnd_list.npy' % (data_name), allow_pickle=True)
# ==========
edge_train_list = edge_list[0: num_train] # Edge list of the training set
edge_val_list = edge_list[num_train: num_train+num_val] # Edge list of the validation set
edge_test_list = edge_list[num_train+num_val: ] # Edge list of the test set
# ==========
gnd_train_list = gnd_list[0: num_train] # Ground-truth list of the training set
gnd_val_list = gnd_list[num_train: num_train+num_val] # Ground-truth list of the validation set
gnd_test_list = gnd_list[num_train+num_val: ] # Ground-truth list of the test set
# ====================
# Define the models
gen_net = GenNet(gen_dims, dropout_rate, device) # Generator
disc_net = DiscNet(disc_dims, dropout_rate, device) # Discriminator
# ==========
# Define the optimizers
gen_opt = optim.RMSprop(gen_net.parameters(), lr=gen_lr, weight_decay=1e-5)
disc_opt = optim.RMSprop(disc_net.parameters(), lr=disc_lr, weight_decay=1e-5)
#gen_opt = optim.Adam(gen_net.parameters(), lr=gen_lr, weight_decay=1e-5)
#disc_opt = optim.Adam(disc_net.parameters(), lr=disc_lr, weight_decay=1e-5)
# ====================
num_train_epochs = 100 # Number of training epochs
for epoch in range(num_train_epochs):
# ====================
# Train the model
gen_net.train()
disc_net.train()
# ==========
# Lists to record the loss functions
gen_loss_list = []
disc_loss_list = []
# ===========
for q in range(p): # Randomly select p snapshots from the training set to optimize the model
s = random.randint(0, num_train-1) # Randomly select a node index
# ====================
gnd = gnd_train_list[s] # Ground-truth
num_nodes = len(gnd) # Number of nodes
edges = edge_train_list[s] # Edge list
adj = get_adj(edges, num_nodes) # Adjacency Matrix
if int(min(gnd)) == 0: # Number of clusters
num_clus = int(max(gnd))+1
else:
num_clus = int(max(gnd))
# ==========
# Construct the partitioning membership indicator
label_onehot = np.zeros((num_nodes, num_clus)) # One-hot representation of the ground-truth
for i in range(num_nodes):
if min(gnd) == 0:
label_idx = int(gnd[i])
else:
label_idx = int(gnd[i])-1
label_onehot[i, label_idx] = 1
# ====================
sup_topo = sp.sparse.coo_matrix(get_gnn_sup(adj)) # Sparse GNN support of the original graph
sup_topo_sp = sparse_to_tuple(sup_topo)
idxs = torch.LongTensor(sup_topo_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_topo_sp[1]).to(device)
sup_topo_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_topo_sp[2]).float().to(device)
# ==========
adj_gnd = np.matmul(label_onehot, np.transpose(label_onehot)) # Adjacency matrix of the auxiliary label-induced graph
sup_gnd = sp.sparse.coo_matrix(get_gnn_sup(adj_gnd)) # Sparse GNN support of the auxiliary label-induced graph
sup_gnd_sp = sparse_to_tuple(sup_gnd)
idxs = torch.LongTensor(sup_gnd_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_gnd_sp[1]).to(device)
sup_gnd_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_gnd_sp[2]).float().to(device)
# ====================
adj_tnr = torch.FloatTensor(adj).to(device)
gnd_tnr = torch.FloatTensor(label_onehot).to(device) # Tensor of the partitioning membership indicator
# ====================
# Extract the neighbor-induced feature (i.e., modularity matrix)
mod_tnr = get_mod_GPU(adj_tnr, num_nodes)
if torch.cuda.is_available():
mod = mod_tnr.cpu().data.numpy()
else:
mod = mod_tnr.data.numpy()
if feat_norm_flag:
mod_ = m_norm(mod) # Get the normalized modularity matrix (w/ value range [-1, 1])
else:
mod_ = mod
feat_norm_tnr = torch.FloatTensor(mod_).to(device) # Tensor of the 'normalized' neighbor-induced feature
# ==========
# Derive the reduced feature input
edge_map = rand_edge_map(edges, mod)
#edge_map = rand_edge_map(edges, max_min_norm(mod))
feat_coar_tnr = feat_coar_gpu(edge_map, mod_tnr, num_nodes, feat_dim, device) # Tensor of the reduced features
# ====================
if epoch==0: # No parameter update when epoch==0
feat_rec, emb, emb_gnd = gen_net(sup_topo_tnr, sup_gnd_tnr, feat_coar_tnr, train_flag=True)
disc_fake = disc_net(emb)
disc_real = disc_net(emb_gnd)
gen_loss = get_gen_loss(disc_fake, feat_norm_tnr, feat_rec, gnd_tnr, alpha, beta) # Loss of generator
disc_loss = get_disc_loss(disc_fake, disc_real) # Loss of discriminator
else: # Update model parameters for epoch>0
# ==========
for _ in range(m):
# ==========
# Train discriminator
_, emb, emb_gnd = gen_net(sup_topo_tnr, sup_gnd_tnr, feat_coar_tnr, train_flag=True)
disc_fake = disc_net(emb)
disc_real = disc_net(emb_gnd)
disc_loss = get_disc_loss(disc_fake, disc_real) # Loss of discriminator
disc_opt.zero_grad()
disc_loss.backward() # Update parameters of discriminator
disc_opt.step()
# ==========
# Train generator
feat_rec, emb, _ = gen_net(sup_topo_tnr, sup_gnd_tnr, feat_coar_tnr, train_flag=False)
disc_fake = disc_net(emb)
gen_loss = get_gen_loss(disc_fake, feat_norm_tnr, feat_rec, gnd_tnr, alpha, beta) # Loss of generator
gen_opt.zero_grad()
gen_loss.backward() # Update parameters of generator
gen_opt.step()
# ==========
gen_loss_list.append(gen_loss.item())
disc_loss_list.append(disc_loss.item())
if q%100 == 0:
print('Train Epoch %d Sample %d / %d' % (epoch, q, p))
# ====================
gen_loss_sum = np.sum(gen_loss_list)
disc_loss_sum = np.sum(disc_loss_list)
print('#%d Train G-Loss %f D-Loss %f' % (epoch, gen_loss_sum, disc_loss_sum))
# ==========
if save_flag: # If save_flag=True, save the trained model w.r.t. the current epoch
torch.save(gen_net, 'chpt/ICD-M_gen_%s_%d.pkl' % (data_name, epoch))
torch.save(disc_net, 'chpt/ICD-M_disc_%s_%d.pkl' % (data_name, epoch))
# ====================
# Validate the model
# ==========
gen_net.eval()
disc_net.eval()
# ==========
# Lists to record the quality metrics and runtime w.r.t. each validation snapshot
NMI_list = []
time_list = []
for s in range(num_val):
# ==========
gnd = gnd_val_list[s] # Ground-truth
num_nodes = len(gnd) # Number of nodes
edges = edge_val_list[s] # Edge list
adj = get_adj(edges, num_nodes) # Adjacency matrix
adj_tnr = torch.FloatTensor(adj).to(device)
if min(gnd) == 0: # Number of clusters
num_clus = int(max(gnd))+1
else:
num_clus = int(max(gnd))
# ===========
# Extract the node label sequence from ground-truth (for evaluation)
if min(gnd)==0:
labels_ = np.array(gnd)
else:
labels_ = np.array(gnd-1)
# ====================
sup_topo = sp.sparse.coo_matrix(get_gnn_sup(adj)) # Sparse GNN support of the original graph
sup_topo_sp = sparse_to_tuple(sup_topo)
idxs = torch.LongTensor(sup_topo_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_topo_sp[1]).to(device)
sup_topo_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_topo_sp[2]).float().to(device)
# ==========
# Extract the neighbor-induced feature (i.e., modularity matrix)
time_start = time.time()
mod_tnr = get_mod_GPU(adj_tnr, num_nodes)
if torch.cuda.is_available():
mod = mod_tnr.cpu().data.numpy()
else:
mod = mod_tnr.data.numpy()
# ==========
# Derive the reduced feature input
edge_map = rand_edge_map(edges, mod)
#edge_map = rand_edge_map(edges, max_min_norm(mod))
feat_coar_tnr = feat_coar_gpu(edge_map, mod_tnr, num_nodes, feat_dim, device) # Tensor of the reduced features
# ==========
time_end = time.time()
feat_time = time_end-time_start # Runtime of feature extraction
# =====================
# Derive the (inductive) graph embedding
time_start = time.time()
_, emb, _ = gen_net(sup_topo_tnr, sup_topo_tnr, feat_coar_tnr, train_flag=False)
time_end = time.time()
prop_time = time_end-time_start # Runtime of feedforward propagation
if torch.cuda.is_available():
emb = emb.cpu().data.numpy()
else:
emb = emb.data.numpy()
# ====================
# Applied downstream kmeans algorithm to the graph embedding
time_start = time.time()
kmeans = KMeans(n_clusters=num_clus, n_init=10).fit(emb.astype(np.float64))
clus_res = kmeans.labels_
time_end = time.time()
clus_time = time_end-time_start # Runtime of downstream clustering
runtime = feat_time+prop_time+clus_time # Total runtime
# =====================
# Compute the quality metric of current partitioning result
NMI = get_NMI(clus_res, labels_)
NMI_list.append(NMI)
time_list.append(runtime)
# ====================
NMI_mean = np.mean(NMI_list)
NMI_std = np.std(NMI_list)
time_mean = np.mean(time_list)
time_std = np.std(time_list)
print('#%d Val NMI %f (%f) Time %f (%f)'
% (epoch, NMI_mean, NMI_std, time_mean, time_std))
# ====================
f_output = open('res/ICD-M_%s_val.txt' % (data_name), 'a+')
f_output.write('#%d NMI %.8f %.8f Time %.8f %.8f\n'
% (epoch, NMI_mean, NMI_std, time_mean, time_std))
f_output.close()
# ====================
# Test the model
# ==========
if not test_eva_flag: # If test_eva_flag=True, conduct evaluation on test set for current epoch
print()
continue
gen_net.eval()
disc_net.eval()
# ==========
# Lists to record the quality metrics and runtime w.r.t. each test snapshot
NMI_list = []
AC_list = []
mod_list = []
ncut_list = []
time_list = []
for s in range(num_test):
# ====================
gnd = gnd_test_list[s] # Ground-truth
num_nodes = len(gnd) # Number of nodes
edges = edge_test_list[s] # Edge list
adj = get_adj(edges, num_nodes) # Adjacency matrix
adj_tnr = torch.FloatTensor(adj).to(device)
if min(gnd) == 0: # Number of clusters
num_clus = int(max(gnd))+1
else:
num_clus = int(max(gnd))
# ===========
# Extract the node label sequence from ground-truth (for evaluation)
if min(gnd)==0:
labels_ = np.array(gnd)
else:
labels_ = np.array(gnd-1)
# ====================
sup_topo = sp.sparse.coo_matrix(get_gnn_sup(adj)) # Normalized sparse adjacency matrix of the original graph
sup_topo_sp = sparse_to_tuple(sup_topo)
idxs = torch.LongTensor(sup_topo_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_topo_sp[1]).to(device)
sup_topo_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_topo_sp[2]).float().to(device)
# ==========
# Extract the neighbor-induced feature (i.e., modularity matrix)
time_start = time.time()
mod_tnr = get_mod_GPU(adj_tnr, num_nodes)
if torch.cuda.is_available():
mod = mod_tnr.cpu().data.numpy()
else:
mod = mod_tnr.data.numpy()
# ==========
# Derive the reduced feature input
edge_map = rand_edge_map(edges, mod)
#edge_map = rand_edge_map(edges, max_min_norm(mod))
feat_coar_tnr = feat_coar_gpu(edge_map, mod_tnr, num_nodes, feat_dim, device) # Tensor of the reduced features
# ==========
time_end = time.time()
feat_time = time_end-time_start # Runtime of feature extraction
# ====================
# Derive the (inductive) graph embedding
time_start = time.time()
_, emb, _ = gen_net(sup_topo_tnr, sup_topo_tnr, feat_coar_tnr, train_flag=False)
time_end = time.time()
prop_time = time_end-time_start # Runtime of one feedforward propagation
if torch.cuda.is_available():
emb = emb.cpu().data.numpy()
else:
emb = emb.data.numpy()
# ====================
# Applied downstream kmeans algorithm to the graph embedding
time_start = time.time()
kmeans = KMeans(n_clusters=num_clus, n_init=10).fit(emb.astype(np.float64))
clus_res = kmeans.labels_
time_end = time.time()
clus_time = time_end-time_start # Runtime of downstream clustering
runtime = feat_time+prop_time+clus_time # Total runtime
# ====================
# Compute the quality metric of current partitioning result
NMI = get_NMI(clus_res, labels_)
AC = get_AC(labels_, clus_res)
# ==========
#mod_metric = get_mod_metric(adj, clus_res, num_clus)
#ncut_metric = get_NCut_metric(adj, clus_res, num_clus)
mod_metric = get_mod_metric_gpu(adj_tnr, clus_res, num_clus, device)
ncut_metric = get_NCut_metric_gpu(adj_tnr, clus_res, num_clus, device)
# ==========
NMI_list.append(NMI)
AC_list.append(AC)
mod_list.append(mod_metric)
ncut_list.append(ncut_metric)
time_list.append(runtime)
# ====================
NMI_mean = np.mean(NMI_list)
NMI_std = np.std(NMI_list)
AC_mean = np.mean(AC_list)
AC_std = np.std(AC_list)
mod_mean = np.mean(mod_list)
mod_std = np.std(mod_list)
ncut_mean = np.mean(ncut_list)
ncut_std = np.std(ncut_list)
time_mean = np.mean(time_list)
time_std = np.std(time_list)
print('#%d Test NMI %f (%f) AC %f (%f) Mod %f (%f) NCut %f (%f) Time %f (%f)'
% (epoch, NMI_mean, NMI_std, AC_mean, AC_std, mod_mean, mod_std, ncut_mean, ncut_std, time_mean, time_std))
# ====================
f_output = open('res/ICD-M_%s_test.txt' % (data_name), 'a+')
f_output.write('#%d NMI %.8f %.8f AC %.8f %.8f Mod %.8f %.8f NCut %.8f %.8f Time %.8f %.8f\n'
% (epoch, NMI_mean, NMI_std, AC_mean, AC_std, mod_mean, mod_std, ncut_mean, ncut_std, time_mean, time_std))
f_output.close()
print()