GCN实践——可视化cora-network

 

本文主要介绍如何使用 GCN 可视化 cora 网络中节点

训练模型

按照源码上readme给出的步骤,训练 citation network —— cora 数据集。具体步骤如下:

  • 步骤一:下载源代码。直接下载zip或者通过git下载都行;
  • 步骤二:进入到 setup.py 所在的目录,执行命令python setup.py install
  • 步骤三:进入到 train.py 所在的目录,执行命令python train.py

通过上面三步骤,利用gcn完成节点分类任务的模型就开始训练起来了!训练界面为:

Epoch: 0001 train_loss= 1.95399 train_acc= 0.07143 val_loss= 1.95070 val_acc= 0.20600 time= 0.08406
Epoch: 0002 train_loss= 1.94801 train_acc= 0.29286 val_loss= 1.94716 val_acc= 0.37000 time= 0.06582
Epoch: 0003 train_loss= 1.94218 train_acc= 0.48571 val_loss= 1.94333 val_acc= 0.47000 time= 0.06585
Epoch: 0004 train_loss= 1.93654 train_acc= 0.56429 val_loss= 1.93922 val_acc= 0.50400 time= 0.06383
Epoch: 0005 train_loss= 1.92665 train_acc= 0.66429 val_loss= 1.93517 val_acc= 0.50400 time= 0.06884
Epoch: 0006 train_loss= 1.92017 train_acc= 0.70000 val_loss= 1.93110 val_acc= 0.51400 time= 0.06865
Epoch: 0007 train_loss= 1.91050 train_acc= 0.71429 val_loss= 1.92704 val_acc= 0.52000 time= 0.06542
Epoch: 0008 train_loss= 1.89941 train_acc= 0.71429 val_loss= 1.92310 val_acc= 0.51600 time= 0.06480
Epoch: 0009 train_loss= 1.89015 train_acc= 0.75714 val_loss= 1.91920 val_acc= 0.52000 time= 0.07132
Epoch: 0010 train_loss= 1.88369 train_acc= 0.67143 val_loss= 1.91527 val_acc= 0.52000 time= 0.06580
....
....
Epoch: 0191 train_loss= 0.60898 train_acc= 0.98571 val_loss= 1.06063 val_acc= 0.78200 time= 0.06386
Epoch: 0192 train_loss= 0.63756 train_acc= 0.95714 val_loss= 1.05901 val_acc= 0.77800 time= 0.06482
Epoch: 0193 train_loss= 0.62371 train_acc= 0.94286 val_loss= 1.05767 val_acc= 0.77800 time= 0.06440
Epoch: 0194 train_loss= 0.60151 train_acc= 0.96429 val_loss= 1.05636 val_acc= 0.77800 time= 0.06582
Epoch: 0195 train_loss= 0.60843 train_acc= 0.95714 val_loss= 1.05533 val_acc= 0.77800 time= 0.06582
Epoch: 0196 train_loss= 0.59138 train_acc= 0.97143 val_loss= 1.05411 val_acc= 0.78000 time= 0.06286
Epoch: 0197 train_loss= 0.59821 train_acc= 0.97857 val_loss= 1.05297 val_acc= 0.78000 time= 0.06286
Epoch: 0198 train_loss= 0.60693 train_acc= 0.97143 val_loss= 1.05188 val_acc= 0.77800 time= 0.06479
Epoch: 0199 train_loss= 0.60899 train_acc= 0.95714 val_loss= 1.05047 val_acc= 0.77800 time= 0.06583
Epoch: 0200 train_loss= 0.59147 train_acc= 0.97143 val_loss= 1.04964 val_acc= 0.77800 time= 0.06485
Optimization Finished!
Test set results: cost= 1.01263 accuracy= 0.81400 time= 0.02793

可视化

进一步,利用 tsne 对 gcn 的 outputs 进行可视化,观测是否能分成明显的7簇?

需要注意的是,要修改源代码,以将网络中节点的embedding 和 label输出出来。

  • 步骤一:修改 utils.py 的load_data()函数,将变量labels返回

    • return adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask, labels
  • 步骤二:修改 train.py 文件。在训练完后添加代码

    • adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask, labels = load_data(FLAGS.dataset)
      ......
      .....
      print("Optimization Finished!")
      label_dict = {0:"0.0000000e+00",1:"1.0000000e+00",2:"2.0000000e+00",3:"3.0000000e+00",4:"4.0000000e+00",5:"5.0000000e+00",6:"6.0000000e+00"} # 定义标签颜色字典
      # 写文件
      with open("./embeddings.txt", "w") as fe, open("./labels.txt", 'w') as fl:
          for i in range(len(outs[3])):
              fl.write(label_dict[int(list(labels[i]).index(1.))]+"\n")
              fe.write(" ".join(map(str, outs[3][i]))+"\n")
      

可视化结果为

 

                                         GCN实践——可视化cora-network_第1张图片

                                                                            cora数据集可视化

可以发现GCN对cora数据集的可视化还是很友好的。即便使用的默认参数,也能训练出有意义的节点表示

你可能感兴趣的:(GCN)