SPATIO-TEMPORAL GRAPHS /// TRAFFIC PREDICTION /// STGCN /// PYTORCH GEOMETRIC /// METR-LA DATASET /// MESSAGE PASSING ///

Traffic Mapping

Module 3.5: Master Spatio-Temporal Graph Neural Networks. Learn to map road topologies and predict congestion using PyTorch Geometric.

stgcn_model.py
1 / 8
12345
πŸ—ΊοΈ

Tutor:Predicting traffic requires understanding two dimensions: Space (how roads connect) and Time (historical speed data).


Architecture Tree

EXPAND NETWORK TOPOLOGY.

Spatial Features

Captures the topology of the road network using Graph Convolutions based on an Adjacency Matrix.

System Check

What structure defines which roads are connected to each other?


Graph Community Hub

Share Your Models

ACTIVE

Successfully predicted traffic on the PeMS dataset? Share your PyG configurations and discuss architectures!

Traffic Prediction with Spatio-Temporal GNNs

Author

Pascual Vila

AI Architecture Lead // Code Syllabus

"Predicting traffic is not just about knowing the history of a single road; it is about understanding the ripple effect of congestion through a complex network."

Why CNNs Fail for Traffic

Traditional Convolutional Neural Networks (CNNs) are exceptional at processing Euclidean dataβ€”like pixels in an image arranged in a strict 2D grid. However, road networks are non-Euclidean. An intersection might connect to two, three, or five different roads of varying lengths. Applying standard grid convolutions to traffic maps results in massive spatial inaccuracies.

Enter the Spatio-Temporal Graph

We model road networks as graphs. Nodes represent traffic sensors or intersections, and Edges represent the road segments connecting them. The strength of the connection is often defined by distance in an Adjacency Matrix.

But traffic changes over time. Therefore, each node carries a time-series of features (e.g., speed, volume). This creates a Spatio-Temporal Graph.

STGCN Architecture

The Spatio-Temporal Graph Convolutional Network (STGCN) elegantly solves this by alternating between two operations:

  • Spatial Convolution: A GNN layer (like GCN or GraphSAGE) aggregates traffic states from neighboring intersections to understand local bottlenecks.
  • Temporal Convolution: A 1D Convolution (or an RNN/GRU) processes the historical sequence of traffic data within each specific node to capture trends and periodicities.

πŸ€– Machine Learning FAQ

How do Graph Neural Networks (GNNs) predict traffic?

GNNs predict traffic by treating the road network as a mathematical graph. They utilize "Message Passing" to allow nodes (intersections) to share their traffic state (e.g., speed, vehicle count) with connected neighbors. By combining this spatial data with temporal layers (analyzing historical data), the model learns how congestion propagates through the network over time.

What datasets are used for Traffic Prediction with GNNs?

The most standard benchmark datasets for Spatio-Temporal Graph Neural Networks in traffic are METR-LA (traffic speed statistics collected from loop detectors in Los Angeles) and PeMS-BAY (traffic data collected by the California Transportation Agencies). Both provide the adjacency matrices and historical time-series required for STGCN models.

What is the Adjacency Matrix in a traffic graph?

The Adjacency Matrix represents the physical layout of the roads. It is an N x N matrix (where N is the number of sensors). If a road connects sensor A directly to sensor B, the matrix holds a value greater than 0 (often calculated using a Gaussian kernel based on the physical distance between them). This allows the GNN to know which nodes should pass messages to each other.

GNN Glossary

Adjacency Matrix
A square matrix used to represent a finite graph, showing which nodes are adjacent (connected) to which other nodes.
model.py
Spatio-Temporal Graph
A graph structure where nodes and edges have static spatial locations, but node features change dynamically over time.
model.py
STGCN
Spatio-Temporal Graph Convolutional Network. A model combining spatial GNNs and temporal CNNs/RNNs.
model.py
Message Passing
The core mechanism of GNNs where nodes aggregate feature vectors from their direct neighbors to update their own state.
model.py