Chapter 12: Node classification and link prediction with GNNs
The introduction connects two GNN tasks to real-world applications: identifying illicit activity in transaction graphs and recommending movies from user–movie interactions.
After this chapter you will be able to
- Transaction graph node classification. Explain how node classification applies to anti-money laundering: financial transactions are represented as a graph, and GNNs can identify suspicious patterns and classify nodes as licit or illicit.
- User–movie link prediction. Describe how link prediction applies to movie recommendations: a GNN predicts potential user–movie interactions from ratings, learning user and movie embeddings and using links between them to recommend movies.
- AML transaction graph classification. Explain how financial transactions are represented as a graph for anti-money laundering node classification, and state what the GNN is used to identify in the Elliptic dataset.
- Elliptic graph input files. Describe how the three Elliptic CSV files represent the transaction graph: transactions are nodes with anonymized features, directed payment flows are edges, and a separate file associates nodes with class labels.
- Incremental node IDs and edges. Trace how the original source and target transaction IDs are replaced with incremental node IDs, then used to create the edge_index tensor describing the graph’s edges.
- Node feature row alignment. Explain how node features become a tensor whose row indices match the new incremental node IDs, with original node IDs dropped.
- PyG graph data construction. Describe how the preprocessed node features, edge indices, and node labels become a PyG Data object.
- Known-label node selection. Explain why the classification datasets are defined using nodes with known labels and how the known_mask tensor is used to determine their size.
- Dataset masks and class checks. Describe how training, validation, and testing index masks are added to the PyG Data object, and what the split statistics let you inspect.
- Node classification encoder–decoder flow. Trace how the NodeClassifier uses a GNN to update each transaction node’s representation from its neighbors, then uses a decoder output to classify the node as licit or illicit.
- Classification output and loss. Explain why the decoder uses log_softmax and how CrossEntropyLoss uses predicted classification values and actual labels during training.
- AML evaluation metrics. Distinguish precision, recall, and F1-score for licit-versus-illicit node classification, and describe why the evaluation uses a weighted average.
- Validation performance comparison. Describe how the models’ validation precision, recall, and F1-scores compare and why recall alone does not establish the best model.
- Class-specific test results. Explain what the test-set confusion matrices add to the evaluation and how their licit-versus-illicit results support choosing SAGE for AML.
- User–movie links for recommendations. Explain how a graph represents interactions and preferences between users and movies as links, making link prediction relevant to movie recommendations.
- MovieLens link prediction inputs. Describe how the selected columns from movies.csv and ratings.csv identify movies, their genres, and user–movie rating records for the link prediction task.
- Movie genre feature vectors. Describe how movie genres are represented numerically for GNN learning: each movie has a vector of 0s and 1s, with a 1 indicating membership in a genre, and a movie may belong to multiple genres.
- User movie edge index. Trace how rating connections are prepared: map original user and movie IDs to incremental IDs starting at 0, then use those new IDs to build an edge_index tensor connecting users and movies.
- Heterogeneous movie graph representation. Explain why the user–movie graph uses PyG HeteroData rather than Data, and describe how it represents node features and the rates relationship.
- Disjoint link prediction edges. Describe how RandomLinkSplit partitions rating links and why the training edges are further separated into message-passing and supervision sets.
- Evaluation context and neighbor sampling. Distinguish the edges supplied for message passing from those used to evaluate validation and test predictions, and describe how LinkNeighborLoader prepares mini-batches.
- User and movie input embeddings. Distinguish how the encoder creates initial embeddings for users and movies: users have no intrinsic features, while movies have genre features.
- Heterogeneous neighborhood aggregation. Explain how the heterogeneous GNN refines embeddings using user–movie connections and how PyG metadata guides that process.
- Dot-product link scores. Describe how the decoder uses encoded user and movie embeddings to score a possible rating link.
- Recommendation evaluation metrics. Describe what precision, recall, and F1-score mean when predicting user–movie links.
- Validation performance tradeoffs. Compare the models' validation precision, recall, and F1-score, including the recommendation tradeoff each result implies.
- Test confusion matrix interpretation. Explain how the test-set confusion matrices support the comparison of missed ratings and irrelevant recommendations across SAGE, GCN, and GAT.
What it builds on
- Node classification (chapter 9) · Introduces node classification as a graph ML task.
- Link prediction (a.k.a. relationship prediction) (chapter 9) · Introduces link prediction as a graph ML task.
- Embeddings in graph representation learning (chapter 11) · Introduces embeddings in graph representation learning.
- The encoder–decoder model (chapter 11) · Introduces the encoder–decoder architecture.
- The message-passing framework: A neural conversation (chapter 11) · Introduces message passing between neighboring nodes.
- The basic GNN model (chapter 11) · Introduces the basic GNN model.
- The decoder: Reconstructing graph properties (chapter 11) · Introduces the decoder’s role in an encoder–decoder model.
Section by section
Introduction
The introduction connects two GNN tasks to real-world applications: identifying illicit activity in transaction graphs and recommending movies from user–movie interactions.
- Transaction graph node classification: Explain how node classification applies to anti-money laundering: financial transactions are represented as a graph, and GNNs can identify suspicious patterns and classify nodes as licit or illicit.
Why it matters: It connects node classification to a concrete fraud-detection use case. - User–movie link prediction: Describe how link prediction applies to movie recommendations: a GNN predicts potential user–movie interactions from ratings, learning user and movie embeddings and using links between them to recommend movies.
Why it matters: It shows how a predicted graph relationship can become a recommendation.
Key terms: node classification, anti-money laundering (AML), licit or illicit, link prediction, user–movie interactions, ratings, embeddings
12.1 Node classification for anti-money laundering applications
Introduces anti-money laundering node classification by representing financial transactions as a graph and using GNNs to identify licit and illicit nodes.
- AML transaction graph classification: Explain how financial transactions are represented as a graph for anti-money laundering node classification, and state what the GNN is used to identify in the Elliptic dataset.
Why it matters: This connects the financial-crime task to the graph representation and the prediction the model makes.
Key terms: AML, node classification, GNNs, licit and illicit nodes, Elliptic dataset
12.1.1 Input data
Introduces the Elliptic financial-transaction dataset and identifies the files that supply nodes, directed edges, features, and class labels for the GNN example.
- Elliptic graph input files: Describe how the three Elliptic CSV files represent the transaction graph: transactions are nodes with anonymized features, directed payment flows are edges, and a separate file associates nodes with class labels.
Why it matters: These files supply the graph structure, node information, and labels used in the classification example.
Key terms: Elliptic dataset, features, edges, classes, directed payment flows
12.1.2 Graph processor: Data preparation
This section prepares financial transaction edges, node features, and labels as aligned numerical tensors for GNN learning.
- Incremental node IDs and edges: Trace how the original source and target transaction IDs are replaced with incremental node IDs, then used to create the edge_index tensor describing the graph’s edges.
Why it matters: The GNN graph representation needs edges expressed using the prepared node IDs. - Node feature row alignment: Explain how node features become a tensor whose row indices match the new incremental node IDs, with original node IDs dropped.
Why it matters: Matching feature rows to node IDs lets the graph structure and node attributes refer to the same nodes. - Numerical node label encoding: Describe the conversion of the original node classes into a numerical node_labels tensor, including which numbers represent licit, illicit, and unknown nodes.
Why it matters: Numerical class labels complete the prepared data for node classification.
Key terms: txId1, txId2, incremental ID, edge_index, node_features, node features, LabelEncoder, node_labels, licit, illicit
12.1.3 Graph processor: Homogeneous PyG graph
The section turns preprocessed Elliptic data into a homogeneous PyG graph and defines labeled-node splits for node classification.
- PyG graph data construction: Describe how the preprocessed node features, edge indices, and node labels become a PyG Data object.
Why it matters: This object is the graph data structure used for the classification task. - Known-label node selection: Explain why the classification datasets are defined using nodes with known labels and how the known_mask tensor is used to determine their size.
Why it matters: The split sizes must be based on nodes whose licit or illicit labels are known. - Dataset masks and class checks: Describe how training, validation, and testing index masks are added to the PyG Data object, and what the split statistics let you inspect.
Why it matters: The masks identify each dataset, while the statistics make its class distribution visible.
Key terms: PyG Data object, node_features, edge_index, node_labels, known_mask, valid nodes, known labels, licit, illicit, training mask
12.1.4 Encoder–decoder architecture
This section shows how a homogeneous GNN encoder and a classification decoder are assembled for anti-money laundering node classification.
- Node classification encoder–decoder flow: Trace how the NodeClassifier uses a GNN to update each transaction node’s representation from its neighbors, then uses a decoder output to classify the node as licit or illicit.
Why it matters: This connects graph message passing to the AML classification decision. - Reusable homogeneous GNN encoders: Describe how the two-layer base graph model supports different homogeneous GNN encoders, and distinguish the SAGEConv and GATConv implementations described here.
Why it matters: The shared model makes it possible to compare encoders without treating every implementation as a separate architecture. - Classification output and loss: Explain why the decoder uses log_softmax and how CrossEntropyLoss uses predicted classification values and actual labels during training.
Why it matters: It explains both how the model produces usable classification output and how incorrect AML predictions drive learning.
Key terms: NodeClassifier, encoder, decoder, node representation, licit, illicit, homogeneous GNN, base graph model, SAGEConv, GATConv
12.1.5 Evaluation and analysis
Compares GCN, GAT, and SAGE for AML node classification using training cost, validation metrics, and test-set confusion matrices to explain why SAGE is the preferred model.
- Encoder training efficiency: Explain how the three encoders compare in training efficiency and why GAT has more learning parameters.
Why it matters: Model choice involves training cost as well as classification performance. - AML evaluation metrics: Distinguish precision, recall, and F1-score for licit-versus-illicit node classification, and describe why the evaluation uses a weighted average.
Why it matters: These metrics reveal different aspects of classification quality in an imbalanced AML dataset. - Validation performance comparison: Describe how the models’ validation precision, recall, and F1-scores compare and why recall alone does not establish the best model.
Why it matters: Similar recall can conceal meaningful differences in incorrect positive predictions. - Class-specific test results: Explain what the test-set confusion matrices add to the evaluation and how their licit-versus-illicit results support choosing SAGE for AML.
Why it matters: For AML, strong overall scores are less reassuring if illicit nodes are frequently labeled licit.
Key terms: GCN, GAT, SAGE, attention mechanism, learning parameters, training time, precision, recall, F1-score, weighted average
12.2 Link prediction for movie recommendations
This section frames movie recommendations as a graph link-prediction problem in which links represent interactions and preferences between users and movies.
- User–movie links for recommendations: Explain how a graph represents interactions and preferences between users and movies as links, making link prediction relevant to movie recommendations.
Why it matters: It connects the recommendation task to the graph relationships the model aims to predict.
Key terms: link prediction, interactions, preferences, user–movie links
12.2.1 Input data
Identifies the MovieLens input columns used for the movie-recommendation link prediction task.
- MovieLens link prediction inputs: Describe how the selected columns from movies.csv and ratings.csv identify movies, their genres, and user–movie rating records for the link prediction task.
Why it matters: These columns establish which movie information and user–movie records are available for the recommendation example.
Key terms: MovieLens, movies.csv, ratings.csv, movieId, userId, genres
12.2.2 Graph processor: Data preparation
This section prepares movie genre features and user–movie rating connections as numerical inputs for a PyG graph.
- Movie genre feature vectors: Describe how movie genres are represented numerically for GNN learning: each movie has a vector of 0s and 1s, with a 1 indicating membership in a genre, and a movie may belong to multiple genres.
Why it matters: This turns movie descriptions into features the GNN can process. - User movie edge index: Trace how rating connections are prepared: map original user and movie IDs to incremental IDs starting at 0, then use those new IDs to build an edge_index tensor connecting users and movies.
Why it matters: The remapped connections provide the rating graph structure needed for the next PyG step.
Key terms: genre information, vector of features, movie_feat, edge_index tensor, ID mappings, ratings
12.2.3 Graph processor: Heterogeneous PyG graph
This section builds a heterogeneous PyG graph for movie link prediction, separates edges for message passing and evaluation, and prepares sampled mini-batches.
- Heterogeneous movie graph representation: Explain why the user–movie graph uses PyG HeteroData rather than Data, and describe how it represents node features and the rates relationship.
Why it matters: The graph representation must preserve which nodes and relationships the recommendation model is working with. - Reverse edges for message passing: Explain why reverse edges are added to the heterogeneous graph and distinguish their role from edges used to train link prediction.
Why it matters: Direction and purpose matter when preparing relationships for a heterogeneous GNN. - Disjoint link prediction edges: Describe how RandomLinkSplit partitions rating links and why the training edges are further separated into message-passing and supervision sets.
Why it matters: Keeping message-passing and supervised links distinct clarifies which edges the model sees as graph structure and which it is asked to predict. - Evaluation context and neighbor sampling: Distinguish the edges supplied for message passing from those used to evaluate validation and test predictions, and describe how LinkNeighborLoader prepares mini-batches.
Why it matters: Evaluation needs graph context distinct from the links being judged, while sampled subgraphs make larger graphs practical to process.
Key terms: HeteroData, Data, users, movies, rates, edge_index, reverse edges, message passing, rev_edge_types, RandomLinkSplit
12.2.4 Encoder–decoder architecture
Explains how a heterogeneous GNN encoder creates user and movie representations and how a dot-product decoder uses them for movie link prediction.
- User and movie input embeddings: Distinguish how the encoder creates initial embeddings for users and movies: users have no intrinsic features, while movies have genre features.
Why it matters: The two node types need different starting representations before the GNN can refine them. - Heterogeneous neighborhood aggregation: Explain how the heterogeneous GNN refines embeddings using user–movie connections and how PyG metadata guides that process.
Why it matters: The refined embeddings reflect interactions in the graph, not just each node's starting representation. - Dot-product link scores: Describe how the decoder uses encoded user and movie embeddings to score a possible rating link.
Why it matters: This turns the encoder's node representations into a score for a potential recommendation. - Probability and training loss: Explain how the dot-product score is used with sigmoid activation and binary cross-entropy during training.
Why it matters: The loss connects the decoder's scores to labeled interactions so the model can improve its predictions.
Key terms: embedding matrix, user_input_dim, out_dim, movie genres, heterogeneous GNN, to_hetero(), metadata, HeteroData, neighborhood’s feature aggregation, decoder
12.2.5 Evaluation and analysis
The section compares the efficiency and recommendation performance of GCN, GAT, and SAGE link-prediction models using validation metrics and test-set confusion matrices.
- Model efficiency and complexity: Explain how the three link-prediction models compare in efficiency and why they have more parameters and longer training times than the node-classification models.
Why it matters: Predictive performance is only part of choosing a recommendation model; training cost matters too. - Recommendation evaluation metrics: Describe what precision, recall, and F1-score mean when predicting user–movie links.
Why it matters: These metrics distinguish irrelevant recommendations from relevant movies the model fails to find. - Validation performance tradeoffs: Compare the models' validation precision, recall, and F1-score, including the recommendation tradeoff each result implies.
Why it matters: The preferred model depends on whether avoiding irrelevant suggestions or finding more potential ratings matters most. - Test confusion matrix interpretation: Explain how the test-set confusion matrices support the comparison of missed ratings and irrelevant recommendations across SAGE, GCN, and GAT.
Why it matters: The confusion matrices make the practical cost of each model's errors visible.
Key terms: SAGE, GAT, GCN, embedding layer, heterogeneous GNN models, parameters, precision, recall, F1-score, user–movie links
12.3 Summary
Concepts explained in this chapter
- GraphSAGEGraphSAGE is an inductive graph representation-learning framework that generates node representations by aggregating information from sampled neighborhoods.
- graph attention networkA graph attention network is a graph neural network that uses learned attention weights to aggregate information from neighboring nodes.
- graph convolutional networkA graph convolutional network is a graph-neural-network model that updates node representations by aggregating information from graph neighborhoods.
- confusion matrixA confusion matrix is a tabular summary of predicted and actual class assignments, including counts of correct and incorrect classifications.
- f-scoreF1 score is the harmonic mean of precision and recall.
- heterogeneous graphA heterogeneous graph is a graph containing multiple types of nodes, relationships, or both.
- encoder-decoder architectureAn encoder-decoder architecture is a model design in which an encoder transforms input data into a learned representation and a decoder transforms that representation into predictions or reconstructed outputs.
- PyTorch GeometricPyTorch Geometric is an open-source library that provides data structures and neural-network layers for geometric and graph machine learning with PyTorch.
- link predictionLink prediction is a graph-learning technique that forecasts missing, future, or otherwise unobserved connections between nodes, optionally assigning scores or probabilities to candidate links.
- cross entropy lossCross-entropy loss is a classification loss that penalizes incorrect probability assignments by comparing predicted probabilities with target labels.
- edge indexAn edge index is a tensor-based representation that encodes graph edges as pairs of source and destination node indices.
- embeddingA feature vector is a numerical vector whose components represent measurable characteristics of an input for machine learning.
- precisionPrecision is the proportion of predicted positive instances that are correct.
- recallRecall is the proportion of relevant positive instances that are successfully identified.
- encoderAn encoder is a model component that transforms input data into a learned representation, such as vectors.
- heterogeneous graph neural networkA heterogeneous graph neural network is a graph neural network designed to learn representations from graphs containing multiple node or relationship types.
- inner productA dot product is the scalar obtained by multiplying corresponding components of two equal-length vectors and summing the results.
- node classificationNode classification is a graph-learning task that predicts one or more labels for each node from labeled examples and graph information.
- preprocessingData preparation is the process of selecting, cleaning, transforming, and organizing data for computational use.
- graphA graph structure is a data representation consisting of interconnected vertices and edges.
- graph neural networkA graph neural network is a neural network architecture that learns representations by propagating information among nodes and edges of a graph.
- sigmoidA sigmoid activation maps a real-valued score to a value between zero and one, commonly interpreted as a probability for a binary outcome.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
