Python 邻接矩阵标准化GCN

本文介绍图神经网络中邻接矩阵的两种标准化方法:GCN和HGCN。详细展示了如何通过添加单位矩阵、计算节点度数及进行矩阵开方等步骤实现标准化过程。适用于理解图神经网络预处理阶段的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图神经网络邻接矩阵标准化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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python量化投资、代码解析与论文精读

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值