-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-gnn.py
482 lines (358 loc) · 14.3 KB
/
test-gnn.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#%% Imports
import numpy as np
from scipy.linalg import sqrtm
from scipy.special import softmax
import networkx as nx
from networkx.algorithms.community.modularity_max import greedy_modularity_communities
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
def draw_kkl(nx_G, label_map, node_color, pos=None, **kwargs):
fig, ax = plt.subplots(figsize=(10,10))
if pos is None:
pos = nx.spring_layout(nx_G, k=5/np.sqrt(nx_G.number_of_nodes()))
nx.draw(
nx_G, pos, with_labels=label_map is not None,
labels=label_map,
node_color=node_color,
ax=ax, **kwargs)
#%% Data
g = nx.karate_club_graph()
#(g.number_of_nodes(), g.number_of_edges())
communities = greedy_modularity_communities(g)
colors = np.zeros(g.number_of_nodes())
for i, com in enumerate(communities):
colors[list(com)] = i
n_classes = np.unique(colors).shape[0]
labels = np.eye(n_classes)[colors.astype(int)]
club_labels = nx.get_node_attributes(g,'club')
#_ = draw_kkl(g, None, colors, cmap='gist_rainbow', edge_color='gray')
fig, ax = plt.subplots(figsize=(10,10))
pos = nx.spring_layout(g, k=5/np.sqrt(g.number_of_nodes()))
kwargs = {"cmap": 'gist_rainbow', "edge_color":'gray'}
nx.draw(
g, pos, with_labels=False,
node_color=colors,
ax=ax, **kwargs)
#plt.savefig('karate_club_graph.png', bbox_inches='tight', transparent=True)
#%% Model
A = nx.to_numpy_matrix(g)
A_mod = A + np.eye(g.number_of_nodes()) # add self-connections
D_mod = np.zeros_like(A_mod)
np.fill_diagonal(D_mod, np.asarray(A_mod.sum(axis=1)).flatten())
D_mod_invroot = np.linalg.inv(sqrtm(D_mod))
A_hat = D_mod_invroot @ A_mod @ D_mod_invroot
X = np.eye(g.number_of_nodes())
def glorot_init(nin, nout):
sd = np.sqrt(6.0 / (nin + nout))
return np.random.uniform(-sd, sd, size=(nin, nout))
def xent(pred, labels):
return -np.log(pred)[np.arange(pred.shape[0]), np.argmax(labels, axis=1)]
def norm_diff(dW, dW_approx):
return np.linalg.norm(dW - dW_approx) / (np.linalg.norm(dW) + np.linalg.norm(dW_approx))
class GradDescentOptim():
def __init__(self, lr, wd):
self.lr = lr
self.wd = wd
self._y_pred = None
self._y_true = None
self._out = None
self.bs = None
self.train_nodes = None
def __call__(self, y_pred, y_true, train_nodes=None):
self.y_pred = y_pred
self.y_true = y_true
if train_nodes is None:
self.train_nodes = np.arange(y_pred.shape[0])
else:
self.train_nodes = train_nodes
self.bs = self.train_nodes.shape[0]
@property
def out(self):
return self._out
@out.setter
def out(self, y):
self._out = y
class GCNLayer():
def __init__(self, n_inputs, n_outputs, activation=None, name=''):
self.n_inputs = n_inputs
self.n_outputs = n_outputs
self.W = glorot_init(self.n_outputs, self.n_inputs)
self.activation = activation
self.name = name
def __repr__(self):
return f"GCN: W{'_'+self.name if self.name else ''} ({self.n_inputs}, {self.n_outputs})"
def forward(self, A, X, W=None):
"""
Assumes A is (bs, bs) adjacency matrix and X is (bs, D),
where bs = "batch size" and D = input feature length
"""
self._A = A
self._X = (A @ X).T # for calculating gradients. (D, bs)
if W is None:
W = self.W
H = W @ self._X # (h, D)*(D, bs) -> (h, bs)
if self.activation is not None:
H = self.activation(H)
self._H = H # (h, bs)
return self._H.T # (bs, h)
def backward(self, optim, update=True):
dtanh = 1 - np.asarray(self._H.T)**2 # (bs, out_dim)
d2 = np.multiply(optim.out, dtanh) # (bs, out_dim) *element_wise* (bs, out_dim)
self.grad = self._A @ d2 @ self.W # (bs, bs)*(bs, out_dim)*(out_dim, in_dim) = (bs, in_dim)
optim.out = self.grad
dW = np.asarray(d2.T @ self._X.T) / optim.bs # (out_dim, bs)*(bs, D) -> (out_dim, D)
dW_wd = self.W * optim.wd / optim.bs # weight decay update
if update:
self.W -= (dW + dW_wd) * optim.lr
return dW + dW_wd
class SoftmaxLayer():
def __init__(self, n_inputs, n_outputs, name=''):
self.n_inputs = n_inputs
self.n_outputs = n_outputs
self.W = glorot_init(self.n_outputs, self.n_inputs)
self.b = np.zeros((self.n_outputs, 1))
self.name = name
self._X = None # Used to calculate gradients
def __repr__(self):
return f"Softmax: W{'_'+self.name if self.name else ''} ({self.n_inputs}, {self.n_outputs})"
def shift(self, proj):
shiftx = proj - np.max(proj, axis=0, keepdims=True)
exps = np.exp(shiftx)
return exps / np.sum(exps, axis=0, keepdims=True)
def forward(self, X, W=None, b=None):
"""Compute the softmax of vector x in a numerically stable way.
X is assumed to be (bs, h)
"""
self._X = X.T
if W is None:
W = self.W
if b is None:
b = self.b
proj = np.asarray(W @ self._X) + b # (out, h)*(h, bs) = (out, bs)
return self.shift(proj).T # (bs, out)
def backward(self, optim, update=True):
# should take in optimizer, update its own parameters and update the optimizer's "out"
# Build mask on loss
train_mask = np.zeros(optim.y_pred.shape[0])
train_mask[optim.train_nodes] = 1
train_mask = train_mask.reshape((-1, 1))
# derivative of loss w.r.t. activation (pre-softmax)
d1 = np.asarray((optim.y_pred - optim.y_true)) # (bs, out_dim)
d1 = np.multiply(d1, train_mask) # (bs, out_dim) with loss of non-train nodes set to zero
self.grad = d1 @ self.W # (bs, out_dim)*(out_dim, in_dim) = (bs, in_dim)
optim.out = self.grad
dW = (d1.T @ self._X.T) / optim.bs # (out_dim, bs)*(bs, in_dim) -> (out_dim, in_dim)
db = d1.T.sum(axis=1, keepdims=True) / optim.bs # (out_dim, 1)
dW_wd = self.W * optim.wd / optim.bs # weight decay update
if update:
self.W -= (dW + dW_wd) * optim.lr
self.b -= db.reshape(self.b.shape) * optim.lr
return dW + dW_wd, db.reshape(self.b.shape)
gcn1 = GCNLayer(g.number_of_nodes(), 2, activation=np.tanh, name='1')
sm1 = SoftmaxLayer(2, n_classes, "SM")
opt = GradDescentOptim(lr=0, wd=1.)
gcn1_out = gcn1.forward(A_hat, X)
opt(sm1.forward(gcn1_out), labels)
def get_grads(inputs, layer, argname, labels, eps=1e-4, wd=0):
cp = getattr(layer, argname).copy()
cp_flat = np.asarray(cp).flatten()
grads = np.zeros_like(cp_flat)
n_parms = cp_flat.shape[0]
for i, theta in enumerate(cp_flat):
#print(f"Parm {argname}_{i}")
theta_cp = theta
# J(theta + eps)
cp_flat[i] = theta + eps
cp_tmp = cp_flat.reshape(cp.shape)
predp = layer.forward(*inputs, **{argname: cp_tmp})
wd_term = wd/2*(cp_flat**2).sum() / labels.shape[0]
#print(wd_term)
Jp = xent(predp, labels).mean() + wd_term
# J(theta - eps)
cp_flat[i] = theta - eps
cp_tmp = cp_flat.reshape(cp.shape)
predm = layer.forward(*inputs, **{argname: cp_tmp})
wd_term = wd/2*(cp_flat**2).sum() / labels.shape[0]
#print(wd_term)
Jm = xent(predm, labels).mean() + wd_term
# grad
grads[i] = ((Jp - Jm) / (2*eps))
# Back to normal
cp_flat[i] = theta
return grads.reshape(cp.shape)
dW_approx = get_grads((gcn1_out,), sm1, "W", labels, eps=1e-4, wd=opt.wd)
db_approx = get_grads((gcn1_out,), sm1, "b", labels, eps=1e-4, wd=opt.wd)
dW, db = sm1.backward(opt, update=False)
assert norm_diff(dW, dW_approx) < 1e-7
assert norm_diff(db, db_approx) < 1e-7
def get_gcn_grads(inputs, gcn, sm_layer, labels, eps=1e-4, wd=0):
cp = gcn.W.copy()
cp_flat = np.asarray(cp).flatten()
grads = np.zeros_like(cp_flat)
n_parms = cp_flat.shape[0]
for i, theta in enumerate(cp_flat):
theta_cp = theta
# J(theta + eps)
cp_flat[i] = theta + eps
cp_tmp = cp_flat.reshape(cp.shape)
pred = sm_layer.forward(gcn.forward(*inputs, W=cp_tmp))
w2 = (cp_flat**2).sum()+(sm_layer.W.flatten()**2).sum()
Jp = xent(pred, labels).mean() + wd/(2*labels.shape[0])*w2
# J(theta - eps)
cp_flat[i] = theta - eps
cp_tmp = cp_flat.reshape(cp.shape)
pred = sm_layer.forward(gcn.forward(*inputs, W=cp_tmp))
w2 = (cp_flat**2).sum()+(sm_layer.W.flatten()**2).sum()
Jm = xent(pred, labels).mean() + wd/(2*labels.shape[0])*w2
# grad
grads[i] = ((Jp - Jm) / (2*eps))
# Back to normal
cp_flat[i] = theta
return grads.reshape(cp.shape)
dW2 = gcn1.backward(opt, update=False)
dW2_approx = get_gcn_grads((A_hat, X), gcn1, sm1, labels, eps=1e-4, wd=opt.wd)
assert norm_diff(dW2, dW2_approx) < 1e-7
def get_gcn_input_grads(A_hat, X, gcn, sm_layer, labels, eps=1e-4):
cp = X.copy()
cp_flat = np.asarray(cp).flatten()
grads = np.zeros_like(cp_flat)
n_parms = cp_flat.shape[0]
for i, x in enumerate(cp_flat):
x_cp = x
# J(theta + eps)
cp_flat[i] = x + eps
cp_tmp = cp_flat.reshape(cp.shape)
pred = sm_layer.forward(gcn.forward(A_hat, cp_tmp))
Jp = xent(pred, labels).mean()
# J(theta - eps)
cp_flat[i] = x - eps
cp_tmp = cp_flat.reshape(cp.shape)
pred = sm_layer.forward(gcn.forward(A_hat, cp_tmp))
Jm = xent(pred, labels).mean()
# grad
grads[i] = ((Jp - Jm) / (2*eps))
# Back to normal
cp_flat[i] = x
return grads.reshape(cp.shape)
dX_approx = get_gcn_input_grads(A_hat, X, gcn1, sm1, labels, eps=1e-4)
assert norm_diff(gcn1.grad/A_hat.shape[0], dX_approx) < 1e-7
class GCNNetwork():
def __init__(self, n_inputs, n_outputs, n_layers, hidden_sizes, activation, seed=0):
self.n_inputs = n_inputs
self.n_outputs = n_outputs
self.n_layers = n_layers
self.hidden_sizes = hidden_sizes
self.activation = activation
np.random.seed(seed)
self.layers = list()
# Input layer
gcn_in = GCNLayer(n_inputs, hidden_sizes[0], activation, name='in')
self.layers.append(gcn_in)
# Hidden layers
for layer in range(n_layers):
gcn = GCNLayer(self.layers[-1].W.shape[0], hidden_sizes[layer], activation, name=f'h{layer}')
self.layers.append(gcn)
# Output layer
sm_out = SoftmaxLayer(hidden_sizes[-1], n_outputs, name='sm')
self.layers.append(sm_out)
def __repr__(self):
return '\n'.join([str(l) for l in self.layers])
def embedding(self, A, X):
# Loop through all GCN layers
H = X
for layer in self.layers[:-1]:
H = layer.forward(A, H)
return np.asarray(H)
def forward(self, A, X):
# GCN layers
H = self.embedding(A, X)
# Softmax
p = self.layers[-1].forward(H)
return np.asarray(p)
gcn_model = GCNNetwork(
n_inputs=g.number_of_nodes(),
n_outputs=n_classes,
n_layers=2,
hidden_sizes=[16, 2],
activation=np.tanh,
seed=100,
)
y_pred = gcn_model.forward(A_hat, X)
embed = gcn_model.embedding(A_hat, X)
xent(y_pred, labels).mean()
pos = {i: embed[i,:] for i in range(embed.shape[0])}
_ = draw_kkl(g, None, colors, pos=pos, cmap='gist_rainbow', edge_color='gray')
#%% Training
train_nodes = np.array([0, 1, 8])
test_nodes = np.array([i for i in range(labels.shape[0]) if i not in train_nodes])
opt2 = GradDescentOptim(lr=2e-2, wd=2.5e-2)
embeds = list()
accs = list()
train_losses = list()
test_losses = list()
loss_min = 1e6
es_iters = 0
es_steps = 50
# lr_rate_ramp = 0 #-0.05
# lr_ramp_steps = 1000
for epoch in range(15000):
y_pred = gcn_model.forward(A_hat, X)
opt2(y_pred, labels, train_nodes)
# if ((epoch+1) % lr_ramp_steps) == 0:
# opt2.lr *= 1+lr_rate_ramp
# print(f"LR set to {opt2.lr:.4f}")
for layer in reversed(gcn_model.layers):
layer.backward(opt2, update=True)
embeds.append(gcn_model.embedding(A_hat, X))
# Accuracy for non-training nodes
acc = (np.argmax(y_pred, axis=1) == np.argmax(labels, axis=1))[
[i for i in range(labels.shape[0]) if i not in train_nodes]
]
accs.append(acc.mean())
loss = xent(y_pred, labels)
loss_train = loss[train_nodes].mean()
loss_test = loss[test_nodes].mean()
train_losses.append(loss_train)
test_losses.append(loss_test)
if loss_test < loss_min:
loss_min = loss_test
es_iters = 0
else:
es_iters += 1
if es_iters > es_steps:
print("Early stopping!")
break
if epoch % 100 == 0:
print(f"Epoch: {epoch+1}, Train Loss: {loss_train:.3f}, Test Loss: {loss_test:.3f}")
train_losses = np.array(train_losses)
test_losses = np.array(test_losses)
#%% Results
fig, ax = plt.subplots()
ax.plot(np.log10(train_losses), label='Train')
ax.plot(np.log10(test_losses), label='Test')
ax.legend()
ax.grid()
print(accs[-1])
pos = {i: embeds[-1][i,:] for i in range(embeds[-1].shape[0])}
_ = draw_kkl(g, None, colors, pos=pos, cmap='gist_rainbow', edge_color='gray')
fig, ax = plt.subplots()
_ = ax.plot(accs, marker='o')
ax.grid()
_ = ax.set(ylim=[0,1])
N = 500
snapshots = np.linspace(0, len(embeds)-1, N).astype(int)
fig, ax = plt.subplots(figsize=(10, 10))
kwargs = {'cmap': 'gist_rainbow', 'edge_color': 'gray', }#'node_size': 55}
def update(idx):
ax.clear()
embed = embeds[snapshots[idx]]
pos = {i: embed[i,:] for i in range(embed.shape[0])}
nx.draw(g, pos, node_color=colors, ax=ax, **kwargs)
anim = animation.FuncAnimation(fig, update, frames=snapshots.shape[0], interval=10, repeat=False)
#HTML(anim.to_html5_video())
writergif = animation.PillowWriter(fps=30)
anim.save('embed_anim.gif', writer=writergif, dpi=300)
# def main():
# pass
# if __name__=="__main__": main()