节点分类
`
将图卷积模型隐藏层的32个输出特征进行降维
activation={ }
def get_activation(name):
def hook(model,input,output):
activation[name]=output.detach()
return hook
获取隐藏层的特征输出
model.conv1.register_forward_hook(get_activation("conv1"))
_=model(data)
conv1=activation["conv1"].data.numpy()
print("conv1.shape:",conv1.shape)
使用TSNE将32维降到2维并且可视化
conv1_tsne=TSNE(n_components=2).fit_transform(conv1)
plt.figure(figsize=(12,8))
ax1=plt.subplot(1,1,1)
X=conv1_tsne[:,0]
Y=conv1_tsne[:,1]
ax1.set_xlim([min(X),max(X)])
ax1.set_ylim([min(Y),max(Y)])
for ii in range(conv1_tsne.shape[0]):
text=dataset.data.y.data.numpy()[ii]
ax1.text(X[ii],Y[ii,],str(text),fontsize=5,bbox=dict(boxstyle="round",facecolor=plt.cm.Set1(text),alpha=0.7))
ax1.set_xlabel("TSNE Feature 1",size=13)
ax1.set_ylabel("TSNE Feature 2", size=13)
ax1.set_title("GCN feature TSNE",size=14)
plt.show()#原始特征分布
`