我们将摄取 Planetoid Cora 数据集,并对词袋输入特征进行行标准化。之后,我们将分析数据集和第一个图形对象。
from torch_geometric.datasets import Planetoidfrom torch_geometric.transforms import NormalizeFeatures dataset =Planetoid(root='data/Planetoid', name='Cora', transform=NormalizeFeatures()) print(f'Dataset: {dataset}:')print('======================')print(f'Number of graphs: {len(dataset)}')print(f'Number of features: {dataset.num_features}')print(f'Number of classes: {dataset.num_classes}') data = dataset[0]# Get the first graph object.print(data)
Dataset:Cora():====================== Number of graphs:1 Number of features:1433 Number of classes:7Data(x=[2708, 1433], edge_index=[2, 10556], y=[2708], train_mask=[2708], val_mask=[2708], test_mask=[2708])
from torch_geometric.nn import GCNConvimport torch.nn.functional as F classGCN(torch.nn.Module):def__init__(self,hidden_channels):super().__init__() torch.manual_seed(1234567) self.conv1 =GCNConv(dataset.num_features, hidden_channels) self.conv2 =GCNConv(hidden_channels, dataset.num_classes) defforward(self,x,edge_index): x = self.conv1(x, edge_index) x = x.relu() x = F.dropout(x, p=0.5, training=self.training) x = self.conv2(x, edge_index)return x model =GCN(hidden_channels=16)print(model) >>>GCN( (conv1): GCNConv(1433, 16) (conv2): GCNConv(16, 7) )