GNN for Beginners: Understanding Core Principles Through Student Grade Prediction

Using student grade prediction as an example, this article explains how GNNs leverage neighbor and multi-hop information to surpass ordinary neural networks.
The article uses the running example of predicting final exam scores from midterm scores to break down GNN core mechanisms. A basic neural network looks only at individual students, ignoring study group information, resulting in high error; a single-layer GNN incorporating neighbors' average scores reduces error to near zero. A random-edge baseline further shows that the performance gain mainly comes from genuine study partner relationships. Two-layer GNNs propagate information across two hops, with layer count tuned via validation sets, often showing an error plateau. The article also explains GNN training/validation peculiarities—strictly distinguishing available information to avoid label leakage—and introduces applications like node embeddings and study group recommendations, finally looking ahead to weighted edges and attention mechanisms.
Title
GNN for Beginners: Understanding Core Principles Through Student Grade Prediction
Meta Description
Using student grade prediction as an example, this article explains the core principles of Graph Neural Networks (GNN): from single-layer GNNs that incorporate neighbor information and random-edge baselines, to two-layer GNNs that enable two-hop information propagation, and discusses layer selection, training/validation peculiarities, and practical applications like embeddings and study group recommendations.
Article Content
Graph Neural Networks (GNNs) sit at the intersection of graph theory and neural networks, making them a highly practical direction in modern data science. This article uses a running example—predicting students' final exam scores from their midterm scores—to clearly break down the core mechanisms of GNNs and explain why they outperform ordinary neural networks.
Starting with a Failed Prediction
Suppose you are a math professor who wants to predict final exam scores using students' midterm scores. The most straightforward approach is to apply a basic neural network: map the midterm score M through weights and activation functions to a hidden state H, then map from the hidden state to a prediction. The model's parameters are learned during training—mathematically, it's just "an activation function applied to a linear combination of inputs."
However, after training you'll find the error is unreasonably high. The reason lies in the classroom itself: students are not isolated entities—they form study groups, share knowledge and information within groups, and barely communicate between groups. Looking only at an individual student's score means discarding this crucial information.
This is exactly the problem GNNs are designed to solve—using not just "who you are" but also "who you study with" to make predictions.
Single-Layer GNN: Incorporating Neighbor Information
Mathematically, a single-hidden-layer GNN is not much different from a basic network. We still use the student's own midterm score, but additionally introduce the average midterm score of their study partners (neighbors), M_N. Both pieces of information are transformed into a hidden state through a familiar formula—the only change is an added linear combination term for the neighbors' average score, which means one more parameter during training.

The difference is quite striking: the basic neural network has high error, while the single-layer GNN's error is driven down to near zero.
A Crucial Control Experiment: Random-Edge Baseline
Before presenting the results, there's a key control worth noting: what happens if we train the same GNN but randomly shuffle all the edges in the student study graph? This baseline serves to rule out an alternative explanation—that the model improved simply because it incorporated other students' midterm scores, rather than genuinely leveraging the actual connections between students.
The experimental results: the random-edge baseline does outperform the basic neural network, showing that "incorporating others' information" has value in itself; but it performs noticeably worse than the GNN with real connections preserved. Students' actual study partner relationships are the main source of performance improvement.
Random-edge baseline is a commonly used ablation experiment in graph machine learning: keeping node features and model hyperparameters unchanged, only randomly rewiring the graph's edges (typically preserving degree distribution or edge count), then training and evaluating through the same pipeline. Its purpose is to separate "information from the graph structure itself" from "gains brought by additional features or more parameters." If the random-edge model performs comparably to the real-graph model, it indicates the performance gain does not come from genuine connections; if the real graph is clearly better, it indicates the edges indeed carry task-relevant signal. This baseline is also widely adopted in tasks like node classification and link prediction, and is the minimum threshold for judging whether a GNN truly "uses the graph."
Two-Layer GNN: Information Propagates Along the Graph
A reasonable challenge at this point: aside from constructing a new feature, the above approach is just an ordinary neural network—what justifies calling it a separate field? To answer this, we need to extend from one hidden layer to two.
A single-layer GNN lets information flow from your one-hop neighbors (direct study partners) to you. A two-layer GNN further incorporates "your study partners' study partners"—i.e., nodes at two hops away from you in the graph. The assumption is intuitive: partners you study with daily have the greatest influence on you, and these partners are themselves learning from their partners—information that can indirectly help you.

