Skip to content
SDB
ML Fundamentals

Chapter 14 · advanced · 38 min

Graph Neural Networks (GNNs)

GCN, GraphSAGE, and GAT — deep learning on graph-structured data for fraud detection, drug discovery, and recommendations

Subhendu Datta BhowmikAI Tutorials

Why Graphs?

Many real-world problems are inherently relational: entities connected by relationships.

DomainNodesEdges
Social networkUsersFriendships
MoleculeAtomsChemical bonds
Knowledge graphEntitiesRelations
Transaction networkAccountsPayments
Citation networkPapersCitations
RecommendationUsers & itemsInteractions

Standard deep learning fails on graphs because:

  • Graphs have no fixed-size grid structure (unlike images)
  • Nodes have variable neighborhoods — no notion of "left neighbor"
  • Permutation invariance required — node ordering is arbitrary
  • Structural information (connectivity) carries signal that feature vectors alone miss

Graph Neural Networks solve this by learning representations that capture both node features and graph topology.

The Message Passing Framework

All major GNN variants follow the message-passing neural network (MPNN) framework (Gilmer et al., 2017):

For each node vv at layer kk:

mv(k)=AGGREGATE(k)({hu(k1):uN(v)})\mathbf{m}_v^{(k)} = \text{AGGREGATE}^{(k)}\left(\{\mathbf{h}_u^{(k-1)} : u \in \mathcal{N}(v)\}\right)

hv(k)=UPDATE(k)(hv(k1), mv(k))\mathbf{h}_v^{(k)} = \text{UPDATE}^{(k)}\left(\mathbf{h}_v^{(k-1)},\ \mathbf{m}_v^{(k)}\right)

Where:

  • hv(0)=xv\mathbf{h}_v^{(0)} = \mathbf{x}_v (initial node features)
  • N(v)\mathcal{N}(v) is the neighborhood of node vv
  • kk indexes the layer (each layer = one hop)

After KK layers, hv(K)\mathbf{h}_v^{(K)} summarizes the KK-hop neighborhood around vv.

Readout (for graph-level tasks): hG=READOUT({hv(K):vG})\mathbf{h}_G = \text{READOUT}\left(\{\mathbf{h}_v^{(K)} : v \in G\}\right)

Different GNN variants differ in how they define AGGREGATE and UPDATE.

GCN — Graph Convolutional Network

Kipf & Welling (2017) — the seminal spectral-based GNN.

H(k)=σ ⁣(D~1/2A~D~1/2H(k1)W(k))\mathbf{H}^{(k)} = \sigma\!\left(\tilde{D}^{-1/2}\tilde{A}\tilde{D}^{-1/2}\mathbf{H}^{(k-1)}\mathbf{W}^{(k)}\right)

Where:

  • A~=A+I\tilde{A} = A + I (adjacency matrix with self-loops)
  • D~ii=jA~ij\tilde{D}_{ii} = \sum_j \tilde{A}_{ij} (degree matrix)
  • W(k)\mathbf{W}^{(k)} — learnable weight matrix

In plain English: take each node's features + its neighbors' features, normalize by degree, linearly transform, then apply activation.

Properties & Limitations

