HomeArticlesGraph Neural Networks: Learning on Relational Data

Graph Neural Networks: Learning on Relational Data

Most of the world's richest data — molecules, social networks, road maps, recommendation histories — doesn't sit neatly on a grid; it sprawls across nodes and connections of wildly varying shape. Graph neural networks were built to learn from exactly that kind of tangled, relational structure, by teaching every node to listen to its neighbors.

mysimulator teamUpdated June 2026≈ 8 min read▶ Open the simulation

Why Grids Don't Work on Graphs

Convolutional neural networks owe their success to a simple regularity: an image is a fixed grid of pixels, so a small filter can slide across it in a consistent, well-defined way, and every pixel has the same number of neighbors in the same relative positions. Graphs offer no such luxury. A social network might have one person with three friends and another with three thousand; a molecule might have a carbon atom bonded to four neighbors and a hydrogen bonded to just one. There is no natural ordering of a node's neighbors and no fixed neighborhood size, so the sliding-window trick that makes convolution so effective on images simply has nowhere to grip. Graph neural networks (GNNs) were designed from the ground up to handle this irregularity, treating variable-sized, unordered neighborhoods as a first-class citizen rather than an inconvenience to be reshaped away.

Message Passing: Every Node Listens to Its Neighbors

The core computational idea behind almost every modern GNN is message passing. Each node starts with some initial feature vector — perhaps an atom's element type, or a user's profile attributes — and then, in a single round, every node gathers messages from its immediate neighbors, combines those messages together, and uses the result to update its own representation. Concretely, a node sends a transformed version of its current features along each edge, the receiving node aggregates all incoming messages into one summary vector, and a small neural network merges that summary with the node's own previous state to produce an updated embedding. After this step, every node's representation reflects not just its own attributes but a distilled sense of who it is connected to, which is exactly the kind of relational context that grid-based networks have no way to capture.

Stacking Layers to Reach Further Across the Graph

A single round of message passing only lets a node see its immediate, one-hop neighbors. Stack a second layer on top, and each node now aggregates from neighbors who have themselves already absorbed information from their own neighbors, so its effective view expands to two hops away. Stacking k layers lets information travel k hops across the graph, gradually widening each node's receptive field the same way stacking convolutional layers widens the receptive field over an image. This is a powerful lever, but it comes with a well-known trap called over-smoothing: stack too many layers, and every node's representation ends up blending in information from so much of the graph that distinct nodes start to look nearly identical, washing out the very structure the network was supposed to learn. In practice, most GNNs used for real tasks stay fairly shallow, typically two to six layers, striking a balance between reaching distant context and preserving each node's individual identity.

Aggregation Functions and Permutation Invariance

The step where a node combines messages from all its neighbors into one vector is the aggregation function, and the choice matters. Sum aggregation preserves information about how many neighbors contributed, which can be theoretically the most expressive choice; mean aggregation normalizes by neighborhood size, which tends to be more stable across nodes with very different numbers of connections; and max aggregation picks out the single most salient neighbor signal, which can be useful for detecting the presence of a specific pattern regardless of how common it is. Whichever function is chosen, it must satisfy one non-negotiable property: permutation invariance. A node's neighbors don't come in any inherent order, so feeding them into the aggregation function in a different sequence must produce exactly the same result. Sum, mean and max all naturally satisfy this, which is precisely why they, rather than order-sensitive operations, dominate GNN design; without permutation invariance, the same graph could yield a different answer just because its neighbor list happened to be written down in a different order.

From Molecules to Social Graphs

Because so much real-world data is naturally relational, GNNs have found traction across a striking range of domains. In drug discovery and chemistry, a molecule is modeled as a graph of atoms connected by bonds, and a GNN can predict properties like solubility, toxicity, or binding affinity directly from that structure, dramatically accelerating the early stages of candidate screening compared to hand-engineered chemical descriptors. In social network analysis, GNNs power tasks like predicting which users are likely to become friends, detecting coordinated fake-account clusters, or classifying a user's likely interests by blending their own profile with signals absorbed from their connections. Recommendation systems increasingly frame the interaction between users and items as a bipartite graph, using message passing to let a product's embedding be shaped by everyone who bought it, and a user's embedding be shaped by everything they've bought, producing recommendations that capture collaborative patterns far richer than simple similarity scores. Traffic forecasting, fraud detection, and even protein structure prediction all lean on the same underlying idea: let structure inform representation.

Frequently asked questions

How is a GNN different from a regular convolutional neural network?

A CNN relies on the fixed, regular grid structure of images, where every pixel has a consistent number of neighbors in predictable positions, letting the same filter slide uniformly across the input. A GNN is built for graphs, where nodes can have wildly different numbers of neighbors with no natural ordering among them, so instead of sliding filters it uses permutation-invariant message passing to aggregate neighbor information regardless of how many neighbors there are or what order they appear in.

What does 'over-smoothing' mean and why does it limit how deep GNNs can go?

Over-smoothing happens when too many message-passing layers are stacked, causing every node's representation to repeatedly blend in information from an ever-widening share of the graph until distinct nodes become nearly indistinguishable from one another. Because each additional layer expands a node's receptive field by one more hop, deep GNNs risk erasing the very local structure they're meant to capture, which is why most practical GNNs stay relatively shallow, often just two to six layers.

Why does the choice between sum, mean, and max aggregation matter?

Each aggregator preserves different information: sum retains a sense of neighborhood size and total signal strength, mean normalizes for comparability across nodes with very different numbers of neighbors, and max isolates the single strongest neighbor signal. The right choice depends on the task, but all three share the essential property of permutation invariance, so the result never depends on the arbitrary order in which neighbors are processed.

Can GNNs handle edges with their own features, like bond types or friendship strength?

Yes. Many GNN variants extend plain message passing so that the message sent along an edge is shaped not just by the sender node's features but also by features attached to the edge itself, such as a chemical bond type, a distance, or an interaction weight. This lets the network distinguish, for example, a single bond from a double bond, or a close friendship from a casual acquaintance, rather than treating every connection identically.

Do GNNs need labeled data for every node to learn something useful?

Not necessarily. While many GNNs are trained for supervised tasks like predicting a molecule's toxicity, self-supervised and unsupervised approaches can also learn useful node embeddings by predicting graph structure itself, such as whether an edge exists between two nodes, which is especially valuable in domains like social networks where only a small fraction of nodes may carry explicit labels.

Try it live

Everything above runs in your browser — open Graph Neural Networks: Learning on Relational Data and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open Graph Neural Networks: Learning on Relational Data simulation

What did you find?

Add reproduction steps (optional)