Skip to main content

The Graph Pooling library for PyTorch Geometric.

Project description



Torch Geometric Pool

The library for pooling in Graph Neural Networks


PyPI PyPI - Python Version CI status codecov Documentation Status Total Downloads

📚 Documentation - 🚀 Getting Started - 💻 Introductory notebooks

tgp (Torch Geometric Pool) is a library that provides a broad suite of graph pooling layers to be inserted into Graph Neural Network architectures built with PyTorch Geometric . With tgp, you can effortlessly construct hierarchical GNNs by interleaving message-passing layers with any pooling operations.

Features

  • Unified API. All pooling layers in tgp are implemented following the SRC (Select, Reduce, Connect) framework, introduced in Understanding Pooling in Graph Neural Networks, which ensures a consistent API across all methods and seamless interoperability.

  • All your pooling operators in one place. Choose from a variety of pooling methods, including sparse techniques like Top-K, NDPPooling, GraclusPooling, and dense methods such as DiffPool and MinCutPooling. Each operator adheres to the modular SRC framework, allowing you to quickly exchange methods within the same GNN architecture and combine them with standard message-passing layers.

  • Precomputed & On-the-Fly Pooling. Accelerate training by precomputing the coarse graph (assignments and connectivity) for methods like NDPPooling or GraclusPooling. Alternatively, use on-the-fly pooling (e.g., Top-K or MinCut) that computes assignments dynamically and supports end-to-end gradient flow.

  • Alias-Based Instantiation. Quickly create any pooler by name (e.g., "topk", "ndp", "diff", "mincut"). Pass a configuration dict for hyperparameters, and receive a fully initialized pooling layer that conforms to the unified SRC interface.

  • Tweak and create new pooling layers. Thanks to the modular SRC framework, the components of different pooling layers in tgp can be easily combined, replaced with existing modules, or swapped for entirely new ones.

Getting Started

If you are unfamiliar with graph pooling, we recommend checking this introduction to the SRC framework and this blog for a deeper dive into pooling in GNNs. Before you dive into using tgp, we recommend browsing the Documentation to familiarize yourself with the API.

If you prefer a notebook-based introduction, check out the following tutorials:

  • nbviewer Basic usage of common pooling operators, including how to pass arguments via aliases and inspect intermediate outputs.

  • nbviewer Demonstrates how to apply precomputed pooling methods and associated data transforms for faster training.

  • nbviewer Deep dive into the SRC framework, showing how each component interacts and how to use the select, reduce, connect and lift operations to modify the graph topology and the graph features.

In addition, check the example folder for a collection of minimalistic python script showcasing the usage of the pooling operators of tgp in all the most common downstream tasks, such as graph classification, node classification, and node clustering.

Installation

tgp is compatible with Python>=3.9. We recommend installation on a Anaconda or Miniconda environment or a virtual env. tgp is conveniently available as a Python package on PyPI and can be easily installed using pip.

pip install torch-geometric-pool

For the latest version, consider installing from source:

pip install git+https://github.com/tgp-team/torch-geometric-pool.git

[!CAUTION] tgp is built upon PyTorch>=1.8 and PyG>=2.6. Make sure you have both installed in your environment before installation. Check the installation guide for more details.

Quick Example

import torch
from torch_geometric.data import Data
from torch_geometric.nn import GCNConv
from tgp.poolers import get_pooler

# Create a simple graph (5 nodes, 5 edges)
edge_index = torch.tensor([[0, 1, 2, 3, 4, 0],
                           [1, 0, 3, 2, 0, 4]], dtype=torch.long)
x = torch.randn((5, 16))  # node features

data = Data(x=x, edge_index=edge_index)

# Instantiate a Top-K pooling layer via its alias
pool = get_pooler("topk", in_channels=32, ratio=0.5)

# Forward pass with pooling
out = GCNConv(in_channels=16, out_channels=32)(data.x, data.edge_index)
out = pool(out, data.edge_index, batch=None)  # PoolingOutput(so=[5, 3], x=[3, 32], edge_index=[2, 2])
out = GCNConv(in_channels=32, out_channels=32)(out.x, out.edge_index)

For more detailed examples, refer to the Tutorials and the coding Examples.

Contributing

Contributions are welcome! For major changes or new features, please open an issue first to discuss your ideas. See the Contributing guidelines for more details on how to get involved. Help us build a better tgp!

Thanks to all contributors 🤝

Citing

If you use Torch Geometric Pool for your research, please consider citing the paper:

@misc{bianchi2025torchgeometricpoolpytorch,
      title={Torch Geometric Pool: the Pytorch library for pooling in Graph Neural Networks}, 
      author={Filippo Maria Bianchi and Carlo Abate and Ivan Marisca},
      year={2025},
      eprint={2512.12642},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2512.12642}, 
}

By Filippo Maria Bianchi, Ivan Marisca and Carlo Abate.

License

This project is licensed under the terms of the MIT license. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_geometric_pool-1.0.2.tar.gz (155.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_geometric_pool-1.0.2-py3-none-any.whl (220.3 kB view details)

Uploaded Python 3

File details

Details for the file torch_geometric_pool-1.0.2.tar.gz.

File metadata

  • Download URL: torch_geometric_pool-1.0.2.tar.gz
  • Upload date:
  • Size: 155.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for torch_geometric_pool-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d58946af97a2eecd4e4c2c10ced6bafa19d755aad3a5bdafc943ca956dc6c1e0
MD5 d8a38f92dbe2688acbb8e0877b21f8f4
BLAKE2b-256 b870b1cbef91483e8abab5b8936d38bcd69c2b51471364796f241c77ac04cedc

See more details on using hashes here.

File details

Details for the file torch_geometric_pool-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_geometric_pool-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e66b0ee627ba846a2a0afe51ea18332b1cd2354e8629fad8d9a261b465b51618
MD5 8b5ed5c5862a75db03b9f5ae00293fc3
BLAKE2b-256 72ccbe7f9614971150918455f2872c553dd3540ba41a19bcecb21c484e6644b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page