What is a graph neural network?
- Authors
- Name
- Amit Shekhar
- Published on
A Graph Neural Network is a type of neural network that learns directly from a graph, using both the features of each node and the edges between the nodes.
In this blog, we will learn about Graph Neural Networks. We will also see what a graph is, what nodes and edges are, why we need a Graph Neural Network, how it learns through message passing, and where we use it.
We will cover the following:
- What is a graph?
- What are nodes and edges?
- Why do we need a Graph Neural Network?
- What is a Graph Neural Network?
- How does a Graph Neural Network learn?
- Message passing explained step by step
- Where do we use Graph Neural Networks?
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
What is a graph?
A graph is a way of showing things and the connections between them.
In simple words, a graph is a collection of dots, and lines that join some of those dots.
Let's say we have a group of friends. Each person is a dot. If two people are friends, we draw a line between their dots. That drawing is a graph.
Let's see a simple graph like below:
(Amit)
/ \
/ \
(Riya)----(John)
\ /
\ /
(Maya)
Here, we can see that there are four people. Each person is a dot. A line between two people means they are friends. Amit is friends with Riya and John. Riya is friends with Amit, John, and Maya. This simple picture is a graph.
What are nodes and edges?
Now, let's learn the proper words used in a graph.
A node is one single thing in the graph. It is a dot in our picture. In our friends example, each person is a node.
An edge is the connection between two nodes. It is a line in our picture. In our friends example, each friendship is an edge.
Let's label our earlier picture like below:
NODE = a dot (a person)
EDGE = a line (a friendship)
(Amit)
/ \
edge edge
/ \
(Riya)--edge--(John)
Here, we can see that the dots are nodes and the lines are edges. Nodes are things. Edges are connections.
Now, one more important idea. Each node can carry some information about itself. We call this information the features of the node.
For a person, the features could be age, city, and favorite hobby. So, a node is not just a plain dot. A node also holds some data about itself.
Planning your move into AI?
No matter which tech domain you work in today, with your effort, you can transition to these roles:
- AI Engineer
- Forward Deployed Engineer
- AI Agent Engineer
- Harness Engineer
- Voice AI Engineer
- AI Architect
- LLM Engineer
- LLM Inference Engineer
- Gen AI Engineer
- MLOps Engineer
- ML Engineer
- Deep Learning Engineer
Our AI and Machine Learning Program at Outcome School takes you there.
Why do we need a Graph Neural Network?
Now, the question is, why do we need something special for graphs?
Let's understand the problem first.
Most normal machine learning models expect data in a neat table or a fixed grid. A photo is a grid of pixels. A sentence is a line of words. These have a fixed shape.
We have a detailed blog on Convolutional Neural Networks that explains how a neural network learns from such a grid of pixels.
But a graph does not have a fixed shape. One node can have two connections. Another node can have ten connections. There is no fixed order and no fixed size.
Let's see the difference like below:
A TABLE (fixed shape) A GRAPH (flexible shape)
+------+-----+-------+ (A)---(B)
| Name | Age | City | | |
+------+-----+-------+ (C)---(D)---(E)
| Amit | 25 | Pune | |
+------+-----+-------+ (F)
| Riya | 30 | Delhi |
+------+-----+-------+
Here, we can see that a table has rows and columns that always line up. A graph does not. Node D has four connections, but node F has only one. A normal model does not know how to read this shape.
There is one more thing. In a graph, the connections carry meaning. Knowing who a person is friends with tells us a lot about that person. A normal model would throw away these connections.
So, here comes the Graph Neural Network to the rescue.
What is a Graph Neural Network?
A Graph Neural Network is a type of neural network that learns directly from a graph, using both the node features and the edges between the nodes.
Before we go ahead, let us understand one key word.
A neighbor of a node is any node directly connected to it by an edge.
In our friends example, the neighbors of Amit are Riya and John, because Amit has an edge to each of them.
The main idea of a Graph Neural Network is that, to understand a node, we look at the node and its neighbors.
This makes sense in real life. To understand a person, it helps to know their friends. A Graph Neural Network uses the same idea.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
How does a Graph Neural Network learn?
Now, let's understand the core trick. It is called message passing.
Every node sends its information to its neighbors, and every node also receives information from its neighbors. Then each node updates itself using what it received.
Let's see the flow like below:
STEP A: Each node collects messages from its neighbors
(B) (C)
\ /
\ /
↓ ↓
(A)
A receives info from B and C
STEP B: A combines its own info with the received info
(A new) = own info + info from B + info from C
Here, we can see that node A gathers information from its neighbors B and C. Then A mixes that gathered information with its own information. The result is a new, richer version of A.
So, after one round of message passing, every node knows a little about its neighbors.
Now, we do this more than once.
After one round, a node knows about its direct neighbors. After two rounds, a node also knows about the neighbors of its neighbors, because that information traveled one more step. After three rounds, the reach grows even further.
Let's see how information spreads like below:
Round 0: A knows only about itself
Round 1: A knows about A + its neighbors
Round 2: A knows about A + neighbors + neighbors of neighbors
(X)---(B)---(A)---(C)---(Y)
In Round 2, A receives information coming from X and Y,
even though A is not directly connected to them.
Here, we can see that in Round 2, node A learns something about X and Y. A is not directly connected to X or Y, but the information traveled across the edges, one step per round, until it reached A.
This is how a Graph Neural Network learns from the structure of the graph. The shape of the connections decides how information flows.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Message passing explained step by step
Now, let's walk through message passing step by step.
Step 1: Every node starts with its own features. Think of this as each person holding a small note about themselves.
Step 2: Each node sends a copy of its note to all of its neighbors. So, every node also receives notes from its neighbors.
Step 3: Each node combines all the notes it received. A common way is to take the average of the neighbor notes, so no single neighbor dominates.
Step 4: Each node mixes its own note with the combined neighbor note to create a new, updated note. This mixing uses the learnable part of the neural network.
Step 5: We repeat Steps 2 to 4 for a few rounds. Each round, the notes get richer because they carry information from further away.
Let's see a tiny numeric example to make this real. Assume that each node holds a single number as its feature. Consider three nodes connected like below:
(A=2)---(B=4)---(C=6)
Here, we can see that A holds the number 2, B holds 4, and C holds 6. A is connected to B. B is connected to A and C.
Now, let's do one round of message passing using a simple rule. The new value of a node is the average of itself and its neighbors.
For node A, the neighbors are just B. So the new A is the average of 2 and 4, which is 3.
For node B, the neighbors are A and C. So the new B is the average of 4, 2, and 6, which is 4.
For node C, the neighbors are just B. So the new C is the average of 6 and 4, which is 5.
Our updated values:
Before: (A=2)---(B=4)---(C=6)
After: (A=3)---(B=4)---(C=5)
Here, we can see that each node changed based on its neighbors. Node A moved from 2 to 3 because of B. Node C moved from 6 to 5 because of B.
In a real Graph Neural Network, the values are not single numbers but lists of numbers, and the mixing uses weights that the model learns from data. But the core idea stays exactly the same. A node updates itself using its neighbors.
To learn Neural Networks, Feed Forward Neural Networks (FFNNs), and Backpropagation, and to build a Neural Network from scratch, check out our AI and Machine Learning Program at Outcome School.
Where do we use Graph Neural Networks?
Now that we have understood how a Graph Neural Network works, it is time to see where it is useful.
A Graph Neural Network fits any problem where things are connected. Example use cases:
- Social networks, to suggest people we may know based on our friends.
- Online shopping, to recommend products based on what similar users bought.
- Maps and navigation, where roads are edges and intersections are nodes, to predict travel time.
- Medicine, where a molecule is a graph of atoms and bonds, to predict how a drug will behave.
- Fraud detection, where accounts and transactions form a graph, to spot suspicious patterns.
Here, we can see one common thread. In every case, the connections matter.
Now we must have understood what a Graph Neural Network is, how it learns through message passing, and where it is used.
Frequently Asked Questions
What is the difference between a Graph Neural Network and a normal neural network?
A normal model expects data in a neat table or a fixed grid, like a photo or a sentence. A graph has no fixed shape, no fixed order, and no fixed size, and a normal model would throw away the connections. A Graph Neural Network learns directly from the graph, using both the node features and the edges.
How many rounds of message passing does a Graph Neural Network use?
A Graph Neural Network repeats message passing for a few rounds. After one round, a node knows about its direct neighbors. After two rounds, it also knows about the neighbors of its neighbors. Each extra round lets information travel one more step across the edges.
Can a Graph Neural Network handle nodes with different numbers of neighbors?
Yes. One node can have two connections and another can have ten, and a Graph Neural Network handles both. Each node combines all the messages it receives from its neighbors, and a common way is to take their average, so no single neighbor dominates.
What does a Graph Neural Network learn during training?
A Graph Neural Network learns the weights used to mix a node's own information with the combined information from its neighbors. In a real Graph Neural Network, the values are lists of numbers, not single numbers, and these weights are learned from data. The core idea stays the same: a node updates itself using its neighbors.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
