Back to Articles Machine Learning • 26 min read

Introduction to Graph Neural Networks (GNNs)

Connected nodes visualization

Most traditional machine learning models process grids or sequences of data — like 2D pixels in images or 1D arrays of tokens in text. However, much of the real world is structured as graphs: molecules, social connections, computer networks, and financial transactions. Graph Neural Networks (GNNs) are built to learn representation vectors directly from these irregular, non-Euclidean data structures.


1. What is a Graph? Mathematical Definition

A graph is defined mathematically as \(G = (V, E)\), where \(V\) is the set of vertices (or nodes) and \(E\) is the set of edges (or connections) connecting those nodes. Nodes and edges typically carry feature vectors that represent their characteristics.

To process graphs numerically, we represent the network structure using an **Adjacency Matrix** \(A\), which is a binary matrix of size \(N \times N\) (where \(N\) is the number of nodes). If node \(i\) is connected to node \(j\), the matrix element \(A_{ij} = 1\); otherwise, \(A_{ij} = 0\). Additionally, we define a **Degree Matrix** \(D\), which is a diagonal matrix containing the number of connections (degree) for each node.

A major challenge in graph processing is **permutation invariance**. Because nodes in a graph do not have a natural sequence or ordering, changing the row and column ordering of the adjacency matrix must not change the output of the neural network.


2. The Message Passing Framework

The core mechanism of GNNs is the message passing loop. In each training layer, every node gathers information from its immediate neighbors, combines this collected information with its own current features, and updates its representation vector.

This process is formally broken down into three steps:

  1. Message Phase: Calculate the message vector coming from neighbor node \(j\) to target node \(i\).
  2. Aggregation Phase: Combine the message vectors from all direct neighbors using a permutation-invariant function (like sum, mean, or max).
  3. Update Phase: Merge the aggregated neighbor information with the node's original feature vector to compute the new representation state.

The formula for updating the hidden state \(h_i^{(l)}\) of node \(i\) at layer \(l\) is represented as:

h_i^{(l+1)} = \text{Update} \left( h_i^{(l)}, \text{Aggregate}_{j \in \mathcal{N}(i)} \left( \text{Message}(h_j^{(l)}) \right) \right)

Here, \(\mathcal{N}(i)\) is the set of all direct neighbors of node \(i\).

Aggregation Function Mathematical Properties Best Use Case Disadvantages
Sum Aggregation Permutation-invariant, scales with graph size Counting node degrees & sub-graph patterns Sensitive to outliers in massive graphs
Mean Aggregation Permutation-invariant, normalized Classifying global node distribution properties Fails to differentiate graph sizes/magnitudes
Max Aggregation Permutation-invariant, selects strongest feature Identifying extreme features or key nodes Discards weak but meaningful shared signals

3. Graph Convolutional Networks (GCNs)

Graph Convolutional Networks (GCNs), introduced by Kipf and Welling in 2016, translate traditional image convolution mathematical properties into graphs. GCNs normalize adjacency messages using the node degrees to prevent nodes with massive numbers of connections from blowing up the vector values.

The standard layer-wise propagation formula for GCNs is:

H^{(l+1)} = \sigma \left( \tilde{D}^{-\frac{1}{2}} \tilde{A} \tilde{D}^{-\frac{1}{2}} H^{(l)} W^{(l)} \right)

Where \(\tilde{A} = A + I_N\) (the adjacency matrix plus self-loops, so nodes aggregate their own features), \(\tilde{D}\) is the degree matrix of \(\tilde{A}\), \(W^{(l)}\) is the layer-specific trainable parameter weight matrix, and \(\sigma\) is a non-linear activation function like ReLU.

PyTorch Geometric GCN Layer (Python Code)

import torch
import torch.nn as nn
from torch_geometric.nn import GCNConv

class SimpleGCN(nn.Module):
    def __init__(self, in_features, hidden_features, out_classes):
        super(SimpleGCN, self).__init__()
        # GCN convolution layers automatically handle normalization
        self.conv1 = GCNConv(in_features, hidden_features)
        self.conv2 = GCNConv(hidden_features, out_classes)
        self.relu = nn.ReLU()

    def forward(self, x, edge_index):
        # x: Node feature matrix of size [N, in_features]
        # edge_index: Graph connectivity matrix of size [2, E]
        x = self.conv1(x, edge_index)
        x = self.relu(x)
        x = self.conv2(x, edge_index)
        return x
                

4. Graph Attention Networks (GATs)

While GCNs normalize connections based on structural node degrees, they assign static weights to all neighbors. Graph Attention Networks (GATs) use the self-attention mechanism from transformers to dynamically calculate the relative importance of neighbor nodes during aggregation.

This allows nodes to focus on specific, highly relevant neighbors while ignoring noisy connections. GATs compute attention coefficients between node features, using multi-head attention to stabilize learning and improve performance on tasks like molecular structure prediction.


5. Real-World Applications


Our Testing Process & Empirical Verification

Our analysis of GNN architectures was tested using the standard Cora citation network dataset. We evaluated GCN and GAT performance models on node classification tasks under controlled settings, running training loops in PyTorch Geometric on an NVIDIA L4 GPU instance to verify convergence stability and evaluate permutation invariance metrics.


Author & Practitioner

Pratyush

Pratyush is an AI researcher learning machine learning, computer vision, and deep learning architectures. He focuses on practical, hands-on ML implementation and building accessible educational resources.

Updated: August 2026 Author Profile

Continue Through the Maze

Deep Learning

Building a Neural Network From Scratch

Exploding gradients, loss spikes, and pure NumPy code.

Reinforcement Learning

Q-Learning & Policy Loops

Agent action cycles, environment feedback, and rewards.

Mathematics

The Math Behind ML

Linear algebra, partial derivatives, and gradient descent.

Information Retrieval

Vector Databases & RAG

High-dimensional embeddings and similarity search math.