Python 邻接矩阵标准化GCN

图神经网络邻接矩阵标准化GCN

adj = np.mat(pd.read_csv(open(r'adj.csv'), header=None))
stock_num = adj.shape[0]
import math
I = np.matrix(np.eye(stock_num))
A_hat = adj + I
D_hat = np.array(np.sum(A_hat, axis=0))[0]
D_hat_sqrt = [math.sqrt(x) for x in D_hat]
D_hat_sqrt = np.array(np.diag(D_hat_sqrt))
D_hat_sqrtm_inv = np.linalg.inv(D_hat_sqrt)  # get the D_hat**-1/2 (开方后求逆即为矩阵的-1/2次方)
# D_A_final = D_hat**-1/2 * A_hat *D_hat**-1/2
D_A_final = np.dot(D_hat_sqrtm_inv, A_hat)
D_A_final = np.dot(D_A_final, D_hat_sqrtm_inv)

图神经网络邻接矩阵标准HGCN

H = np.array(H)
n_edge = H.shape[1]
# the weight of the hyperedge
W = np.ones(n_edge)
# the degree of the node
DV = np.sum(H * W, axis=1)
# the degree of the hyperedge
DE = np.sum(H, axis=0)
# print('DE',DE.shape)
invDE = np.mat(np.diag(np.power(DE, -1)))
# print(invDE)
invDE[np.isinf(invDE)]=0.0
# print('invDE', invDE.shape)
DV2 = np.mat(np.diag(np.power(DV, -0.5)))
DV2[np.isinf(DV2)]=0.0
W = np.mat(np.diag(W))
# print(W)
H = np.mat(H)
HT = H.T
if variable_weight:
    DV2_H = DV2 * H
    invDE_HT_DV2 = invDE * HT * DV2
    return DV2_H, W, invDE_HT_DV2
else:
    G = DV2 * H * W * invDE * HT * DV2
    # print(G.shape)
    # exit()
    return G

猜你喜欢

转载自blog.csdn.net/weixin_39559994/article/details/128794696