How GNNs Work: Learn Graph Neural Networks with Code & Use Cases
Curious about Graph Neural Networks (GNNs) but not sure where to start? You’re in the right place. GNNs are one of the most exciting…
How GNNs Work: Learn Graph Neural Networks with Code & Use Cases
Curious about Graph Neural Networks (GNNs) but not sure where to start? You’re in the right place. GNNs are one of the most exciting developments in deep learning — built specifically for graph-structured data like social networks, recommendation systems, and even molecular chemistry.
In this beginner-friendly GNN tutorial, we’ll break down how Graph Neural Networks work, why they’re different from traditional machine learning models, and how you can start building your own using Python and PyTorch Geometric. Expect real-world examples, hands-on code, and some light Facebook-friendship drama along the way.
The Problem with Flat ML!
Okay! Lets first take an Analogy…
Imagine modeling the concept of friendship using the framework of Facebook.

You’ve got users as rows, and columns for age, location, number of likes
Fine. But… where do you put “Linus is friends with GrepHer, but not with ElonMuskFanboi”? You can surely fit it in, but the relationship between them gets “messy” and “complicated” fast!
That’s because traditional machine learning models; your beloved MLPs and CNNs — love flat, independent data points. They don’t natively care about how one sample connects to another.
Now surely, its great for photos of Cat but “not so great” for:
- Financial Transactions
- Recommendation Systems
- Social Network
- And many more
In all of the above listed stuff, the Structure of the Data is a part of the Data.That’s where Graph Neural Networks(GNNs) comes into the picture!

With GNNs, the question isn’t just “What does this node look like?(Who is Linus?)” — it’s also “Who’s it connected to, and what are they like? (With home he has relation with?)”
What is Graph?
Before we straight away dive into Graph Neural Network lets first have an idea of what is a Graph…. [Not the line Chart kind, but the Mathematics kind]
At its core a Graph is basically:
- Nodes (also called vertices): represent things
- Edges : Represent Relationship Between things
In our example the user will be the nodes and relation Linus have with GrepHer will be the edge.

Simple as that? So now lets quickly consider some real world examples

You probably already have used graph without knowing!
- Google Maps? Graph of roads.
- Spotify recommendations? Graph of users, artists, and playlists.
Want to make a baby graph? Here’s the code:
import networkx as nx
import matplotlib.pyplot as plt
# Create an undirected graph
G = nx.Graph()
# Add some nodes
G.add_nodes_from([0, 1, 2, 3])
# Add edges (relationships)
G.add_edges_from([(0, 1), (1, 2), (2, 3), (0, 2)])
# Visualise it
nx.draw(G, with_labels=True, node_color='skyblue', node_size=800)
plt.show()

Now this blog isnt about networkx so not gonna go into that, but just understand its a package where:
- G is variable with graph initialization
- add_nodes( [ node1 , node2, node3 ] ) is used for adding nodes
- add_edges( [ (node1 connected to node2) ] ) is used for adding edges
- nx.draw to visualize it
This gives us a simple graph with four nodes and a few connections. A full complete structure and relationship it contains with other data points not just the information of the data points.
WHY THIS STRUCTURE IS IMPORTANT?
Let’s say node 2 is a person applying for a loan. They’re new, so there’s not much data about them—no credit score, no payment history.
But node 2 is connected to nodes 1 and 3, who do have strong financial records. If your model only looks at node 2, it won’t know what to do. But if it also looks at their connections, it can make a smarter guess.
This is exactly what GNNs are good at — learning from both the data and the structure of the network.
What makes soo GNN Special !?
Okay! Now we know what a graph is… But still the main question isnt answered that, “How can we learn from Graph?”
Here comes Graph Neural Networks, popularly known as GNNs. Unlike regular ML models that looks one row or one sample at a time, GNNs on the other hand are team players. They update each node by looking at its neighboring nodes.
This process is called Message Passing.
In simple idea:
- Each node starts with its own features
- Gathers information from its connecting neighboring nodes.
- Combines everything to update its own stake
- Repeat for few rounds.
Welp you can say you ask some advice from your friend and after hearing their advice you adjust your own opinion based on theirs.
Code Example: Mean Aggregation
Each node has a number (like a feature), and we want each node to update itself by averaging with its neighbors.
import torch
# Node features: let's say 4 nodes, each with 1 feature
x = torch.tensor([[1.0], [2.0], [3.0], [4.0]]) # Node 0 to 3
# Edges: node pairs (from, to)
edge_index = torch.tensor([[0, 1, 2],
[1, 2, 3]]) # 0↔1, 1↔2, 2↔3
def mean_aggregate(x, edge_index):
row, col = edge_index
messages = x[col]
# Simple averaging for each sender node
agg = torch.zeros_like(x)
agg.index_add_(0, row, messages)
return (x + agg) / 2 # Combine with own feature
print(mean_aggregate(x, edge_index))