Mathematically, the operation is: for each neighbor, take the average of their first-layer hidden vectors, pass each through its own learned linear combination, obtain contributions from self and neighbors, sum them and apply an activation function to get the second-layer hidden vector. The key is that the mean of neighbors' first-layer vectors is itself a function of their neighbors—adding one more layer allows information to travel two hops to reach you. For simplicity, constant terms like biases are omitted from the diagram; they too are learned during training.
How to Choose the Number of Layers
The number of layers (i.e., how many hops of neighbors to consider) is itself a hyperparameter to tune, determined using the training set and validation set. In practice, an error plateau typically emerges: one layer beats the basic network, two layers improves further, but adding a third layer yields no significant benefit—making two layers the reasonable choice.

How GNN Training and Validation Differ
An ordinary neural network only needs to be trained on some data points and validated on a held-out set. But GNN data is highly interconnected through edges, so it's essential to clearly define which information is available and which is not.
Consider three consecutive cohorts of students: the first year has complete information—study relationships, midterm scores, final scores—used to train the model; the second year provides only study relationships and midterm scores, with final scores hidden, to test whether the model can predict accurately and to compute validation error. Once validation is satisfactory, the model can be deployed in practice.
Label leakage refers to the target variable indirectly entering the model's visible information during training or validation, through features, edges, or data partitioning. It is particularly insidious on graph data: because nodes are interconnected through edges, if a validation node's neighbors include training nodes, and those training nodes' labels are used to construct features or message passing, validation metrics will be overestimated. Common safeguards include partitioning training/validation sets by time or by connected components, masking validation node labels during training, and evaluating under both transductive and inductive settings. The "hiding second-year final scores" approach described in the article is essentially cutting off the path of label propagation along edges.
What Can Be Done After Training
The most direct use is, of course, predicting final exam scores. But there are two easily overlooked applications.
First, embeddings: after training, each student obtains a learned vector representation that can serve as a starting point for other related tasks.
Second, helping students without study groups choose partners. By inputting an isolated student's midterm score and a hypothetical connection into the trained GNN, you can immediately generate predictions of "what would the final score be if they joined the green group, blue group, or purple group." If the goal is simply to maximize scores, choose the group connection that yields the highest predicted value.

Node embedding is the result of representation learning that maps each node to a low-dimensional dense vector, encoding the node's features and local graph structure information. The hidden layer outputs of a trained GNN can naturally serve as embeddings: nodes with similar structure or similar neighbors tend to have closer vectors in space. Embeddings are not limited to the original task—they can serve as input features for downstream tasks, and also for clustering, similarity retrieval, and visualization. Unlike structure-only embedding methods like DeepWalk and node2vec, GNN-produced embeddings also incorporate the node's own features, making them typically more expressive in feature-rich scenarios.
How Much Further Can We Go
The above model makes a simplifying assumption: all edges have equal weight. In reality, you spend far more time studying with some people than others—this information can be encoded into edges to improve prediction, and can also learn representations for the edges themselves, not just nodes. Another natural extension is introducing attention layers, letting the model learn which study partners are more important—currently we simply average over all partners, but different academic personalities clearly bring different value.
Whether it's students in a math class, flights between cities, or friendships in a book club, a great many real-world objects are well modeled as sets of nodes and edges. Incorporating graph structure information into models often opens up problem spaces that were previously closed.
One-line Summary
Using student grade prediction as an example, this article clearly explains how GNNs leverage neighbor and multi-hop information to outperform ordinary neural networks.
Paragraph Summary
The article uses the running example of "predicting final exam scores from midterm scores" to break down the core mechanisms of graph neural networks. A basic neural network looks only at individual students, ignoring the information brought by study groups, resulting in high error; a single-layer GNN incorporating neighbors' average scores reduces error to near zero. A random-edge baseline experiment further shows that the performance improvement mainly comes from genuine study partner relationships, not merely from incorporating others' information. Two-layer GNNs allow information to propagate across two hops, and the number of layers needs to be tuned via validation sets, typically showing an error plateau. The article also explains the peculiarities of GNN training and validation—strictly distinguishing available information to avoid label leakage—introduces applications like node embeddings and study group recommendations, and finally looks ahead to extensions with weighted edges and attention mechanisms.
Related articles

One Year of Dioxus Fullstack in Production: Gains and Pain Points
A developer shares a year of real-world experience running the Dioxus fullstack framework in production, covering type-safety gains in fullstack Rust, plus pain points like compile times and ecosystem maturity, as a reference for technology selection.

The Hidden Faith of AGI Leaders: Humans Are Nothing Special?
Vergecast podcast reveals the hidden belief within AGI leadership circles: humans are nothing special, consciousness is just computation. Exploring how computational functionalism shapes the worldview of tech elites and the future of AI.

GM Shifts Toward Hybrids: From an All-Electric Bet to a Pragmatic Parallel Strategy
GM once skipped hybrids to bet on pure EVs; now it plans hybrid SUV versions. This article analyzes the strategic shift, its significance for SUVs, and consumer impact.