-
Notifications
You must be signed in to change notification settings - Fork 17
/
normalization.py
54 lines (49 loc) · 1.7 KB
/
normalization.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
import numpy as np
import scipy.sparse as sp
import torch
def aug_normalized_adjacency(adj):
adj = adj + sp.eye(adj.shape[0])
adj = sp.coo_matrix(adj)
row_sum = np.array(adj.sum(1))
d_inv_sqrt = np.power(row_sum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt).tocoo()
def normalized_adjacency(adj):
adj = adj
adj = sp.coo_matrix(adj)
row_sum = np.array(adj.sum(1))
d_inv_sqrt = np.power(row_sum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt).tocoo()
def fetch_normalization(type):
switcher = {
'AugNormAdj': aug_normalized_adjacency, # A' = (D + I)^-1/2 * ( A + I ) * (D + I)^-1/2
'NormAdj': normalized_adjacency, # A' = (D)^-1/2 * ( A) * (D)^-1/2
}
func = switcher.get(type, lambda: "Invalid normalization technique.")
return func
def row_normalize(mx):
"""Row-normalize sparse matrix"""
rowsum = np.array(mx.sum(1))
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
mx = r_mat_inv.dot(mx)
return mx
# def row_normalize(mx, adj):
# """Row-normalize sparse matrix"""
#
#
# rowsum = np.array(mx.sum(1))
# r_inv = np.power(rowsum, -1).flatten()
# r_inv[np.isinf(r_inv)] = 0.
# r_mat_inv = sp.diags(r_inv)
# mx = r_mat_inv.dot(mx)
# row_sum = np.array(adj.sum(1))
# d_inv_sqrt = np.power(row_sum, -0.5).flatten()
# d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
# d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
# mx = d_mat_inv_sqrt.dot(mx)
# return mx, d_mat_inv_sqrt