Output
This is a very simplified GNN idea: each node updates by averaging with its neighbours.
Why this is powerful?
Because Connection carry meaning:
- User’s preferences may align with their friends.
- New transaction might be suspicious if linked to known fraudsters.
- Molecule’s behaviour depends on how its atoms are bonded.
It helps you model all these, bringing structure into your prediction.
Tour of our Popular GNN Variants
So as you folks are already starting to nod for Message passing. Lets meet with the “Celebs” of this GNN world:
GCN — Graph Convolutional Network
GCNs take the average of a node’s neighbours and blend it with the node’s own features. It’s smooth, it’s classic, and it gets the job done when things aren’t too chaotic.
How does it look?

GCN
- On the left, you’ve got your input graph — each node starts with its own features.
- Then, in the hidden layers, each node aggregates information from its neighbours.
- Notice how purple nodes appear? That’s the model learning new node representations based on the structure.
- The ReLU activations between layers? They inject non-linearity so the model doesn’t just learn boring averages.
- Finally, the output layer gives you a new graph — same structure, but with richer, smarter node features (good for predictions!).
Use it when: You have graphs like social or citation network and the relationships between the nodes are un-directed.
Code Interpretation
import torch
from torch_geometric.nn import GCNConv
# Node features: let's say 4 nodes, each with 16 features
x = torch.randn(4, 16)
# Edges: node pairs (from, to) - using the edge_index from the previous cell
edge_index = torch.tensor([[0, 1, 2],
[1, 2, 3]])
conv = GCNConv(in_channels=16, out_channels=32)
out = conv(x, edge_index)
print(out)
In practice, you might stack 2–3 layers, and maybe finish off with a softmax if you’re doing node classification.
GAT — Graph Attention Network
What if you don’t want to treat all neighbours equally? What if you only trust some of your friends? That’s where GAT comes in.
It uses attention — the model learns which neighbours to listen to more.

Lets understand this:
- Each node (like node 1 in blue) wants to update its feature vector.
- But instead of blindly averaging its neighbours’ features (like GCN), it calculates a weight for each neighbour — these weights are called attention coefficients (denoted as α₁₂, α₁₄, etc.).
- These coefficients tell the node how much “trust” to place in each neighbour’s information.
Now if you are interested to know that how these coefficient are calculated, then that will be a separate medium blog on GATs.
But for now understand This “importance” is learned dynamically using a self-attention mechanism. Mathematically, it’s:
attention(h_i, h_j) = a(Wh_i, Wh_j)
Which just means: Take node i and j’s features, apply a linear transformation (W), then pass them through a learnable function (often a feed-forward layer with softmax) to get a score.
These scores are then normalised across neighbors (softmax), so everything sums to 1. After that, the node updates itself by blending its neighbors’ features — but weighted by those attention scores:
h'_i = Σ α_ij * h_j
In the diagram, you can see how node 1 gathers its neighbours’ features (h₂, h₃, h₄…), multiplies each by their respective α’s, and combines them to produce the updated h’₁.
Use it when: working with graphs where different neighbours should have different levels of influence — like in social networks, citations, or recommendation systems.
Code Interpretation:
from torch_geometric.nn import GATConv
conv = GATConv(in_channels=16, out_channels=32, heads=2)
out = conv(x, edge_index)
Graph Sage
In a crowded networking event you arent going to talk to all the person present there, instead you will grab a few people nearby get their views and build your understanding of the whole event.
That what Graph Sage does!
Unlike GCNs, which aggregates information from all neighbours, GraphSAGE takes a shortcut: sample just a few neighbours and aggregate their features. It’s fast, it’s smart, and it scales beautifully.
SAGE = Sample and Aggregate

