PyG实现GCN【Cora数据集】

from torch_geometric.datasets import Planetoid
import torch

把这里一改就可以实现Citeseer和Pubmed数据集了。

dataset_cora = Planetoid(root='./cora/',name='Cora')
print(dataset_cora)
Cora()
import torch
import torch.nn as nn
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import add_self_loops, degree

使用MPNN重写了GCNConv,实际上可以直接掉包,效果也一样。
from torch_geometric.nn import GCNConv

class GCNConv(MessagePassing):
    def __init__(self, input_dim, output_dim):
        super(GCNConv, self).__init__(aggr='add')
        self.fc = nn.Linear(input_dim, output_dim)

    def forward(self, x, edge_index):
        edge_index, _ = add_self_loops(
            edge_index=edge_index, num_nodes=x.shape[0])

        x = self.fc(x)

        row, col = edge_index
        deg = degree(index=col, dtype=x.dtype)
        deg_inv_sqrt = deg.pow(-0.5)
        norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]

        return self.propagate(edge_index, x=x, norm=norm)

    def message(self, x_j, norm):
        norm = norm.view(-1, 1)
        m = norm * x_j
        return m
    def update(self,aggr_out):
        return aggr_out
import torch.nn.functional as F

定义网络结构

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = GCNConv(dataset_cora.num_node_features, 16)
        self.conv2 = GCNConv(16, dataset_cora.num_classes)

    def forward(self, data):
        x, edge_index = data.x, data.edge_index
        x = self.conv1(x, edge_index)
        x = F.relu(x)
        x = F.dropout(x, training=self.training)
        x = self.conv2(x, edge_index)
        x = F.softmax(x, dim=1)

        return x
model = Net()
print(model)
Net(
  (conv1): GCNConv(
    (fc): Linear(in_features=1433, out_features=16, bias=True)
  )
  (conv2): GCNConv(
    (fc): Linear(in_features=16, out_features=7, bias=True)
  )
)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
model.to(device)
data = dataset_cora[0].to(device)
print(data)
cpu
Data(edge_index=[2, 10556], test_mask=[2708], train_mask=[2708], val_mask=[2708], x=[2708, 1433], y=[2708])
import torch.optim as optim
criterion = nn.CrossEntropyLoss().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
model.train()
for epoch in range(200):
    out = model(data)
    loss = criterion(out[data.train_mask], data.y[data.train_mask])

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    _, pred = torch.max(out[data.train_mask], dim=1)
    correct = (pred == data.y[data.train_mask]).sum().item()
    acc = correct/data.train_mask.sum().item()

    print('Epoch {:03d} train_loss: {:.4f} train_acc: {:.4f}'.format(
        epoch, loss.item(), acc))
Epoch 000 train_loss: 1.9454 train_acc: 0.1929
Epoch 001 train_loss: 1.9379 train_acc: 0.2786
...
Epoch 198 train_loss: 1.1854 train_acc: 1.0000
Epoch 199 train_loss: 1.1907 train_acc: 0.9929
model.eval()
out = model(data)
loss = criterion(out[data.test_mask], data.y[data.test_mask])
_, pred = torch.max(out[data.test_mask], dim=1)
correct = (pred == data.y[data.test_mask]).sum().item()
acc = correct/data.test_mask.sum().item()
print("test_loss: {:.4f} test_acc: {:.4f}".format(loss.item(), acc))
test_loss: 1.4006 test_acc: 0.8050

在测试集上的效果还不错!

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值