关于GNN一直垂涎已久,今日放假终于有空上手
我就简单的选择了pytorch_geometric作为工具
环境配置
升级CUDA到11.1,升级pytorch到1.9
代码上手
基本API
Batch(batch=[767], edge_index=[2, 2864], ptr=[25], x=[767, 21], y=[24])我感觉batch应该是节点的数量,即767个节点,因为他要存储下标edge index应该是边,一个边有两个节点,一共有2864条边,由于是无向图,所以应该是1432边y的话,应该是当前batch的数据量,正常的batch是32(设置的)这里是最后一个batchx:x是节点的表示,即一共767个节点,每一个节点的特征是32个
示例代码
from torch_geometric.datasets import Planetoiddataset = Planetoid(root='./data/', name='Cora')import torchimport torch.nn.functional as Ffrom torch_geometric.nn import GCNConvclass Net(torch.nn.Module):def __init__(self):super(Net, self).__init__()self.conv1 = GCNConv(dataset.num_node_features, 16)self.conv2 = GCNConv(16, dataset.num_classes)def forward(self, data):x, edge_index = data.x, data.edge_indexx = self.conv1(x, edge_index)x = F.relu(x)x = F.dropout(x, training=self.training)x = self.conv2(x, edge_index)return F.log_softmax(x, dim=1)device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')model = Net().to(device)data = dataset[0].to(device)optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)model.train()for epoch in range(200):optimizer.zero_grad()out = model(data)loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])loss.backward()optimizer.step()model.eval()_, pred = model(data).max(dim=1)correct = int(pred[data.test_mask].eq(data.y[data.test_mask]).sum().item())acc = correct / int(data.test_mask.sum())print('Accuracy: {:.4f}'.format(acc))
参考链接
https://pytorch-geometric.readthedocs.io/en/latest/notes/introduction.html#
https://blog.csdn.net/qq_43440798/article/details/111083989
https://blog.csdn.net/qq_39732684/article/details/104984667