https://images.app.goo.gl/QbrBX9U5zm5Wy38S7
- On the left, you have the input:
Ais the adjacency matrix (structure of the graph).Xis the node features (what each node knows initially).- In the middle box — the GraphSAGE engine:
- It picks a subset of neighbours (see how not all connections are active).
- Each node gathers info from this subset — using
mean,max, or even a learnable function. - The updated node features become deeper shades of green — meaning they’ve been enriched by the context of their sampled neighbours.
- On the right, you get the final updated node embeddings — nice and refined, ready for classification, clustering, or any downstream task.
Code Interpretation:
from torch_geometric.nn import SAGEConv
conv = SAGEConv(in_channels=16, out_channels=32)
out = conv(x, edge_index)
Real World Use Cases
- Recommendation System: If we consider the example of Netflix, so netflix doesnt need to know what you like rather it needs to know what people like you likes, and it needs to update the knowledge constantly. Here GNNs works the best…. Why? Users and items can form a bipartite graph. GNNs can learn the structure of these relationships over time, capturing not just “what” you interacted with, but how and why.
- Social Media Network and Content moderation: Bad actors don’t tweet in isolation. Whether it’s spam, misinformation, or bot coordination, these behaviours are often graphable. Social networks are graphs by definition. GNNs can identify communities, influence patterns, and flag suspicious network behaviours better than isolated text classifiers.
Hands-On: Building Your First GNN (Using PyTorch Geometric)
[embed]ColabNotebook GNN Detail code with explanation
Common Pitfalls and Performance Tips
So you’ve built your first GNN. It runs, it trains, it even kinda predicts. Nice. But then reality kicks in — and graphs can be messy little beasts.
Graph too Big?
If you are trying the cram the whole graph into a memory is like inviting the whole internet to dear lappy’s Birthday party thereby following its funeral too… Using neighbour sampling like in GraphSAGE or mini-batch training with subgraphs will help things scale smoothly.
Over-smoothing?
Stack too many GNN layers and all your node features start looking the same — like blending every colour into beige. Use residual connections, layer norm, or just keep it shallow.
Disconnected nodes?
Got isolated loners in your graph? No problem. Add self-loops so they can at least talk to themselves. Also, try feature engineering or preprocessing tricks to give them more context.
Resources and Further Reading
Research Papers:
Final Thoughts
GNNs are not just academic toys anymore. They’ve moved into fraud detection, content moderation, recommender systems, and even biology labs. If your data lives in a world of relationships and interactions, GNNs just might give you the edge (pun obviously intended).
Next steps? → Try building your own recommender with a GNN. → Explore graph-level tasks like molecule classification. → Or go wild and model a social network with attention-based gossip.
Graph Neural Networks aren’t just a new model — they’re a new way of thinking about data. And once you start seeing graphs, you kind of can’t stop.
메타데이터
- post_id
- 00a2564aa97e
- slug
- how-gnns-work-learn-graph-neural-networks-with-code-use-cases-00a2564aa97e
- url
- https://medium.com/@mochoye/how-gnns-work-learn-graph-neural-networks-with-code-use-cases-00a2564aa97e
- canonical_url
- https://medium.com/@mochoye/how-gnns-work-learn-graph-neural-networks-with-code-use-cases-00a2564aa97e
- author_url
- https://medium.com/@mochoye
- status
- ok
- fetched_at
- 2026-06-15 20:49:13