Deep learning for molecular discovery with a simple sklearn-style interface
torch-molecule is a package that facilitates molecular discovery through deep learning, featuring a user-friendly, sklearn-style interface. It includes model checkpoints for efficient deployment and benchmarking across a range of molecular tasks. The package focuses on three main components: Predictive Models, Generative Models, and Representation Models, which make molecular AI models easy to implement and deploy.
See the List of Supported Models section for all available models.
Installation
Python 3.10 or newer is required; GenMol and Molexar require Python 3.11 or newer.
-
Create a mamba environment:
micromamba create --name torch-molecule --channel conda-forge python=3.12 pip eval "$(micromamba shell hook --shell bash)" micromamba activate torch-molecule
-
Install using pip:
pip install torch-molecule
-
Install from source for the latest version:
Clone the repository:
git clone https://github.com/liugangcode/torch-molecule cd torch-molecule
Install:
pip install .
Additional Packages
Optional model requirements install as pip extras, e.g. pip install "torch-molecule[molgen]" (or pip install ".[molgen]" from a source checkout):
| Extra | Installs | For |
|---|---|---|
hf |
torch>=2.6, transformers | HFPretrainedMolecularEncoder, HFPretrainedMolecularGenerator |
molgen |
torch>=2.6, transformers, selfies>=2.1 | MolGen |
molexar |
torch>=2.6, transformers>=5.8, loguru | Molexar (Python 3.11+); also install Fragment-SELFIES and Molexar from source |
safe |
torch>=2.6, transformers, safe-mol | SAFE-GPT |
genmol |
transformers==4.57.1, safe-mol>=0.2.1,<0.3 | GenMol (Python 3.11+) |
scatter |
torch-scatter | BFGNN, GRIN |
grin |
torch-scatter, CombineMols | GRIN with repetition_augmentation=True |
Hugging Face pretrained generators require PyTorch 2.6 or newer.
Use separate environments for GenMol and Molexar: GenMol requires
transformers==4.57.1, while Molexar requires transformers>=5.8.
After installing the molexar extra, install its source packages:
python -m pip install git+https://github.com/yiyanliao/Fragment-SELFIES.git git+https://github.com/yiyanliao/Molexar.git
Install a torch-scatter wheel matching your PyTorch and CUDA versions using
the PyTorch Geometric installation guide.
from torch_molecule import HFPretrainedMolecularGenerator
model = HFPretrainedMolecularGenerator(
repo_id="datamol-io/safe-gpt",
)
model.fit()
print(model.generate(n_samples=5))
print(model.generate(n_samples=5, scaffold="c1ccccc1"))
Usage
More examples can be found in the
examplesfolder.
Datasets
Explore small-molecule and polymer datasets in the molecular dataset hub, with original sources, licenses, units, source splits, and quality tags.
See the dataset documentation for dataset selection, caching, target columns, and train/test splits.
Current counts follow the hub's published metadata. Hugging Face entries include public and gated datasets and files without SMILES tables. Source rows may repeat structures; each dataset retains its own license.
Load any hub dataset by name, the way scikit-learn loads its datasets:
from torch_molecule.datasets import list_datasets, load_dataset
X, y = load_dataset("qm9", targets=["gap"], return_X_y=True) # SMILES list, (133885, 1) array
data = load_dataset("moleculeace", "chembl204_ki", split="train") # one of a dataset's configs
data.info["title"] # 'Prothrombin (CHEMBL204) · Ki'
list_datasets("thrombin") # search names, titles, and label names
list_datasets(category="adme", grade="ready")
More options, all keywords of load_dataset: scheme= picks one of several source splits (WelQrate's ten),
columns= returns other source columns in data.extra, task_values= selects assays of tables with one row
per measurement (task_values(name) lists them), and load_table / download_files return every column or
the native files (3D structures, spectra, images). See the dataset documentation.
load_qm9, load_chembl2k, load_broad6k, load_toxcast, load_admet, load_gasperm, and load_zinc250k still work but are deprecated aliases of load_dataset and will be removed.
(We are actively adding more datasets. We welcome your suggestions and contributions on your datasets!)
Fit a Model
After preparing the dataset, split it, then fit a model with an sklearn-style API (no extra SMILES featurization is required):
from torch_molecule.datasets import load_dataset
from torch_molecule import GREAMolecularPredictor
data = load_dataset("qm9", targets=["gap"])
# "random" | "scaffold" | "butina" | "size"
# scaffold: unseen Bemis-Murcko scaffolds; butina: Tanimoto clusters; size: heavy-atom count
train, val = data.train_test_split(test_size=0.2, method="scaffold", seed=42)
grea = GREAMolecularPredictor(
num_task=1,
task_type="regression",
evaluate_higher_better=False,
verbose="progress_bar" #or "print_statement" recommended for jupyter notebooks, or "none"
)
# Fit with automatic hyperparameter tuning with 10 attempts, or implement .fit() with the default/manual hyperparameters
grea.autofit(
X_train=train.data,
y_train=train.target,
X_val=val.data,
y_val=val.target,
n_trials=10,
)
Random splitting assigns individual rows, so repeated structures can occur
in both splits. The splitters preserve data.groups metadata but do not use it
to assign rows. Use scaffold or butina, or split by group yourself, when
repeated measurements must stay together.
Checkpoints
torch-molecule provides checkpoint functions that can be interacted with on Hugging Face:
from torch_molecule import GREAMolecularPredictor
repo_id = "user/repo_id" # replace with your own Hugging Face username and repo_id
# Save the trained model to Hugging Face
grea.save_to_hf(
repo_id=repo_id,
task_id="qm9_grea",
commit_message="Upload qm9_grea",
private=False
)
# Load a pretrained checkpoint from Hugging Face
model = GREAMolecularPredictor()
model.load_from_hf(repo_id=repo_id, local_cache="GREAMolecularPredictor.pt")
# Adjust model parameters and make predictions
model.set_params(verbose='none')
predictions = model.predict(smiles_list)
Or you can save the model to a local path:
grea.save_to_local("qm9_grea.pt")
new_model = GREAMolecularPredictor()
new_model.load_from_local("qm9_grea.pt")
List of Supported Models
Predictive Models
Generative Models
Representation Models
Acknowledgements
The project template was adapted from https://github.com/lwaekfjlk/python-project-template. We thank the authors for their contribution to the open-source community.
This project is initiated and maintained by Gang Liu and Meng Jiang, and would not be possible without the valuable contributions from the open-source community, including the following members:
|
Man Hei Matthew Thom Integrating Pretrained Generator (NovoMolGen, MolGen, Molexar, SAFE-GPT), dataset splitting modules |
Eric Inae Integrating MoAMa, GraphMAE, AttrMasking, ContextPred, EdgePred |
Yihan Zhu Integrating DeFoG, GRIN, BFGNN, RPGNN, Transformer (SMILES) |
Monica C. S Debugging and integrating training logs (#19) |
Metadata
Release files for torch-molecule 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_molecule-0.2.1.tar.gz | 3.0 MB | Without provenance | Details for torch_molecule-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_molecule-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for torch_molecule-0.2.1-py3-none-any.whl |
Total release size: 3.5 MB
Release files / torch_molecule-0.2.1.tar.gz
| Download URL | torch_molecule-0.2.1.tar.gz |
|---|---|
| Size | 3.0 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e42ca03fd899dba41a47314291578b81a58f0f9fa1b7d126c813686bb5d62e35
|
|
BLAKE2b-256 checksum How to use checksums |
19ff1976992a98ab2b9b127ab2d17109b3171953809cbb64fafc64cf040ce0aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Release files / torch_molecule-0.2.1-py3-none-any.whl
| Download URL | torch_molecule-0.2.1-py3-none-any.whl |
|---|---|
| Size | 501.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42cebbc85e68763124619aeffa39b1e91c4e622a33e9c81980408b4776e73830
|
|
BLAKE2b-256 checksum How to use checksums |
a612154829766aca6faf69baa0dd0aa8c9522f8bfb0ed0d09e982d6a1b65111e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Security for torch-molecule 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.