Graph Neural Networks and DGL: A Beginner's Guide

Jul 10, 2026·
Yassir Boulaamane
Yassir Boulaamane
· 8 min read

Most machine learning architectures operate on grid-structured inputs, such as tables or image pixel grids. However, molecular structures, citation networks, social graphs, and transport networks are structured as irregular graphs. Graph Neural Networks (GNNs) learn representation vectors directly from this non-Euclidean topology without requiring grid projection.

This guide provides an overview of GNN concepts, maps these structures to the Deep Graph Library (DGL), and defines key terminology.


1. What is a graph?

A graph is just two things: a set of nodes (the entities) and a set of edges (the connections between them). A citation network is papers connected by “cites” relationships. A molecule is atoms connected by bonds. A social network is people connected by friendships.

Graph TopologyABCDGraph Elements• Nodes (V) = {A, B, C, D} - Represent entities• Edges (E) = {(A,B), (A,C), (B,C), (B,D), (C,D)} - ConnectionsDegree & Neighborhood• Degree: How many edges touch a node (e.g., Node B degree = 3)• Neighborhood: Directly connected nodes (e.g., N(A) = {B, C})Adjacency Matrix (A)A_ij = 1 if edge exists else 0

Key terminology:

  • Directed vs. undirected: An edge can point one way ($A \rightarrow B$) or both ways. DGL edges are directed by default; an undirected graph is represented by defining edges in both directions.
  • Degree: The number of edges connected to a node (such as Node B degree = 3).
  • Neighborhood: The set of nodes directly connected to a given node. Node A’s neighborhood is $\{B, C\}$.
  • Adjacency matrix: A matrix representation of graph topology where cell $i, j$ is 1 if there is an edge from node $i$ to node $j$, and 0 otherwise. DGL stores this matrix in a compressed sparse format to optimize memory.

Nodes and edges hold associated feature vectors (g.ndata and g.edata). For example, an atomic node feature vector may encode element type and charge, while an edge feature vector represents bond characteristics.


2. Message Passing Workflows

GNN layers update node representations through a three-step message-passing sequence:

GNN Layer Message Passing Recipe1. Message StepNeighbors send features along edgesu₁u₂vmsg₁msg₂2. Aggregate StepCollect & combine (sum/mean/max)∑m_v = Aggregate({msg})3. Update StepCompute new target representationvh_v^(l) = Update(h_v^(l-1), m_v)Updated Feature!
  1. Message: Every node sends information along its edges, usually just its current feature vector or a transformed version of it.
  2. Aggregate: Every node collects the messages arriving from its neighbors and combines them, commonly by summing, averaging, or taking the maximum.
  3. Update: Every node combines the aggregated message with its own previous feature to produce a new feature.

In DGL, this whole cycle is executed in a single call to g.update_all(), using either a built-in message/reduce function or ones you write yourself. Stack this three-step recipe into multiple layers, and each node’s final representation encodes information from further and further away in the graph.


3. Stacking layers: how far can a node "see"?

A single message-passing layer only lets a node see its immediate neighbors, its 1-hop neighborhood. Stack a second layer, and information from those neighbors’ neighbors flows in too, representing a 2-hop neighborhood. A GNN with $K$ layers lets every node see $K$ hops away.

GNN Layer Stacking: Receptive Field & Over-smoothing1-Hop (Layer 1)Can see direct neighborsw₁w₂u₁u₂u₃u₄v2-Hop (Layer 2)Can see neighbors' neighborsw₁w₂u₁u₂u₃u₄vK-Hop (Over-smoothing)Too many layers = representations mergeAll nodes look identical!

Stacking GNN layers increases the receptive field but can introduce performance degradation. When layer count ($K$) is high, node representations tend to converge and become indistinguishable, a phenomenon termed over-smoothing. Practical applications typically use 2 to 4 layers.


4. Neighborhood Aggregation Functions: GCN, GAT, GraphSAGE

GNN architectures differ primarily in their neighborhood aggregation functions:

ArchitectureAggregation MechanismCharacteristics
GCN (Graph Convolutional Network)Fixed weighting based on node degree; every neighbor contributes proportionally.The classic starting point; strong with just 2 layers.
GAT (Graph Attention Network)Learned attention score per neighbor, so more relevant neighbors count more.Multi-head attention runs several of these in parallel, then combines them.
GraphSAGESamples a fixed number of neighbors per layer instead of using all of them.Built for huge graphs and for adding new nodes after training (inductive learning).

5. Downstream Prediction Tasks

Node representations support three primary downstream prediction tasks:

