TopoModelX (TMX)
repository·main·Indexed 43 days ago
https://github.com/pyt-team/topomodelxA Python module for topological deep learning (TDL) used to implement topological neural networks (TNNs) for science and engineering applications. It provides specialized components for modeling cell complexes, hypergraphs, and simplicial complexes, including implementations like the Simplicial Attention Network (SAN) and Cell Attention Network (CAN). The package includes tools for handling Laplacians and incidence matrices, and integrates with TopoNetX and PyTorch.
What's inside TopoModelX
- TopoModelX (TMX) is a Python package designed for Topological Deep Learning (TDL). It enables performing deep learning on topological domains, following the TDL blueprint established in recent research. The package is built to reproduce and extend topological neural networks (TNNs).
Overview of TopoModelX modules
mainThe
TopoModelXpackage is organized into three primary modules that handle different aspects of topological modeling:base: Implements core mathematical concepts, specifically the fundamental mechanics of message passing.nn: Contains neural network layers, which are categorized by their topological domains.utils: Provides utility functions, such as operations for computing with sparse tensors.
Overview of the TopoModelX nn module
mainThe
nnmodule in TopoModelX provides neural network components specifically designed for topological data structures. It is organized into three primary submodules based on the type of topological structure being modeled:cell: Components for cell complexes.hypergraph: Components for hypergraph-based neural networks.simplicial: Components for simplicial complex-based neural networks.
Users should select the submodule that corresponds to the mathematical structure of their input data (e.g., use
simplicialfor simplicial complexes orhypergraphfor hypergraphs).Overview of the Cell base class components
mainThe
Cellbase class in TopoModelX is implemented through six primary classes that form the core of the neural network cell architecture. These classes are categorized into layers and their corresponding functional units:- CAN:
Can_LayerandCan - CCXN:
CCXN_LayerandCCXN - CWN:
CWN_LayerandCWN
- CAN:
Requirements for implementing a TopoModelX layer
mainWhen implementing a
{Name of model}Layer, follow these structural and technical guidelines:- Domain Placement: Store the file in
topomodelx/nn/simplicial,topomodelx/nn/cell,topomodelx/nn/hypergraph, ortopomodelx/nn/combinatorialbased on the model's domain. - Primitives: Use TopoModelX computational primitives. If you must modify
topomodelx/baseto add new primitives, you must provide unit tests for every new function added. - Logic: One layer should be equivalent to the message passing depicted in the tensor diagram representation for the model.
- Testing: Use
pytest(notunittest) for all unit tests.
- Domain Placement: Store the file in
Understand the components of the Base class
mainThe
Baseclass in TopoModelX is a foundational structure composed of three primary functional layers used to build topological models:- Aggregation: Defines the message passing layer, determining how information is combined.
- Conv: Responsible for building the message passing neighborhood structure.
- Message Passing: The core class defining the message passing structure itself.
Getting started with TopoModelX
mainTo begin using TopoModelX, refer to the official tutorials which provide a step-by-step guide through loading datasets, preparing topological features (like Laplacians), defining networks, and performing forward passes.Install TopoModelX via pip
mainYou can install the core
topomodelxpackage usingpip. Note that you must also installtorchand specific PyTorch geometric dependencies (torch-scatter,torch-sparse, andtorch-cluster) compatible with your CUDA version to ensure full functionality.pip install topomodelx # Example for CUDA 11.3 (replace ${CUDA} with cpu, cu102, cu113, or cu115) pip install torch==2.0.1 --extra-index-url https://download.pytorch.org/whl/cu113 pip install torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-2.0.1+cu113.html pip install torch-cluster -f https://data.pyg.org/whl/torch-2.0.0+cu113.htmlDataset loading and lifting for TNN tutorials
mainWhen creating the
{name_of_model}_train.ipynbtutorial, use the following patterns for data preparation:For models requiring features on faces or rank-2 neighborhood matrices:
Use TopoNetX to load:
shrec16: Suitable for complex-level classification.Karate Club: Suitable for node-level classification.
For models requiring only node/edge features and rank-0/1 neighborhood matrices:
Use TopoNetX or Torch Geometric to load benchmark graph datasets.
Lifting to Topological Domains:
After loading the graph dataset, you must lift it from the graph domain to the chosen topological domain (hypergraph, simplicial complex, cell complex, or combinatorial complex) using TopoNetX.
Explore Topological Neural Net tutorials
mainTopoModelX provides specialized tutorials for implementing Topological Neural Networks (TNNs) across different mathematical structures. You can find detailed notebooks for the following domains:
- Cellular Complexes: Tutorials located in
../notebooks/cell/. - Hypergraphs: Tutorials located in
../notebooks/hypergraph/. - Simplicial Complexes: Tutorials located in
../notebooks/simplicial/.
Each tutorial typically follows a workflow of loading a dataset, preparing Laplacians and features, defining the network architecture, and performing forward passes.
- Cellular Complexes: Tutorials located in
Submit a Topological Neural Network (TNN) implementation
mainTo contribute a new model to TopoModelX, you must submit a Pull Request containing a specific set of files that implement a pre-existing model from the literature (specifically those listed in Fig. 11 of the survey Architectures of Topological Deep Learning).
Your submission must include three specific files:
Model Layer:
{name_of_model}_layer.py- Location:
topomodelx/nn/{domain}(where domain issimplicial,cell,hypergraph, orcombinatorial). - Requirement: Must contain a single class
{Name of model}Layerthat uses TopoModelX computational primitives to implement one layer (one message passing step).
- Location:
Training Notebook:
{name_of_model}_train.ipynb- Location:
tutorials/{domain}. - Requirement: Must demonstrate:
- Pre-processing: Importing the layer, loading a dataset (e.g.,
shrec16orKarate Clubvia TopoNetX), and lifting the dataset to the target topological domain. - Network Creation: A class
{Name of model}inheriting fromtorch.nn.Moduleusing your layer. - Training: A simple training loop for a classification task.
- Pre-processing: Importing the layer, loading a dataset (e.g.,
- Location:
Unit Tests:
test_{name_of_model}_layer.py- Location:
test/nn/{domain}. - Requirement: Must contain a class
Test{Name of model}Layerusingpytestto test all functions in your layer class.
- Location:
Important: All code must pass the TopoModelX GitHub Action workflow, including tests, linting (flake8), and formatting (Black, isort).
Develop TopoModelX from source
mainTo contribute to TMX, it is recommended to use Python 3.12 or later. Follow these steps to set up a local development environment:
- Create and activate a conda environment:
conda create -n tmx python=3.12 conda activate tmx - Clone the repository and install in editable mode:
Note: On Windows, usegit clone git@github.com:pyt-team/TopoModelX.git cd TopoModelX pip install -e '.[all]'pip install -e .[all]without quotes. - Install PyTorch and its dependencies (torch-scatter, torch-sparse, torch-cluster) as described in the installation guide.
- Run the test suite using
pytestand install pre-commit hooks withpre-commit install.
- Create and activate a conda environment: