-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRwlGNN_two.py
218 lines (180 loc) · 6.04 KB
/
RwlGNN_two.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
import time
import numpy as np
from copy import deepcopy
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from deeprobust.graph.utils import accuracy
import matplotlib.pyplot as plt
import warnings
from utils import *
class RwlGNN:
""" Rwl-GNN-Two-stage (Robust Weighted Gaph Laplacian)
Parameters
----------
model:
model: The backbone GNN model in RWL-GNN
args:
model configs
device: str
'cpu' or 'cuda'.
Examples
--------
See details in https://github.com/Bharat-Runwal/RWL-GNN.
"""
def __init__(self,model, args, device):
self.device = device
self.args = args
def fit(self, features, adj):
"""Train RWL-GNN: Two-Stage.
Parameters
----------
features :
node features
adj :
the adjacency matrix. The format could be torch.tensor or scipy matrix
labels :
node labels
idx_train :
node training indices
idx_val :
node validation indices
"""
args = self.args
self.symmetric = args.symmetric
optim_sgl = args.optim
lr_sgl = args.lr_optim
adj = (adj.t() + adj)/2
rowsum = adj.sum(1)
r_inv = rowsum.flatten()
D = torch.diag(r_inv)
L_noise = D - adj
# INIT
# self.weight = torch.rand(int(n*(n-1)/2),dtype=torch.float,requires_grad=True,device = self.device)
self.weight = self.Linv(L_noise)
self.weight.requires_grad = True
self.weight = self.weight.to(self.device)
c = self.Lstar(2*L_noise*args.alpha - args.beta*(torch.matmul(features,features.t())) )
if optim_sgl == "Adam":
self.sgl_opt =AdamOptimizer(self.weight,lr=lr_sgl)
elif optim_sgl == "RMSProp":
self.sgl_opt = RMSProp(self.weight,lr = lr_sgl)
elif optim_sgl == "sgd_momentum":
self.sgl_opt = sgd_moment(self.weight,lr=lr_sgl)
else:
self.sgl_opt = sgd(self.weight,lr=lr_sgl)
t_total = time.time()
for epoch in range(args.epochs_pre):
self.train_specific(c)
print("Optimization Finished!")
print("Total time elapsed: {:.4f}s".format(time.time() - t_total))
print(args)
return self.A().detach()
def w_grad(self,alpha,c):
with torch.no_grad():
grad_f = self.Lstar(alpha*self.L()) - c
return grad_f
def train_specific(self,c):
args = self.args
if args.debug:
print("\n=== train_adj ===")
t = time.time()
sgl_grad = self.w_grad(args.alpha ,c)
total_grad = sgl_grad
self.weight = self.sgl_opt.backward_pass(total_grad)
self.weight = torch.clamp(self.weight,min=0)
def feature_smoothing(self, adj, X):
adj = (adj.t() + adj)/2
rowsum = adj.sum(1)
r_inv = rowsum.flatten()
D = torch.diag(r_inv)
L = D - adj
r_inv = r_inv + 1e-3
r_inv = r_inv.pow(-1/2).flatten()
r_inv[torch.isinf(r_inv)] = 0.
r_mat_inv = torch.diag(r_inv)
L = r_mat_inv @ L @ r_mat_inv
XLXT = torch.matmul(torch.matmul(X.t(), L), X)
loss_smooth_feat = torch.trace(XLXT)
return loss_smooth_feat
def A(self,weight=None):
# with torch.no_grad():
if weight == None:
k = self.weight.shape[0]
a = self.weight
else:
k = weight.shape[0]
a = weight
n = int(0.5 * (1 + np.sqrt(1 + 8 * k)))
Aw = torch.zeros((n,n),device=self.device)
b=torch.triu_indices(n,n,1)
Aw[b[0],b[1]] =a
Aw = Aw + Aw.t()
return Aw
def L(self,weight=None):
if weight==None:
k= len(self.weight)
a = self.weight
else:
k = len(weight)
a = weight
n = int(0.5*(1+ np.sqrt(1+8*k)))
Lw = torch.zeros((n,n),device=self.device)
b=torch.triu_indices(n,n,1)
Lw[b[0],b[1]] = -a
Lw = Lw + Lw.t()
row,col = np.diag_indices_from(Lw)
Lw[row,col] = -Lw.sum(axis=1)
return Lw
def Linv(self,M):
with torch.no_grad():
N=M.shape[0]
k=int(0.5*N*(N-1))
# l=0
w=torch.zeros(k,device=self.device)
##in the triu_indices try changing the 1 to 0/-1/2 for other
## ascpect of result on how you want the diagonal to be included
indices=torch.triu_indices(N,N,1)
M_t=torch.tensor(M)
w=-M_t[indices[0],indices[1]]
return w
def Lstar(self,M):
N = M.shape[1]
k =int( 0.5*N*(N-1))
w = torch.zeros(k,device=self.device)
tu_enteries=torch.zeros(k,device=self.device)
tu=torch.triu_indices(N,N,1)
tu_enteries=M[tu[0],tu[1]]
diagonal_enteries=torch.diagonal(M)
b_diagonal=diagonal_enteries[0:N-1]
x=torch.linspace(N-1,1,steps=N-1,dtype=torch.long,device=self.device)
x_r = x[:N]
diagonal_enteries_a=torch.repeat_interleave(b_diagonal,x_r)
new_arr=torch.tile(diagonal_enteries,(N,1))
tu_new=torch.triu_indices(N,N,1)
diagonal_enteries_b=new_arr[tu_new[0],tu_new[1]]
w=diagonal_enteries_a+diagonal_enteries_b-2*tu_enteries
return w
def normalize(self,w=None):
if self.symmetric:
if w == None:
adj = (self.A() + self.A().t())
else:
adj = self.A(w)
adj = adj + adj.t()
else:
if w == None:
adj = self.A()
else:
adj = self.A(w)
normalized_adj = self._normalize(adj + torch.eye(adj.shape[0]).to(self.device))
return normalized_adj
def _normalize(self, mx):
rowsum = mx.sum(1)
r_inv = rowsum.pow(-1/2).flatten()
r_inv[torch.isinf(r_inv)] = 0.
r_mat_inv = torch.diag(r_inv)
mx = r_mat_inv @ mx
mx = mx @ r_mat_inv
return mx