Three Core GNN Prediction TasksNode ClassificationPredict properties of individual nodes?Class: ActiveLink PredictionPredict if edge should exist between nodesp(Edge) = 0.92Graph ClassificationPool all node features for whole-graph labelPOOLVectorLabel: Mutagenic
  • Node classification: Predict a label for each node. Example: Is this citation-network paper about biology or physics?
  • Link prediction: Predict whether an edge should exist between two nodes. It is trained using negative sampling, feeding the model real edges alongside randomly sampled non-edges so it learns to tell them apart.
  • Graph classification: Predict a label for an entire graph. Example: Is this molecule toxic? Since graphs vary in size, you need a readout (or pooling) step, typically summing or averaging all node embeddings into one fixed-size vector before classifying.

6. Training on graphs too big for GPU memory

Real graphs (like a full citation network or a social graph) can have millions of nodes - far more than can fit on a GPU at once. The fix is the same one used everywhere else in deep learning: mini-batches. But you can’t just grab a random slice of a graph, since a node’s prediction depends on its neighbors.

Neighbor sampling solves this: instead of using all of a node’s neighbors, sample a fixed number at each layer. This produces a small, bounded computational subgraph per training step instead of the whole graph.

Neighbor Sampling for Large-Scale Trainingu₃u₄u₅u₁u₂TargetHow it works:• Mini-batching raw graphs is hard due to dependencies.• Solution: Sample a small, fixed number of neighborsper node (such as sample size = 2).• Unsampled nodes (u₃, u₄, u₅) and their edges are ignored.• This bounds memory size and allows GPU mini-batch trainingon graphs with billions of nodes.

The DGL GraphBolt framework implements this pipeline:

  1. An ItemSampler selects target nodes.
  2. A SubgraphSampler extracts local neighborhoods.
  3. A FeatureFetcher retrieves corresponding node and edge attributes.
  4. The result is a MiniBatch object that flows through training modules.

This modular design enables parallelized training across multi-GPU and distributed systems.


7. DGL Tutorial Pathways

Official DGL tutorials expand on these methodologies:

SeriesScopeTarget Audience
Blitz IntroductionNode classification, the DGLGraph object, writing custom message-passing layers, link prediction, graph classification, custom datasetsIntroductory reference.
Stochastic Training / GraphBoltNeighbor sampling, mini-batch node and link classification, multi-GPU training, building an OnDiskDatasetLarge-scale datasets.
Graph TransformerPositional encodings, multi-head attention over graphsAdvanced transformer architectures.
dgl.sparseExpressing GNNs as sparse linear algebra, graph diffusion, hypergraphsMatrix-based formulations.
CPU TrainingMulti-core scaling with ARGO, CPU tuningHardware-specific optimization.
Multi-GPU TrainingDistributed training on one machineMulti-GPU scale-up.
Distributed TrainingTraining across multiple machinesMulti-machine distributed systems.
Paper StudyFaithful DGL implementations of GCN, R-GCN, GAT, Tree-LSTM, DGMG, and Capsule NetworksReference implementations for replication studies.

Glossary

Core Graph Concepts

TermMeaning
GraphNodes connected by edges, written $G = (V, E)$.
Node / VertexA single entity in the graph.
EdgeA connection between two nodes; directed by default in DGL.
Heterogeneous GraphA graph with more than one type of node and/or edge.
HypergraphA graph where one edge can connect more than two nodes.
SubgraphA smaller graph formed from a subset of nodes and edges.
Adjacency MatrixThe graph written as a grid of 0s and 1s.
DegreeHow many edges touch a node.
NeighborhoodThe nodes directly connected to a given node.

GNN Concepts

TermMeaning
Message PassingSend $\rightarrow$ aggregate $\rightarrow$ update; the core GNN computation.
Node/Edge FeatureA vector of numbers describing a node or edge.
EmbeddingA learned vector representation of a node, edge, or graph.
GNN LayerOne round of message passing; $K$ layers reach $K$ hops.
Over-smoothingToo many layers make all node representations converge.
GCN / GAT / GraphSAGEThree popular message-passing architectures.
Readout / PoolingCombining all node embeddings into one graph-level vector.
Node / Link / Graph ClassThe three main prediction tasks on graphs.

Training Concepts

TermMeaning
Mini-batch TrainingTraining on a small random subset instead of the whole graph.
Neighbor SamplingSampling a fixed number of neighbors per layer to bound batch size.
Negative SamplingAdding fake edges as negative examples for link prediction.
Computational SubgraphThe subgraph actually needed to compute one mini-batch.
Positional EncodingExtra node features (such as Laplacian eigenvectors) encoding position in Graph Transformers.

DGL-Specific Terms

TermMeaning
DGLGraphDGL’s core graph object, holding topology plus ndata/edata.
GraphBoltDGL’s pipelined framework for large-graph training.
MiniBatchThe bundled object carrying a sampled subgraph through the pipeline.
OnDiskDatasetA GraphBolt dataset format for graphs larger than RAM.
dgl.sparse.SparseMatrixDGL’s sparse matrix class for matrix-based graph operations.
update_all()DGL’s batched message-passing call.
DistGraphDGL’s distributed graph object for multi-machine training.