Skip to main content
Graph Neural Networks

Graph Neural Networks

Why Graphs?

Most neural networks assume data lives on a grid: images are 2D grids of pixels, text is a 1D sequence of tokens, audio is a 1D sequence of samples. But much of the real world does not fit neatly on a grid. A social network is not a grid. A molecule is not a sequence. A road network is not a rectangle. This is where graphs come in. A graph is simply a collection of nodes (things) connected by edges (relationships). This deceptively simple structure can represent an enormous range of real-world data:
  • Social networks: Users (nodes) and friendships (edges)
  • Molecules: Atoms (nodes) and chemical bonds (edges)
  • Knowledge graphs: Entities (nodes) and relations (edges)
  • Citation networks: Papers (nodes) and references (edges)
  • Traffic: Intersections (nodes) and roads (edges)
Graphs capture relational structure that traditional neural networks miss entirely. A standard MLP fed a molecule’s atoms as a flat vector has no idea which atoms are bonded to which. A GNN knows.
If you are new to GNNs, start with PyTorch Geometric (PyG). It provides optimized implementations of all the architectures discussed below, handles batching of variable-size graphs, and includes standard benchmark datasets. Building GNNs from scratch (as we do here for pedagogical purposes) is valuable for understanding, but use PyG for production work.

Graph Fundamentals

Graph Representation


Message Passing Framework

The foundation of all GNNs. The core idea is beautifully simple: each node updates its representation by collecting information from its neighbors. Think of it like a game of telephone, but structured — at each round, every person asks their friends “what do you know?”, gathers the answers, and updates their own understanding. After a few rounds, each person has absorbed information from an increasingly wide neighborhood of the network: hv(k+1)=UPDATE(hv(k),AGGREGATE({hu(k):uN(v)}))h_v^{(k+1)} = \text{UPDATE}\left(h_v^{(k)}, \text{AGGREGATE}\left(\{h_u^{(k)} : u \in \mathcal{N}(v)\}\right)\right)

Graph Convolutional Network (GCN)

GCN is the “ResNet of graph learning” — a simple, strong baseline that most practitioners reach for first. The idea: each node’s new representation is a weighted average of its neighbors’ features (including itself), passed through a linear transform and activation. The weighting is based on node degree, which prevents high-degree nodes from dominating.

Graph Attention Network (GAT)

GCN treats all neighbors equally (modulo degree normalization). But in practice, some neighbors are more relevant than others — your best friend’s opinion matters more than an acquaintance’s. GAT addresses this by learning attention weights for each edge, so the model can decide how much to listen to each neighbor.

GraphSAGE

GCN and GAT require the full graph in memory during training, which breaks down for graphs with millions or billions of nodes (think: the entire Facebook social graph). GraphSAGE solves this with a simple but powerful idea: instead of using all neighbors, sample a fixed number of neighbors at each layer. This makes mini-batch training possible on arbitrarily large graphs.

Graph Isomorphism Network (GIN)

GIN asks the theoretical question: how powerful can message-passing GNNs actually be? The answer turns out to be: at most as powerful as the Weisfeiler-Lehman (WL) graph isomorphism test, a classical algorithm for checking if two graphs are structurally identical. GIN achieves this theoretical maximum expressiveness. If you need your GNN to distinguish between subtly different graph structures (common in molecular property prediction), GIN is your tool.

Graph Pooling

Just as CNNs use pooling to reduce spatial resolution and build hierarchical features, GNNs need pooling to reduce the number of nodes and create graph-level representations. The challenge is that graphs have irregular structure — you cannot just “stride 2” across a graph. Graph pooling methods learn which nodes to keep and which to merge, creating a coarser version of the original graph.
Over-smoothing is the silent killer of deep GNNs. After about 5-6 message-passing layers, all node representations converge to nearly the same vector — the GNN equivalent of blurring an image until everything is grey. If your validation accuracy drops when you add more layers, over-smoothing is the likely culprit. Mitigations include skip connections (like JKNet), DropEdge during training, or graph pooling to reduce the effective depth.

Practical Application: Node Classification

The classic benchmark for GNNs is semi-supervised node classification on the Cora citation network: given a graph of academic papers (nodes) with citation links (edges), classify each paper into one of 7 research topics using only the labels of 20 papers per class (140 total out of 2,708). This is a remarkably data-efficient setting — the graph structure carries so much information that you can classify most unlabeled nodes by labeling just a handful.

GNN Best Practices


Exercises

Extend GCN to use edge features in the convolution:
Build a model for graph-level classification:
  • Use GIN layers
  • Implement graph-level readout
  • Test on molecular property prediction

What’s Next?

3D Deep Learning

Point clouds, voxels, and meshes

Object Detection

YOLO, Faster R-CNN, DETR