PropertyDetail
AggregationMean of normalized neighbors
Complexity$O(
StrengthSimple, effective for homophilic graphs
WeaknessFixed equal weights to all neighbors (no attention)
WeaknessTransductive — cannot generalize to unseen nodes

GraphSAGE — Inductive Representation Learning

Hamilton et al. (2017) — enables inductive learning (generalizes to new nodes).

hN(v)(k)=AGGREGATEk({hu(k1):uN(v)})\mathbf{h}_{\mathcal{N}(v)}^{(k)} = \text{AGGREGATE}_k\left(\{\mathbf{h}_u^{(k-1)} : u \in \mathcal{N}(v)\}\right)

hv(k)=σ ⁣(W(k)CONCAT(hv(k1), hN(v)(k)))\mathbf{h}_v^{(k)} = \sigma\!\left(\mathbf{W}^{(k)} \cdot \text{CONCAT}\left(\mathbf{h}_v^{(k-1)},\ \mathbf{h}_{\mathcal{N}(v)}^{(k)}\right)\right)

AGGREGATE options: mean, max-pooling, or LSTM over sampled neighbors.

Key Advantages Over GCN

  • Inductive: train on one graph, predict on new graphs / new nodes (crucial for production)
  • Mini-batch training: samples a fixed-size neighborhood → scalable to millions of nodes
  • Flexible aggregation: max-pooling captures the most prominent neighbor features

When to Use GraphSAGE

Large graphs where nodes arrive over time (e.g., new users in a social network, new products in a catalog). Used in Pinterest's recommendation system (PinSage) — trained on 3 billion nodes.

GAT — Graph Attention Network

Veličković et al. (2018) — learns different attention weights for different neighbors.

hv(k)=σ ⁣(uN(v){v}αvu(k)W(k)hu(k1))\mathbf{h}_v^{(k)} = \sigma\!\left(\sum_{u \in \mathcal{N}(v) \cup \{v\}} \alpha_{vu}^{(k)} \mathbf{W}^{(k)} \mathbf{h}_u^{(k-1)}\right)

Attention coefficient:

αvu=exp ⁣(LeakyReLU(aT[WhvWhu]))wN(v)exp ⁣(LeakyReLU(aT[WhvWhw]))\alpha_{vu} = \frac{\exp\!\left(\text{LeakyReLU}(\mathbf{a}^T [\mathbf{W}\mathbf{h}_v \| \mathbf{W}\mathbf{h}_u])\right)}{\sum_{w \in \mathcal{N}(v)} \exp\!\left(\text{LeakyReLU}(\mathbf{a}^T [\mathbf{W}\mathbf{h}_v \| \mathbf{W}\mathbf{h}_w])\right)}

Multi-head attention applies KK independent attention heads and concatenates (or averages) their outputs.

Comparison of GNN Variants

ModelAggregationInductiveAttentionBest For
GCNNormalized meanNoNoSmall homophilic graphs
GraphSAGEMean / max / LSTMYesNoLarge-scale, dynamic graphs
GATWeighted by attentionYesYesHeterophilic graphs, interpretability
GINSum (most expressive)YesNoGraph classification, isomorphism
Node Classification with GCN + GAT (PyTorch Geometric)python
import torch
import torch.nn.functional as F
from torch_geometric.datasets import Planetoid
from torch_geometric.nn import GCNConv, GATConv

# --- Load Cora citation dataset ---
# 2708 nodes (papers), 5429 edges (citations), 7 classes, 1433 features
dataset = Planetoid(root='/tmp/Cora', name='Cora')
data = dataset[0]

print(f"Nodes: {data.num_nodes}, Edges: {data.num_edges}")
print(f"Node features: {data.num_node_features}, Classes: {dataset.num_classes}")
print(f"Train/Val/Test: {data.train_mask.sum()}/{data.val_mask.sum()}/{data.test_mask.sum()}")


# --- GCN Model ---
class GCN(torch.nn.Module):
    def __init__(self, in_channels, hidden_channels, out_channels):
        super().__init__()
        self.conv1 = GCNConv(in_channels, hidden_channels)
        self.conv2 = GCNConv(hidden_channels, out_channels)

    def forward(self, x, edge_index):
        x = F.relu(self.conv1(x, edge_index))
        x = F.dropout(x, p=0.5, training=self.training)
        return self.conv2(x, edge_index)


# --- GAT Model ---
class GAT(torch.nn.Module):
    def __init__(self, in_channels, hidden_channels, out_channels, heads=8):
        super().__init__()
        self.conv1 = GATConv(in_channels, hidden_channels, heads=heads, dropout=0.6)
        self.conv2 = GATConv(hidden_channels * heads, out_channels, heads=1, concat=False, dropout=0.6)

    def forward(self, x, edge_index):
        x = F.dropout(x, p=0.6, training=self.training)
        x = F.elu(self.conv1(x, edge_index))
        x = F.dropout(x, p=0.6, training=self.training)
        return self.conv2(x, edge_index)


def train_eval(model_cls, epochs=200, **kwargs):
    model = model_cls(dataset.num_node_features, 64, dataset.num_classes, **kwargs)
    optimizer = torch.optim.Adam(model.parameters(), lr=0.005, weight_decay=5e-4)

    for epoch in range(epochs):
        model.train()
        optimizer.zero_grad()
        out = model(data.x, data.edge_index)
        loss = F.cross_entropy(out[data.train_mask], data.y[data.train_mask])
        loss.backward()
        optimizer.step()

    model.eval()
    with torch.no_grad():
        pred = model(data.x, data.edge_index).argmax(dim=1)
        acc = (pred[data.test_mask] == data.y[data.test_mask]).float().mean()
    return acc.item()

gcn_acc = train_eval(GCN)
gat_acc = train_eval(GAT)

print(f"\nGCN Test Accuracy:  {gcn_acc:.4f}")
print(f"GAT Test Accuracy:  {gat_acc:.4f}")
# Expected: GCN ~0.81, GAT ~0.83 on Cora

Real-World Applications

Fraud Detection

  • Nodes: bank accounts, merchants, devices
  • Edges: transactions, shared attributes
  • Task: node classification — is this account fraudulent?
  • GNNs catch fraud rings (coordinated groups) invisible to per-account models
  • PayPal, Alibaba, and Amazon use GNN-based fraud detection in production

Drug Discovery & Molecular Property Prediction

  • Nodes: atoms, Edges: bonds
  • Task: graph classification — will this molecule inhibit a target protein?
  • GNNs (e.g., MPNN, D-MPNN) outperform fingerprint-based methods
  • Used in COVID-19 drug screening (MIT, Pfizer, DeepMind's AlphaFold context)

Recommendation Systems

  • Nodes: users + items (bipartite graph)
  • Edges: interactions (clicks, purchases, ratings)
  • Task: link prediction — will user U interact with item I?
  • LightGCN (2020): simplified GCN without feature transformation — state of the art on RecSys benchmarks
  • PinSage (Pinterest): GraphSAGE at 3B nodes, serving billions of recommendations daily

Knowledge check

A GNN is trained on a citation network to classify research papers. After training, new papers are published and need to be classified without retraining. Which architecture handles this best?

Summary

  • Graphs represent relational data — social networks, molecules, transactions, knowledge graphs
  • All GNNs follow message passing: iteratively aggregate neighbor features → capture K-hop context
  • GCN: simple, effective, normalized mean aggregation — best for small homophilic graphs
  • GraphSAGE: inductive, scalable with neighbor sampling — best for large dynamic graphs
  • GAT: attention-weighted aggregation — best when neighbor importance varies
  • Real applications: fraud detection, drug discovery, recommendation systems, knowledge graph completion

Next: Bayesian Machine Learning — probabilistic reasoning and uncertainty quantification.

ML Fundamentals