Introduction
GNNs process graph-structured data for social networks, molecular properties, and recommendations.
PyTorch Geometric
import torch
from torch_geometric.datasets import KarateClub
from torch_geometric.nn import GCNConv
dataset = KarateClub()
data = dataset[0]
class GCN(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv1 = GCNConv(dataset.num_features, 16)
self.conv2 = GCNConv(16, dataset.num_classes)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = torch.relu(x)
x = self.conv2(x, edge_index)
return x
Message Passing
from torch_geometric.nn import MessagePassing
class MyConv(MessagePassing):
def __init__(self):
super().__init__(aggr="add")
def forward(self, x, edge_index):
return self.propagate(edge_index, x=x)
def message(self, x_j):
return x_j
Practice Problems
- Load graph datasets
- Implement GCN layer
- Train node classification
- Visualize graph embeddings
- Create custom message passing