7 2 graph level tasks
Detailed Graph-Level Tasks Examples for xtorch
This document expands the "Time Series and Graph Neural Networks -> Graph-Level Tasks" subcategory of examples for the xtorch library, a C++ deep learning framework that extends PyTorch’s LibTorch API with user-friendly abstractions. The goal is to provide a comprehensive set of beginner-to-intermediate examples that introduce users to graph-level tasks in graph neural networks (GNNs), showcasing xtorch’s capabilities in model building, training, data handling, and integration with C++ ecosystems. These examples are designed to be included in the xtorch-examples repository, helping users learn graph-level GNN tasks in C++.
Background and Context
xtorch simplifies deep learning for C++ developers by offering high-level model classes (e.g., XTModule, ResNetExtended, XTCNN), a streamlined training loop via the Trainer module, enhanced data utilities (e.g., ImageFolderDataset, CSVDataset), extended optimizers, and model serialization tools (e.g., save_model(), export_to_jit()). The original two graph-level task examples—DiffPool for graph classification and Message Passing Neural Networks (MPNNs) for molecular property prediction—provide a solid foundation. This expansion adds six more examples to cover additional architectures (e.g., GIN, GraphSAGE, EdgeConv), datasets (e.g., OGBG-MolHIV, ZINC, MUTAG), and techniques (e.g., graph pooling, transfer learning, real-time visualization), ensuring a broad introduction to graph-level tasks with xtorch.
The current time is 10:45 AM PDT on Monday, April 21, 2025, and all considerations are based on available information from the xtorch GitHub repository (xtorch GitHub Repository) without contradicting this timeframe.
Expanded Examples
The following table provides a detailed list of eight "Time Series and Graph Neural Networks -> Graph-Level Tasks" examples, including the original two and six new ones. Each example is designed to be standalone, with a clear focus on a specific graph-level GNN concept or xtorch feature, making it accessible for users.
| Category | Subcategory | Example Title | Description |
|---|---|---|---|
| Time Series and Graph Neural Networks | Graph-Level Tasks | Graph Classification with DiffPool | Implements DiffPool for graph classification on the PROTEINS dataset from TUDataset (protein structures). Uses xtorch’s xtorch::nn to perform differentiable graph pooling, trains with cross-entropy loss, and evaluates with accuracy and F1 score. |
| Molecular Property Prediction with Message Passing Neural Networks | Uses Message Passing Neural Networks (MPNNs) to predict molecular properties (e.g., solubility, dipole moment) on the QM9 dataset (small molecules). Uses xtorch to implement message passing and aggregation, trains with Mean Squared Error (MSE) loss, and evaluates with Mean Absolute Error (MAE). | ||
| Graph Classification with Graph Isomorphism Network (GIN) on OGBG-MolHIV | Trains a Graph Isomorphism Network (GIN) for graph classification on the OGBG-MolHIV dataset (HIV inhibition prediction). Uses xtorch’s xtorch::nn for expressive graph convolutions, trains with binary cross-entropy loss, and evaluates with ROC-AUC. |
||
| Graph Classification with GraphSAGE on TUDataset | Implements GraphSAGE for graph classification on the ENZYMES dataset from TUDataset (enzyme structures). Uses xtorch to aggregate neighborhood features for graph-level representations, trains with cross-entropy loss, and evaluates with accuracy and macro-F1 score. | ||
| Graph Property Prediction with EdgeConv on ZINC | Trains an EdgeConv-based GNN for graph property prediction (e.g., molecular energy) on the ZINC dataset (chemical compounds). Uses xtorch to model edge features in convolutions, trains with MSE loss, and evaluates with MAE and Root Mean Squared Error (RMSE). | ||
| Transfer Learning with MPNN for Graph Classification | Fine-tunes a pre-trained MPNN model from one graph dataset (e.g., QM9) to another (e.g., OGBG-MolPCBA for bioactivity prediction). Uses xtorch’s model loading utilities to adapt the model, trains with binary cross-entropy loss, and evaluates with adaptation performance (ROC-AUC improvement) and training efficiency. | ||
| Real-Time Graph Classification with xtorch and OpenCV | Combines xtorch with OpenCV to perform real-time graph classification on dynamic molecular graphs (e.g., a subset of TUDataset’s MUTAG). Uses a trained DiffPool model to classify graphs, visualizes graph labels in a GUI, and evaluates with qualitative classification accuracy, highlighting C++ ecosystem integration. | ||
| Graph Classification with Global Attention Pooling on MUTAG | Implements a GNN with global attention pooling for graph classification on the MUTAG dataset (mutagenic molecules). Uses xtorch to focus on important nodes via attention, trains with cross-entropy loss, and evaluates with accuracy and F1 score. |
Rationale for Each Example
- Graph Classification with DiffPool: Introduces DiffPool, a hierarchical pooling GNN, using PROTEINS for its relevance to bioinformatics. It’s beginner-friendly and teaches graph pooling basics.
- Molecular Property Prediction with Message Passing Neural Networks: Demonstrates MPNNs, a versatile GNN framework, using QM9 to teach molecular property prediction, relevant for cheminformatics.
- Graph Classification with Graph Isomorphism Network (GIN) on OGBG-MolHIV: Introduces GIN, a highly expressive GNN, using OGBG-MolHIV to teach graph classification for drug discovery applications.
- Graph Classification with GraphSAGE on TUDataset: Demonstrates GraphSAGE for graph-level tasks, using ENZYMES to teach scalable neighborhood aggregation, suitable for diverse graph datasets.
- Graph Property Prediction with EdgeConv on ZINC: Introduces EdgeConv, which leverages edge features, using ZINC to teach regression tasks in molecular modeling.
- Transfer Learning with MPNN for Graph Classification: Teaches transfer learning, a practical technique for reusing models across graph datasets, using QM9 and OGBG-MolPCBA to show adaptation efficiency.
- Real-Time Graph Classification with xtorch and OpenCV: Demonstrates real-time GNN applications, integrating xtorch with OpenCV to visualize dynamic graph classification, relevant for interactive chemical analysis.
- Graph Classification with Global Attention Pooling on MUTAG: Introduces global attention pooling, a modern pooling technique, using MUTAG to teach attention-based graph classification for small molecular graphs.
Implementation Details
Each example should be implemented as a standalone C++ program in the xtorch-examples repository, with the following structure:
- Source Code: A main.cpp file containing the example code, using xtorch’s API (e.g., xtorch::nn, xtorch::data, xtorch::optim) and, where applicable, OpenCV for visualization.
- Build Instructions: A CMakeLists.txt file to compile the example, linking against xtorch, LibTorch, and OpenCV (if needed).
- README.md: A detailed guide explaining the example’s purpose, prerequisites (e.g., LibTorch, graph dataset downloads, OpenCV), steps to run, and expected outputs (e.g., accuracy, F1 score, MAE, ROC-AUC, or visualized graph labels).
- Dependencies: Ensure users have xtorch, LibTorch, and graph datasets (e.g., TUDataset, QM9, OGBG-MolHIV, ZINC, OGBG-MolPCBA, MUTAG) installed, with download instructions in each README. For OpenCV integration, include setup instructions. Graph data handling may require libraries like PyTorch Geometric’s C++ equivalents or custom graph utilities in xtorch.
For example, the “Graph Classification with Graph Isomorphism Network (GIN) on OGBG-MolHIV” might include:
- Code: Define a GIN model with xtorch::nn for graph convolutions with sum aggregation, process OGBG-MolHIV graph data (nodes, edges, features), train with binary cross-entropy loss using xtorch::optim::Adam, and evaluate ROC-AUC using xtorch’s metrics module.
- Build: Use CMake to link against xtorch and LibTorch, specifying paths to OGBG-MolHIV data.
- README: Explain GIN’s expressive power and its role in molecular classification, provide compilation commands, and show sample output (e.g., ROC-AUC of ~0.80 on OGBG-MolHIV test set).
Why These Examples?
These examples are designed to: - Cover Core Concepts: From DiffPool and MPNNs to GIN, GraphSAGE, EdgeConv, and global attention pooling, they introduce key graph-level GNN paradigms, covering classification and regression tasks. - Leverage xtorch’s Strengths: They highlight xtorch’s high-level API, graph data utilities, and C++ performance, particularly for scalable models like GraphSAGE and real-time applications. - Be Progressive: Examples start with simpler models (DiffPool, MPNN) and progress to complex ones (GIN, EdgeConv), supporting a learning path. - Address Practical Needs: Techniques like graph pooling, transfer learning, and real-time classification are widely used in real-world graph applications, from drug discovery to material science. - Encourage Exploration: Examples like GIN and global attention pooling expose users to cutting-edge GNN techniques, fostering innovation.
Feasibility and Alignment with xtorch
The examples are feasible given xtorch’s features, as outlined in its GitHub repository:
- Model Building: xtorch::nn::Sequential, Linear, and custom modules support defining DiffPool, MPNN, GIN, GraphSAGE, EdgeConv, and attention-based pooling architectures.
- Data Handling: xtorch::data::CSVDataset and custom graph utilities handle graph datasets (nodes, edges, features, labels), with support for batching and graph-level labels.
- Training: The Trainer API and optimizers (e.g., xtorch::optim::Adam) simplify training and support losses like cross-entropy, MSE, and binary cross-entropy.
- Evaluation: xtorch’s metrics module supports accuracy, F1 score, MAE, RMSE, and ROC-AUC, critical for graph-level tasks.
- C++ Integration: xtorch’s compatibility with OpenCV enables real-time visualization of graph structures and labels.
The examples align with xtorch’s goal of simplifying deep learning in C++, making them ideal for the xtorch-examples repository’s graph-level GNN section.
Comparison with Existing Practices
Popular deep learning libraries like PyTorch Geometric provide GNN tutorials, such as “Graph Classification with GIN” (PyTorch Geometric Tutorials), which covers GIN on molecular datasets. The proposed xtorch examples mirror this approach but adapt it to C++, emphasizing xtorch’s unique features like the Trainer API, real-time performance, and OpenCV integration. They also include modern GNN architectures (e.g., EdgeConv, global attention pooling) and tasks (e.g., real-time classification, transfer learning) to stay relevant to current trends, as seen in repositories like “pyg-team/pytorch_geometric” (GitHub - pyg-team/pytorch_geometric).
Implementation Notes
- Directory Structure: Organize
xtorch-exampleswith atime_series_and_graph_neural_networks/graph_level_tasks/directory, containing subdirectories for each example (e.g.,diffpool_proteins/,mpnn_qm9/). - User Guidance: The main
README.mdshould list all examples, suggest a learning path (e.g., start with DiffPool, then GIN, then EdgeConv), and link to xtorch’s documentation. - C++ Focus: Ensure code uses modern C++ practices (e.g., smart pointers, exception handling) and includes detailed comments for clarity.
- Dependencies: Note that users need LibTorch, xtorch, graph datasets (e.g., TUDataset, QM9, OGBG-MolHIV, ZINC, OGBG-MolPCBA, MUTAG), and optionally OpenCV installed, with download and setup instructions in each README. Graph data handling may require custom utilities or integration with C++ graph libraries.
Conclusion
The expanded list of eight "Time Series and Graph Neural Networks -> Graph-Level Tasks" examples provides a comprehensive introduction to graph-level GNN tasks with xtorch, covering DiffPool, MPNN, GIN, GraphSAGE, EdgeConv, global attention pooling, transfer learning, and real-time classification. These examples are beginner-to-intermediate friendly, leverage xtorch’s strengths, and align with its goal of making deep learning accessible in C++. By including them in xtorch-examples, you can help users build a solid foundation in graph-level GNN tasks, fostering adoption and engagement with the xtorch community.