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
在测试集上的效果还不错!
标签:loss,Cora,self,torch,mask,GCN,train,data,PyG 来源: https://blog.csdn.net/weixin_41650348/article/details/112755027