Graph Neural Networks: AI That Understands Relationships

Deep Learning · intermediate

Featured image for Graph Neural Networks: AI That Understands Relationships

🕸️ Introduction: The World is a Web

So far in our AI journey, we have looked at two types of data:

  • Images: Organized in perfect, square grids (trained by CNNs).
  • Text: Organized in straight, sequential lines (trained by Transformers).

But what about data that is not a grid and not a line? What about data that looks like a massive spiderweb—a tangled network of connected dots?

Imagine the Singapore MRT map. The MRT map is a web. There are Stations (dots) connected by Tracks (lines).

  • You cannot represent the MRT map as a square grid of pixels.
  • You cannot represent it as a straight line of text.
  • It is fundamentally a Graph—a collection of points and lines that connect them.

Now imagine you want an AI to analyze this graph. You want the AI to answer: “If I am at Bugis station, which station is the closest to me with a shopping mall?” A standard CNN or Transformer will completely fail at this because they are designed for grids and lines.

To solve this, scientists created a special type of AI called Graph Neural Networks (GNNs). GNNs are designed specifically to learn from webs, networks, and connections.

In this 3000+ word deep dive, we will explore how GNNs work, why Facebook uses them to recommend friends, and how they are helping scientists discover new medicines!


📍 Chapter 1: What is a Graph? (The Nodes and Edges)

Before we look at the AI, let’s understand the data structure. A Graph is made of two very simple things:

1. Nodes (The Dots)

A Node is a single entity. It can represent a person, a train station, a molecule, or a web page.

  • Facebook Example: Each user account is a Node.
  • Singapore MRT Example: Each MRT station (e.g., Orchard, Raffles Place) is a Node.

2. Edges (The Connections)

An Edge is the line that connects two Nodes. It represents a relationship between them.

  • Facebook Example: If two users are “Friends,” there is an Edge between their Nodes.
  • Singapore MRT Example: If two stations are connected by a train track, there is an Edge between their Nodes.

The Power of Graphs

In a Graph, the context of a Node depends entirely on its surrounding Nodes. You cannot understand a Facebook user (Node) without looking at who their friends are (Edges to other Nodes).


⚙️ Chapter 2: Why Standard AI Fails on Graphs

Let’s imagine you want to train a standard CNN to analyze a Facebook social network.

The Problem

  • An image is a Grid. The pixels are always arranged in rows and columns. Pixel #5 is always next to Pixel #6. The CNN relies on this rigid structure.
  • A Graph has no fixed structure. You might have 10 friends, and I might have 1,000 friends. The connections are completely different for every single Node. There are no fixed rows and columns.

If you flatten the Graph and feed it into a CNN, the CNN will be completely confused. It will try to find neighbors where there are none, and it will miss connections that are 5 hops away.

The GNN Solution

A GNN doesn’t look at the graph as a flat grid. It looks at the graph as a messaging system.

  • Every Node in the graph is sending a message to all of its neighboring Nodes.
  • The messages contain information about the Node’s own properties.
  • A Node then looks at all the messages it receives from its neighbors, combines them, and updates its own internal state.

This concept of “Passing Messages” is the secret superpower of GNNs.


📨 Chapter 3: Message Passing (The Core of GNNs)

Let’s use the Facebook Friend Recommendation system as an example of how Message Passing works.

Imagine you are User A. You want a new friend. The GNN wants to know: “Is User Z similar to me? Should I be friends with User Z?”

The Message Passing Process

  1. Encoding: User A (Node A) encodes its own properties (Age, Hobbies, Location) into a digital message.
  2. Broadcast: User A sends this message to ALL of its direct friends (Node B, Node C, Node D).
  3. Aggregation: Node B receives the message from A. Node B combines A’s message with its own data. It then passes this combined message to its friends (Node E, Node F, and Node Z).
  4. 2-Hop Connection: Now, User Z has received a message that originally came from User A, but was filtered through User B.
  5. The Prediction: The GNN now looks at the similarity between User Z’s data and User A’s data. It calculates: “User A and User Z have a high chance of being friends because they are both connected to User B, and they share similar hobbies.”

Why this is brilliant

The GNN can “see” connections that are 2, 3, or 100 hops away. It can detect indirect relationships. For example, if you have a friend who has a friend who owns a skateboard shop, the GNN might recommend the skateboard shop to you because you are connected through the web.


🔬 Chapter 4: Where are GNNs Changing the World?

GNNs are not just for social media. They are solving some of the hardest problems in science and engineering.

1. Drug Discovery (Chemicals and Molecules)

A chemical molecule is a graph. The atoms are Nodes. The chemical bonds between them are Edges.

  • In the past, scientists had to physically synthesize (mix) chemicals in a lab to test if they could cure a disease. This took years and cost millions of dollars.
  • Today, pharmaceutical companies use GNNs to analyze the graph of a molecule.
  • The GNN looks at how the atoms are arranged. It predicts: “If we switch this Carbon atom with a Nitrogen atom, the molecule will bind perfectly to the cancer protein.”
  • The GNN tests 1 billion virtual molecules in 1 hour. The scientists only synthesize the top 10 candidates. This speeds up drug discovery by 500%!

2. Traffic Congestion Prediction (Smart Cities)

