An adjacency matrix represents connections between graph nodes. In transaction fraud detection, it links transactions that share a user ID or device ID; the matrix is built as sparse data, row-normalized, and converted to dense form when the graph is small. section 9.4.2
A graph neural network multiplies transformed node features by the matrix to aggregate neighbor information, then combines that information with each node’s own features. section 9.4.2
Listing 9.7 Defining the GNN model
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from scipy.sparse import csr_matrix
# Simple GNN Model
class FraudGNN(nn.Module):
def __init__(self, input_dim, hidden_dim=32):
super(FraudGNN, self).__init__()
self.conv1 = nn.Linear(input_dim, hidden_dim)
self.conv2 = nn.Linear(hidden_dim, hidden_dim)
self.classifier = nn.Linear(hidden_dim, 2) # Normal/Fraud
def forward(self, x, adj):
# First GNN layer: Aggregate neighbor information
h1 = torch.relu(self.conv1(x))
h1_agg = torch.mm(adj, h1) # Average neighbors' information
h1 = h1 + h1_agg
# Second GNN layer
h2 = torch.relu(self.conv2(h1))
h2_agg = torch.mm(adj, h2)
h2 = h2 + h2_agg
# Classification
out = self.classifier(h2)
return out
See the PyTorch model’s adjacency-matrix multiplication and feature aggregation. section 9.4.2







