DEV Community

Cover image for how i built a graph neural network (and what i learned)
Anamika Ghosh
Anamika Ghosh

Posted on

how i built a graph neural network (and what i learned)

Every time you swipe your card, pay a bill, or send money online, there's a silent war happening in the background: fraudsters vs. banks. And honestly? The banks are losing more often than you'd think.

I wanted to understand how this war actually works - not from the outside looking in, but from the inside, hands on the keyboard. So I built a fraud detection system using Graph Neural Networks (GNNs). This is what I found.

The Problem with Traditional Fraud Detection

Most fraud detection systems today still run on rules.

  • Transaction above ₹50,000? Flag it.
  • New location? Flag it.
  • 3 AM purchase on something you've never bought before? Flag it.

Simple, right? Except fraudsters are smart. They adapt. They know the rules -sometimes better than the systems built to catch them.

And here's the deeper issue: fraud was never really about individual transactions. It's about connections.

A single transaction can look completely normal on its own. But zoom out -look at the merchant, the device, the IP address, the account history -and patterns start to emerge that no single rule could ever catch.

That's exactly where Graph Neural Networks come in.

Wait, What Even Is a Graph Neural Network?

Let me break it down without the jargon.

Picture a graph as a network of connected dots. Each dot (a node) is something real -a user, a merchant, a device. The lines connecting them (edges) represent relationships -a transaction between a user and a merchant, or a device shared by multiple accounts.

A Graph Neural Network looks at the entire graph, not just individual nodes in isolation. It learns from how nodes interact with their neighbors.

So if a user is connected to a suspicious merchant, and that merchant is connected to a handful of other flagged accounts, a GNN can pick up on that pattern -even when every individual transaction looks perfectly clean on paper.

How I Actually Built It

I built the system using PyTorch and PyTorch Geometric, a library purpose-built for graph-based deep learning.

Here's the high-level architecture I landed on:

  1. Modeled transactions as a graph -users and merchants became nodes, transactions became edges.
  2. Engineered features -transaction amount, time, location, device, frequency, and a handful of derived signals.
  3. Used a GraphSAGE architecture -it builds embeddings for each node by sampling and aggregating information from its neighbors, which is exactly what you want when "who you're connected to" matters as much as "what you did."
  4. Trained on labeled data -a dataset of known fraud and non-fraud transactions gave the model something to learn from.

The key shift here: the model wasn't just staring at transaction amounts in a vacuum. It was looking at the neighborhood around every transaction.

And the results genuinely surprised me.

What I Found

The GNN caught patterns that traditional rule-based systems flat-out missed.

A transaction that looked totally normal on its own turned out to be suspicious once the model noticed it was linked to a merchant that had previously been flagged -by a completely different, unrelated user.

It even surfaced entire fraud rings -coordinated groups working together -the kind of organized behavior a rule-based system would never catch, because no single rule captures "these five accounts are quietly connected."

The precision landed around 92% on the test set. But honestly, the number that mattered more to me wasn't the metric -it was that the model was catching things nobody else was catching.

The Hard Part (And What I Learned From It)

Building the model architecture? That was the easy part.

The hard part was the data.

I spent far more time cleaning data, constructing the graph structure, and engineering features than I ever spent training the model itself. "Data preparation is 80% of the work" is one of those phrases you hear a hundred times before a project -and then you finally feel it on your own.

I also learned, the hard way, that GNNs are computationally expensive. Training on a graph with millions of nodes takes hours, even on a solid GPU. I had to get comfortable with batching, sampling, and making real trade-offs between accuracy and training time.

It was worth every hour.

Why This Matters Beyond Fraud

Fraud detection is really just one application of this idea. Graph Neural Networks show up everywhere -recommendation systems, drug discovery, social network analysis, and more.

But for me, this project was never really about just building a working model. It was about understanding something bigger: connections matter more than individual data points.

We live in a connected world. It only makes sense that our systems should reflect that.

What's Next

I'm currently working on making this system production-ready wiring it up to a FastAPI backend, adding real-time inference, and building a dashboard to actually visualize the graph as it evolves.

Always learning. Always building. ✌️

Top comments (0)