-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_filtering.py
60 lines (55 loc) · 1.52 KB
/
graph_filtering.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
import numpy as np
import traceback
import matplotlib.pyplot as plt
import torch
import scipy.sparse as sp
def fgc_filter(A, X, device, args, k, a):
# Convert A and X to PyTorch tensors
if type(A) is np.ndarray:
A = torch.from_numpy(A).to(device)
if type(X) is np.ndarray:
X = torch.from_numpy(X).to(device)
# Convert X to float
X = X.float().to(device)
A = A.float().to(device)
I_n = torch.eye(A.shape[0]).to(device)
I_d = torch.eye(X.shape[1]).to(device)
# Normalize A
A = A + I_n
D = torch.sum(A, 1)
D = torch.pow(D, -0.5)
D[torch.isinf(D)] = 0
D = torch.diag(D)
A = D.matmul(A).matmul(D)
# Get filter G
Ls = I_n - A
G = I_n - 0.5*Ls
# Set f(A)
A_ = I_n
if args.dataset=='ACM': f=2
elif args.dataset=='amazon': f=1
elif args.dataset=='aminer': f=3
elif args.dataset=='DBLP_L': f=3
for _ in range(f):
A_ = G.matmul(A_)
# Set the order of filter
G_ = G
kk = 1
while(kk <= k):
#compute
X_bar = G_.matmul(X)
XtX_bar = X_bar.matmul(X_bar.T)
XXt_bar = X_bar.t().matmul(X_bar)
tmp = torch.inverse(I_d + XXt_bar/a)
tmp = X_bar.matmul(tmp).matmul((X_bar.t()))
tmp = I_n/a -tmp/(a*a)
S = tmp.matmul(a * A_ + XtX_bar)
kk += 1
G_ = G_.matmul(G)
X_ = S.matmul(X)
return X_
def fgc_multi(A, X, device, k, a, args):
X_ = X.copy()
for idx, x in enumerate(X):
X_[idx] = fgc_filter(A[idx], x, device, args, k, a)
return X_