Dgl+Pytorch实现节点分类

import dgl
import numpy as np


def build_karate_club_graph():
    # All 78 edges are stored in two numpy arrays. One for source endpoints
    # while the other for destination endpoints.
    src = np.array([1, 2, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 10, 10,
        10, 11, 12, 12, 13, 13, 13, 13, 16, 16, 17, 17, 19, 19, 21, 21,
        25, 25, 27, 27, 27, 28, 29, 29, 30, 30, 31, 31, 31, 31, 32, 32,
        32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33,
        33, 33, 33, 33, 33, 33, 33, 33, 33, 33])
    dst = np.array([0, 0, 1, 0, 1, 2, 0, 0, 0, 4, 5, 0, 1, 2, 3, 0, 2, 2, 0, 4,
        5, 0, 0, 3, 0, 1, 2, 3, 5, 6, 0, 1, 0, 1, 0, 1, 23, 24, 2, 23,
        24, 2, 23, 26, 1, 8, 0, 24, 25, 28, 2, 8, 14, 15, 18, 20, 22, 23,
        29, 30, 31, 8, 9, 13, 14, 15, 18, 19, 20, 22, 23, 26, 27, 28, 29, 30,
        31, 32])
    # Edges are directional in DGL; Make them bi-directional.
    #做两次数据组合  组成u,v 这样将u和v填充进去得到的就是一个双向无向图
    u = np.concatenate([src, dst])
    v = np.concatenate([dst, src])
    # Construct a DGLGraph
    return dgl.DGLGraph(graph_data=(u, v))

G=build_karate_club_graph()

import networkx as nx

# Since the actual graph is undirected, we convert it for visualization
# purpose.

#转为无向图
nx_G=G.to_networkx().to_undirected()
# Kamada-Kawaii layout usually looks pretty for arbitrary graphs
#使用Kamada-Kawai路径长度代价函数定位节点
pos = nx.kamada_kawai_layout(G=nx_G)#一个布局,作图方式让节点画出来在图上更直观
#nx.spring_layout(nx_G)

nx.draw(G=nx_G, pos=pos, with_labels=True, node_color=[[.2, .8, .2]])


# In DGL, you can add features for all nodes at once, using a feature tensor that
# batches node features along the first dimension. The code below adds the learnable
# embeddings for all nodes:

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

embed = nn.Embedding(34, 5)  # 34 nodes with embedding dim equal to 5

Graph=build_karate_club_graph()
Graph.ndata['feature']=embed.weight#给节点赋予初始的特征向量

#查看节点的特征
# Graph.nodes[0].data['feature']
# Graph.ndata['feature'][2]
print(Graph.ndata['feature'][[2,3]])

#定义GCN模块
from dgl.nn.pytorch import GraphConv

#包含两层gcn的深度gcn模块
class GCN(nn.Module):
    def __init__(self, in_feats, hidden_size, num_classes):
        super(GCN, self).__init__()
        self.conv1 = GraphConv(in_feats, hidden_size)
        self.conv2 = GraphConv(hidden_size, num_classes)

    def forward(self, g, inputs):
        h = self.conv1(g, inputs)#输入g应该是邻接矩阵(或者是原始的能被识别的图),inputs应该是特征矩阵,这里可以是Graph.ndata
        h = torch.relu(h)
        h = self.conv2(g, h)
        return h


# The first layer transforms input features of size of 5 to a hidden size of 5.
# The second layer transforms the hidden layer and produces output features of
# size 2, corresponding to the two groups of the karate club.
net=GCN(5,5,2)#定义网络结构

#数据表示和初始化
inputs=Graph.ndata['feature']#要输入的特征矩阵
labeled_nodes = torch.tensor([0, 33]) #半监督学习,只有0号和33号节点有标签
labels = torch.tensor([0, 1])#给0和33号节点赋予0  1标签

#训练和可视化
# 1、创建优化器
# 2、将输入填充到模型中
# 3、计算loss
# 4、优化反向传播

import itertools

optimizer = torch.optim.Adam(itertools.chain(net.parameters(), embed.parameters()), lr=0.01)
all_logits = []
for epoch in range(50):
    logits = net(G, inputs)
#     print(epoch,"  logits:",logits)
    # we save the logits for visualization later
    all_logits.append(logits.detach())#阻断反向传播
    #用log_softmax更稳定,速度也更快  损失函数的定义
    logp = F.log_softmax(input=logits, dim=1)#input是经过GCN输出的每一个节点的特征表示向量  dim=1表示在列上做log_softmax计算
    # we only compute loss for labeled nodes
    loss = F.nll_loss(logp[labeled_nodes], labels)#利用损失函数  对有标签节点进行loss计算

    optimizer.zero_grad()#梯度清零
    loss.backward()#反向传播
    optimizer.step()#参数更新到每一步

    print('Epoch %d | Loss: %.4f' % (epoch, loss.item()))

len(all_logits)#两层嵌套循环  外层长度为50,代表50次循环迭代
print(len(all_logits[0]))#这是内层循环   得到的是每次循环34个节点的输出特征表示向量

#因为这个模型  最后输出的节点的特征表示是2维的特征表示  所以可以用一个2D空间里来可视化模型的训练过程
import matplotlib.animation as animation
import matplotlib.pyplot as plt
%matplotlib notebook

#参数i代表第几次迭代循环
def draw(i):
    cls1color = '#00FFFF'
    cls2color = '#FF00FF'
    pos = {}
    colors = []
    for v in range(34):
        pos[v] = all_logits[i][v].numpy()
        #返回每行或每列最大值的索引
        cls = pos[v].argmax()#输出哪一个索引位置   对应的值最大,这里是二维,只有(0 1)两个索引  正好用于二分类
        colors.append(cls1color if cls else cls2color)
    ax.cla()#Clear axis即清除当前图形中的当前活动轴。其他轴不受影响。  
    ax.axis('off')
    ax.set_title('Epoch: %d' % i)
    #=nx.kamada_kawai_layout(G.to_networkx().to_undirected())
    nx.draw_networkx(nx_G.to_undirected(), pos, node_color=colors,
            with_labels=True, node_size=300, ax=ax)

fig = plt.figure(dpi=150)
fig.clf()#Clear figure清除所有轴,但是窗口打开,这样它可以被重复使用
ax = fig.subplots()

ani = animation.FuncAnimation(fig, draw, frames=len(all_logits), interval=500)#制作动画

draw(0)

Dgl+Pytorch实现节点分类_第1张图片

你可能感兴趣的:(DGL工具)