I. Model structure
In the study of Graph Neural Networks, GCN (Graph Convolutional Networks) is one of the more common and effective models.
In a GCN model, each node contains an aggregation of information about that node's neighboring nodes, which means it is a global model. A typical GCN model usually consists of two parts: a convolutional layer based on a message passing algorithm and a multilayer perceptron. Among them, the former mainly accomplishes feature fusion, while the latter is responsible for the classification task.
For a graph G with n nodes, its identity matrix X can be expressed as:
The steps are as follows:
- Construct a two-layer convolutional network: the first layer is a GCN layer followed by ReLU activations and a random deactivation layer; the second layer is an output classifier.
- The model is optimized for a specific loss function (e.g., cross-entropy loss) during training and is used to predict new data.
II. PyTorch implementation
PyTorch uses the dgl library to easily build graphs, and PyG provides similar tools. Next, see how to implement a simple GCN model using PyTorch + PyG, using the Cora dataset as an example.
Prepare data
Cora is a dataset for a categorization task, which contains 2708 text node names, and 1433 dimensional features (lexical correlations) for each node. First, we need to convert it into a graphical object with corresponding edge information in PyG. Specifically, use the tool to load the Cora dataset and then convert it into a PyG graph.
from torch_geometric.datasets import Planetoid import torch_geometric.transforms as T dataset = Planetoid(root='/path/to/dataset', name='Cora', transform=()) data = dataset[0] print(data)
Define the GCN model
Before defining the GCN network for PyG, it is necessary to define the Convolutional Layer, which takes as input the adjacency matrix A, scatters the messages through the weights weights matrix W, and outputs a new feature vector.
import as F from torch_geometric.nn import GCNConv class Net(): def __init__(self): super(Net, self).__init__() self.conv1 = GCNConv(dataset.num_features, 16) self.conv2 = GCNConv(16, dataset.num_classes) def forward(self, x, edge_index): x = self.conv1(x, edge_index) x = (x) x = (x, training=) x = self.conv2(x, edge_index) return F.log_softmax(x, dim=1)
Defining the training process
The specific process of training is as follows:
- For each epoch, stochastic gradient descent optimization is performed. We choose cross entropy as the loss function and use Adam as the optimizer.
- During testing, the accuracy is evaluated with a validation set.
device = ('cuda' if .is_available() else 'cpu') model = Net().to(device) (device) optimizer = ((), lr=0.01, weight_decay=5e-4) def train(): () optimizer.zero_grad() out = model(, data.edge_index) loss = F.nll_loss(out[data.train_mask], [data.train_mask]) () () def test(): () _, pred = model(, data.edge_index).max(dim=1) correct = int(pred[data.test_mask].eq([data.test_mask]).sum().item()) acc = correct / int(data.test_mask.sum()) return acc for epoch in range(1, 201): train() test_acc = test() print(f'Epoch: {epoch:03d}, Test Acc: {test_acc:.4f}')
Above is a detailed explanation of Pytorch+PyG to achieve the GCN process example of the details, more information about Pytorch PyG to achieve the GCN please pay attention to my other related articles!