本文主要是介绍GCN学习(四):使用numpy从底层一步步搭建GCN网络:Zachary数据集随机参数无训练实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Zachary 空手道俱乐部是一个被广泛使用的社交网络,其中的节点代表空手道俱乐部的成员,边代表成员之间的相互关系。当年,Zachary 在研究空手道俱乐部的时候,管理员和教员发生了冲突,导致俱乐部一分为二。下图显示了该网络的图表征,其中的节点标注是根据节点属于俱乐部的哪个部分而得到的,「0」表示属于Mr. Hi部分的中心节点,[32」表示属于Officer阵营的中心节点
通过networkx调用karate club图
import networkx as nx
import numpy as np
#通过nx.to_numpy_array将图节点按照顺序转化成numpy的数组
zkc=nx.karate_club_graph()
order=list(zkc.nodes())
A=nx.to_numpy_array(zkc.nodes(),nodelist=order)
计算GCN的各项变量
具体算法原理见上一篇博客
#具体算法原理见上一篇博客
I=np.eye(A.shape[0])
A_hat=A+I
print('A_hat:',A_hat)
D_hat=np.sum(A_hat,axis=0)
D_hat=np.diag(D_hat)
print('D_hat:',D_hat)
D_hat_I=np.linalg.inv(D_hat)
D_hat_I_=D_hat_I**0.5
D_hat_I_=np.matrix(D_hat_I_)
A_hat=np.matrix(A_hat)
print('D_hat_I:',D_hat_I)
print('D_hat_I_:',D_hat_I_)
>>>A: [[0. 1. 1. ... 1. 0. 0.][1. 0. 1. ... 0. 0. 0.][1. 1. 0. ... 0. 1. 0.]...[1. 0. 0. ... 0. 1. 1.][0. 0. 1. ... 1. 0. 1.][0. 0. 0. ... 1. 1. 0.]]
A_hat: [[1. 1. 1. ... 1. 0. 0.][1. 1. 1. ... 0. 0. 0.][1. 1. 1. ... 0. 1. 0.]...[1. 0. 0. ... 1. 1. 1.][0. 0. 1. ... 1. 1. 1.][0. 0. 0. ... 1. 1. 1.]]
D_hat: [[17. 0. 0. ... 0. 0. 0.][ 0. 10. 0. ... 0. 0. 0.][ 0. 0. 11. ... 0. 0. 0.]...[ 0. 0. 0. ... 7. 0. 0.][ 0. 0. 0. ... 0. 13. 0.][ 0. 0. 0. ... 0. 0. 18.]]
D_hat_I: [[0.05882353 0. 0. ... 0. 0. 0. ][0. 0.1 0. ... 0. 0. 0. ][0. 0. 0.09090909 ... 0. 0. 0. ]...[0. 0. 0. ... 0.14285714 0. 0. ][0. 0. 0. ... 0. 0.07692308 0. ][0. 0. 0. ... 0. 0. 0.05555556]]
D_hat_I_: [[0.24253563 0. 0. ... 0. 0. 0. ][0. 0.31622777 0. ... 0. 0. 0. ][0. 0. 0.30151134 ... 0. 0. 0. ]...[0. 0. 0. ... 0.37796447 0. 0. ][0. 0. 0. ... 0. 0.2773501 0. ][0. 0. 0. ... 0. 0. 0.23570226]]
获得随机参数&生成GCN中两个隐层
这里未代入激活函数
W_1=np.random.normal(loc=0,scale=1,size=(A.shape[0],4))
W_2=np.random.normal(loc=0,scale=1,size=(W_1.shape[1],2))
def gcn_layer(W,H):return D_hat_I_*A_hat*D_hat_I_*H*W
H_1=gcn_layer(W_1,I)
H_2=gcn_layer(W_2,H_1)
print('W_1:',W_1)
print('W_2:',W_2)
print('H_1:',H_1)
print('H_2:',H_2)
>>>W_1: [[-0.83170794 0.32384612 0.19254748 0.70696413][-0.18471147 0.0883649 -1.11661952 -0.48318362][ 1.16053196 1.35436878 1.7325107 -0.81340677][-0.05643657 0.78313805 1.48633055 -0.78429642][ 2.04825991 0.75946907 0.7877478 0.40471074][-1.6825711 1.24341323 -0.13138839 0.09831662][ 1.20441677 -1.02081272 -0.15438911 1.03961703][ 0.85859398 1.04836695 0.54997693 0.34259851][-1.24704768 -0.72198946 -2.01097211 0.74801387][-0.69092111 0.52773092 0.28031613 0.25813498][-1.26664724 -1.49773084 -0.93544537 -0.285444 ][-0.24153454 1.91185276 -0.3330864 0.18437171][ 0.57581633 1.47301229 0.85280729 0.21404787][-0.73612301 0.0249905 -0.22220335 2.47315254][ 0.01006193 1.14992678 1.81193055 0.24425048][ 0.49826219 0.46537806 1.15559036 1.22559649][-0.17573175 0.7642652 -0.61541977 1.09116569][-2.0212443 -1.20003893 1.29472694 0.60905143][-0.15924997 -0.1955486 0.93153308 -0.54726901][ 0.10496633 -1.75949359 -0.57203004 -0.44890057][-1.55455233 0.60525928 -0.85878164 -0.17461178][ 2.09575314 1.26960615 -2.41258093 1.70626018][-0.4674859 -0.46671862 -0.08762951 0.49089521][ 0.47360112 0.7122978 1.4140279 -0.02709561][ 1.21027325 -0.58404666 0.69746025 0.25142965][ 0.94810688 1.5624341 -0.07637426 -2.13034201][ 0.36438514 0.6206908 0.0784685 0.25960684][ 1.21386557 0.29242271 -1.09378702 -0.08382282][-0.08687322 0.28939295 -1.42996523 0.96757856][ 0.89381163 -0.85629006 0.57127589 -0.35855218][-0.27673337 0.69421144 2.84473947 1.3983313 ][-0.3101751 -0.64610953 -0.90312309 2.52619311][ 0.41719548 -0.86331992 0.97754337 -0.1387886 ][ 0.83100378 0.35643724 -0.13823076 0.71158029]]
W_2: [[-0.36620028 0.10869551][ 0.20106848 -0.41626057][ 1.46492995 0.18866806][ 1.1037179 -0.59881204]]
H_1: [[-9.77573622e-03 4.47259336e-01 -2.68136684e-01 9.30080792e-01][ 3.91011566e-02 2.40576126e-01 4.02806554e-01 7.51832662e-01][-4.25046615e-02 4.02775288e-01 -1.98745516e-01 4.52892529e-01][ 1.83068787e-01 8.08970173e-01 5.38734247e-01 2.88551670e-01][ 3.63859541e-01 -3.73553995e-01 -4.80970347e-02 3.48014115e-01][-5.14446933e-01 -5.79242231e-02 -4.04343439e-01 5.22178309e-01][ 2.26788840e-01 4.46801019e-01 -1.90257319e-02 6.76501601e-01][ 2.02331834e-01 5.72293659e-01 4.57813658e-01 -1.65381793e-01][-7.06639358e-02 1.41736786e-01 5.13920070e-01 4.02601735e-01][ 8.48009367e-02 4.60180616e-01 3.76219287e-01 4.12828613e-02][-2.81690570e-01 1.32742319e-01 -4.29539179e-02 1.37532944e-01][-2.63404003e-01 1.01146553e+00 -1.33521581e-01 2.13429204e-01][ 6.31609004e-02 7.07246358e-01 6.35575053e-01 -8.03440067e-04][-1.47780407e-02 3.69488775e-01 2.67178037e-01 2.67142059e-01][ 1.83244009e-01 2.93572066e-01 7.41698242e-01 1.56026675e-01][ 3.45977430e-01 6.53891596e-02 5.22918177e-01 4.83142011e-01][-1.82036166e-01 3.12230272e-01 -2.78927357e-01 6.57535104e-01][-8.23934054e-01 -3.38532298e-01 2.54671788e-01 2.13795066e-01][ 1.26806708e-01 -1.54919725e-01 4.48232418e-01 -1.07813153e-01][-5.88853171e-03 -3.44623038e-01 -3.12501397e-01 -1.90306455e-02][-3.38294077e-01 1.12016234e-01 -1.48539155e-01 1.64059237e-02][ 5.48398429e-01 4.84682726e-01 -9.81097503e-01 5.79531317e-01][ 2.40613990e-02 -2.45309733e-01 1.08511554e-01 2.38241587e-01][ 7.84473937e-01 2.71245658e-01 2.22068174e-01 -4.67379303e-01][ 7.52406042e-01 1.87881340e-01 -2.59980935e-01 -1.10658173e-02][ 5.77650872e-01 2.67890814e-01 2.73234515e-01 2.14666589e-03][ 4.65328173e-01 3.43087536e-02 1.54848144e-01 9.07916513e-02][ 8.43947365e-01 2.78129762e-01 4.14405336e-01 -1.62742006e-04][ 1.92555623e-01 1.96430323e-01 -2.83270365e-01 6.80535120e-01][ 4.98655722e-01 4.95409797e-02 4.99359183e-01 4.81653435e-02][-1.69805316e-01 -4.99875404e-02 1.50560982e-01 4.05694340e-01][ 3.88690410e-01 1.18226973e-01 -1.74070779e-01 3.02309738e-01][-1.27975340e-01 2.30745593e-01 9.45281200e-01 6.41611149e-01][-1.76417968e-01 1.16101401e-01 3.56420570e-01 9.63796968e-01]]
H_2: [[ 0.80668705 -0.56944503][ 0.55274371 -0.38204191][ 1.06747189 -0.37504982][ 0.88372849 -0.32185238][ 0.33144136 -0.27353039][ 0.4203575 -0.47771249][ 0.39499527 -0.44329704][ 0.63488063 -0.28928895][ 0.85832425 -0.30308914][ 0.50031215 -0.20765919][ 0.2051401 -0.24015893][ 0.29462237 -0.43763181][ 0.71417285 -0.25166981][ 0.77168669 -0.35817718][ 0.99474094 -0.14773845][ 0.97309833 -0.18923361][ 0.37770452 -0.45453059][ 0.64819026 -0.20665704][ 0.73120033 -0.05334585][ 0.3788625 -0.21494375][ 0.5601559 -0.16956082][ 0.06696635 -0.42187805][ 0.69911044 -0.13496531][ 0.56439119 -0.01401422][-0.02490054 -0.03893154][-0.19040559 0.00897865][ 0.44032396 -0.04930899][ 0.07001811 -0.06985502][ 0.3120261 -0.31273757][ 0.5349575 -0.02366688][ 1.0258222 -0.2474404 ][ 0.42400361 -0.29460932][ 1.07155588 -0.15361521][ 1.08338933 -0.18341541]]
绘图
for i in range(A.shape[0]):if zkc.nodes[i]['club'] == 'Mr. Hi':plt.scatter(np.array(H_2)[i,0],np.array(H_2)[i,1] ,label=i,color = 'b',alpha=0.5,s = 250)plt.text(np.array(H_2)[i,0],np.array(H_2)[i,1] ,i, horizontalalignment='center',verticalalignment='center', fontdict={'color':'black'})else:plt.scatter(np.array(H_2)[i,0],np.array(H_2)[i,1] ,label = 'i',color = 'r',alpha=0.5,s = 250)plt.text(np.array(H_2)[i,0],np.array(H_2)[i,1] ,i, horizontalalignment='center',verticalalignment='center', fontdict={'color':'black'})
plt.show()
结果
每次随机的初始化参数不同,产生的结果好坏区别甚大。
这篇关于GCN学习(四):使用numpy从底层一步步搭建GCN网络:Zachary数据集随机参数无训练实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!