Remember the Virtual Singapore Digital Twin? It relies heavily on GNNs.

  • A city’s road network is a massive graph (Roads are Edges, Intersections are Nodes).
  • GNNs look at historical traffic data and the connections between roads.
  • They predict: “If there is an accident at Junction A, within 10 minutes, traffic will back up at Junctions B and C because they are connected in the graph.”
  • The GNN allows the Smart City to adjust traffic lights at Junctions B and C automatically before the traffic even reaches them. The traffic jams never happen.

3. Recommendation Systems (Beyond Collaborative Filtering)

We learned about Collaborative Filtering earlier. But GNNs take recommendations to a whole new level.

  • On Pinterest, users have “Boards” and “Pins.” A user (Node) creates a Board (Node) and saves a Pin (Node). This creates a massive web.
  • Pinterest uses a GNN called PinSage to analyze this graph.
  • If you save a Pin about “Keto Diet Recipes,” the GNN traces the edges. It finds: “Users who saved this Keto Pin also saved a Pin about ‘Smoothie Makers’.”
  • It recommends the Smoothie Maker to you, even though you never searched for it. The GNN found the hidden connection through the shared web of users.

4. Fraud Detection (Finding the Criminal Network)

Credit card fraudsters usually work in teams. They have fake identities (Nodes) that connect to each other (Edges) to share money.

  • The GNN scans the web of bank transactions.
  • If it detects a strange pattern—like 10 fake accounts sending $5 to each other in a perfect circle—the GNN instantly flags the entire network as a fraud ring.
  • Normal Machine Learning would miss this because it looks at individual transactions. The GNN catches it because it looks at the connections.

🧬 Chapter 5: Real-Life GNNs (Graph Convolutional Networks)

If you read scientific papers, you will see a term called GCN (Graph Convolutional Network).

The Evolution

  • We learned about CNNs (Convolutional Neural Networks) for images. A CNN uses a “magnifying glass” to scan a grid of pixels.
  • A GCN (Graph Convolutional Network) is the adapted version for graphs.
  • Instead of scanning a rigid grid, the GCN uses a “Message Passing” filter that adapts to the shape of the graph.

How a GCN works

  1. It picks a central Node.
  2. It looks at all the neighbors of that Node.
  3. It aggregates (averages) the data of the central Node and its neighbors into a single mathematical number.
  4. It does this for every single Node in the massive graph.
  5. By aggregating the local neighborhoods, the GCN learns the global structure of the entire web.

The Secret

GCNs are incredibly efficient. They can process a graph with 1 billion Nodes (like the entire social network of Asia) in just a few hours on a single computer. They are the backbone of modern graph analysis.


💼 Chapter 6: Careers in Graph Neural Networks

1. Graph Data Scientist (The Network Analyst)

  • What they do: They don’t just build the AI; they build the graph. They take massive, messy datasets (like a list of 1 million emails sent between people) and convert them into a clean graph of Nodes and Edges. They use Python libraries like NetworkX to draw the graph and see the connections.
  • Average Salary: $150,000+ USD / year.

2. Chemoinformatics Engineer (The Molecule Hunter)

  • What they do: They specialize in applying GNNs to biology and chemistry. They take the structure of a virus and apply GNNs to find the exact spot where a drug molecule can attach to stop the virus.
  • Average Salary: $160,000+ USD / year.

3. Knowledge Graph Engineer (The Web Architect)

  • What they do: Google uses a massive graph called a “Knowledge Graph” to power its search engine. This graph connects every person, place, and thing in the world. The Knowledge Graph Engineer builds and updates this web so that GNNs can help answer complex questions like “What is the capital of the country that hosted the 2024 Olympics?”
  • Average Salary: $170,000+ USD / year.

🧪 Chapter 7: Experiment – Visualizing a Graph with Python NetworkX

You can build and visualize a graph right now using Python. We will recreate the famous “Zachary Karate Club” graph—a real social network of 34 martial arts students in the 1970s.

The Karate Club Experiment

  1. Install the library: pip install networkx matplotlib
  2. Create a Python file karate_graph.py.
  3. Paste the following code. It draws the social connections between the 34 students.
import networkx as nx
import matplotlib.pyplot as plt

# Load the famous "Zachary Karate Club" graph (Real-life social network from the 1970s)
G = nx.karate_club_graph()

# Draw the graph
nx.draw(G, with_labels=True, node_color='lightblue', edge_color='gray', node_size=800)

# Show the graph
plt.show()

🏁 Conclusion: The Network of Intelligence

Graph Neural Networks are revolutionizing how we understand connected data. From molecules to social networks, GNNs are uncovering patterns we couldn’t see before.

We’ve Learned

  • Graphs consist of Nodes and Edges (dots and connections)

  • GNNs use Message Passing to understand relationships

  • They’re used in drug discovery, traffic prediction, recommendations, and fraud detection

  • GCNs are the graph version of CNNs

  • They process 1 billion nodes efficiently

What This Means for You

Understanding GNNs helps you:

  • See the world as a network of connections

  • Understand social media algorithms

  • Appreciate relationship-based AI

In Our Next Article:

Now that you understand Graph Neural Networks, it’s time to explore Self-Supervised Learning—AI that teaches itself!