liugangcode/torch-molecule

torch-molecule is a deep learning package for molecular discovery, designed with an sklearn-style interface for property prediction, inverse design and representation learning.

Python

332

237 commits

updated Sep 24, 2026

See the code

README

torch-molecule logo

GitHub Repository Documentation

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.

scikit-learn vs torch-molecule comparison

See the List of Supported Models section for all available models.

Installation

  1. Create a Conda environment:

    conda create --name torch_molecule python=3.11.7
    conda activate torch_molecule
    
  2. Install using pip:

    pip install torch-molecule
    
  3. 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

ModelRequired Packages
HFPretrainedMolecularEncodertransformers
HFPretrainedMolecularGeneratortransformers
HFPretrainedMolecularGenerator (MolGen)transformers, selfies
HFPretrainedMolecularGenerator (Molexar)transformers, fragment-selfies, molexar
HFPretrainedMolecularGenerator (SAFE-GPT)transformers, safe-mol
BFGNNMolecularPredictortorch-scatter
GRINMolecularPredictortorch-scatter
GRINMolecularPredictor (if enable repetition_augmentation=True)CombineMols

For models that require torch-scatter: Install using the following command: pip install torch-scatter -f https://data.pyg.org/whl/torch-${TORCH}+${CUDA}.html, e.g.,

pip install torch-scatter -f https://data.pyg.org/whl/torch-2.7.1+cu128.html

For models that require transformers: pip install transformers

For MolGen (selfies): pip install "selfies>=2.1". Source: aspuru-guzik-group/selfies.

For Molexar: pip install fragment-selfies loguru (Fragment-SELFIES) and pip install git+https://github.com/fairydance/Molexar.git (Molexar). Molexar itself requires transformers>=5.8.

For SAFE-GPT: pip install safe-mol (SAFE).

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 examples and tests folders.

torch-molecule supports applications in broad domains from chemistry, biology, to materials science. To get started, you can load prepared datasets from torch_molecule.datasets (updated after v0.1.3):

DatasetDescriptionFunction
qm9Quantum chemical properties (DFT level)load_qm9
chembl2kBioactive molecules with drug-like propertiesload_chembl2k
broad6kBioactive molecules with drug-like propertiesload_broad6k
toxcastToxicity of chemical compoundsload_toxcast
admetChemical absorption, distribution, metabolism, excretion, and toxicityload_admet
gaspermSix gas permeability properties for polymeric materialsload_gasperm
zinc250kA common subset of ZINC dataset, which does not have labels and could be used for unconditional generation or virtual screeningload_zinc250k
from torch_molecule.datasets import load_qm9

# local_dir is the local path where the dataset will be saved
molecular_data = load_qm9(local_dir='torchmol_data')
smiles_list, property_np_array = molecular_data.data, molecular_data.target

# len(smiles_list): 133885
# Property array shape: (133885, 1)

# load_qm9 returns the target "gap" by default, but you can adjust it by passing new target_cols
target_cols = ['homo', 'lumo', 'gap']
molecular_data = load_qm9(local_dir='torchmol_data', target_cols=target_cols)
smiles_list, property_np_array = molecular_data.data, molecular_data.target

# the target could be None if loading an unlabeled dataset
from torch_molecule.datasets import load_zinc250k
molecular_data = load_zinc250k(local_dir='torchmol_data')
smiles_list = molecular_data.data
assert molecular_data.target is None

(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_qm9
from torch_molecule import GREAMolecularPredictor

data = load_qm9(local_dir='torchmol_data')
# "random" | "scaffold" | "butina" | "size"
# scaffold: unseen Bemis-Murcko scaffolds; butina: Tanimoto clusters; size: heavy-atom count
# Split the full dataset. subsample() is only for local debugging / CI — do not
# shrink QM9 (or any benchmark) just to make Butina cheaper.
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,
)

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=f"{model_dir}/GREA_{task_name}.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

ModelReference
MoAMaMotif-aware Attribute Masking for Molecular Graph Pre-training. LoG 2024
GraphMAEGraphMAE: Self-Supervised Masked Graph Autoencoders. KDD 2022
AttrMaskingStrategies for Pre-training Graph Neural Networks. ICLR 2020
ContextPredStrategies for Pre-training Graph Neural Networks. ICLR 2020
EdgePredStrategies for Pre-training Graph Neural Networks. ICLR 2020
InfoGraphInfoGraph: Unsupervised and Semi-supervised Graph-Level Representation Learning via Mutual Information Maximization. ICLR 2020
SupervisedSupervised pretraining
PretrainedGPT2-ZINC-87M: GPT-2 based model (87M parameters) pretrained on ZINC dataset with ~480M SMILES strings.
RoBERTa-ZINC-480M: RoBERTa based model (102M parameters) pretrained on ZINC dataset with ~480M SMILES strings.
UniKi/bert-base-smiles: BERT model pretrained on SMILES strings.
ChemBERTa-zinc-base-v1: RoBERTa model pretrained on ZINC dataset with ~100k SMILES strings.
ChemBERTa series: Available in multiple sizes and training objectives (MLM/MTR). ChemBERTa-5M-MLM, ChemBERTa-5M-MTR, ChemBERTa-10M-MLM, ChemBERTa-10M-MTR, ChemBERTa-77M-MLM, ChemBERTa-77M-MTR.
ChemGPT series: GPT-Neo based models pretrained on PubChem10M dataset with SELFIES strings. ChemGPT-1.2B, ChemGPT-4.7B, ChemGPT-19B.

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
Man Hei Matthew Thom

Integrating Pretrained Generator (NovoMolGen, MolGen, Molexar, SAFE-GPT), dataset splitting modules
Eric Inae
Eric Inae

Integrating MoAMa, GraphMAE, AttrMasking, ContextPred, EdgePred
Yihan Zhu
Yihan Zhu

Integrating DeFoG, GRIN, BFGNN, RPGNN, Transformer (SMILES)
Monica C. S
Monica C. S

Debugging and integrating training logs (#19)
deep-learning
graph-neural-networks
inverse-design
molecule
property-prediction
representation-learning

liugangcode/torch-molecule

torch-molecule is a deep learning package for molecular discovery, designed with an sklearn-style interface for property prediction, inverse design and representation learning.

Python

332

237 commits

updated Sep 24, 2026

See the code

README

torch-molecule logo

GitHub Repository Documentation

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.

scikit-learn vs torch-molecule comparison

See the List of Supported Models section for all available models.

Installation

  1. Create a Conda environment:

    conda create --name torch_molecule python=3.11.7
    conda activate torch_molecule
    
  2. Install using pip:

    pip install torch-molecule
    
  3. 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

ModelRequired Packages
HFPretrainedMolecularEncodertransformers
HFPretrainedMolecularGeneratortransformers
HFPretrainedMolecularGenerator (MolGen)transformers, selfies
HFPretrainedMolecularGenerator (Molexar)transformers, fragment-selfies, molexar
HFPretrainedMolecularGenerator (SAFE-GPT)transformers, safe-mol
BFGNNMolecularPredictortorch-scatter
GRINMolecularPredictortorch-scatter
GRINMolecularPredictor (if enable repetition_augmentation=True)CombineMols

For models that require torch-scatter: Install using the following command: pip install torch-scatter -f https://data.pyg.org/whl/torch-${TORCH}+${CUDA}.html, e.g.,

pip install torch-scatter -f https://data.pyg.org/whl/torch-2.7.1+cu128.html

For models that require transformers: pip install transformers

For MolGen (selfies): pip install "selfies>=2.1". Source: aspuru-guzik-group/selfies.

For Molexar: pip install fragment-selfies loguru (Fragment-SELFIES) and pip install git+https://github.com/fairydance/Molexar.git (Molexar). Molexar itself requires transformers>=5.8.

For SAFE-GPT: pip install safe-mol (SAFE).

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 examples and tests folders.

torch-molecule supports applications in broad domains from chemistry, biology, to materials science. To get started, you can load prepared datasets from torch_molecule.datasets (updated after v0.1.3):

DatasetDescriptionFunction
qm9Quantum chemical properties (DFT level)load_qm9
chembl2kBioactive molecules with drug-like propertiesload_chembl2k
broad6kBioactive molecules with drug-like propertiesload_broad6k
toxcastToxicity of chemical compoundsload_toxcast
admetChemical absorption, distribution, metabolism, excretion, and toxicityload_admet
gaspermSix gas permeability properties for polymeric materialsload_gasperm
zinc250kA common subset of ZINC dataset, which does not have labels and could be used for unconditional generation or virtual screeningload_zinc250k
from torch_molecule.datasets import load_qm9

# local_dir is the local path where the dataset will be saved
molecular_data = load_qm9(local_dir='torchmol_data')
smiles_list, property_np_array = molecular_data.data, molecular_data.target

# len(smiles_list): 133885
# Property array shape: (133885, 1)

# load_qm9 returns the target "gap" by default, but you can adjust it by passing new target_cols
target_cols = ['homo', 'lumo', 'gap']
molecular_data = load_qm9(local_dir='torchmol_data', target_cols=target_cols)
smiles_list, property_np_array = molecular_data.data, molecular_data.target

# the target could be None if loading an unlabeled dataset
from torch_molecule.datasets import load_zinc250k
molecular_data = load_zinc250k(local_dir='torchmol_data')
smiles_list = molecular_data.data
assert molecular_data.target is None

(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_qm9
from torch_molecule import GREAMolecularPredictor

data = load_qm9(local_dir='torchmol_data')
# "random" | "scaffold" | "butina" | "size"
# scaffold: unseen Bemis-Murcko scaffolds; butina: Tanimoto clusters; size: heavy-atom count
# Split the full dataset. subsample() is only for local debugging / CI — do not
# shrink QM9 (or any benchmark) just to make Butina cheaper.
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,
)

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=f"{model_dir}/GREA_{task_name}.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

ModelReference
MoAMaMotif-aware Attribute Masking for Molecular Graph Pre-training. LoG 2024
GraphMAEGraphMAE: Self-Supervised Masked Graph Autoencoders. KDD 2022
AttrMaskingStrategies for Pre-training Graph Neural Networks. ICLR 2020
ContextPredStrategies for Pre-training Graph Neural Networks. ICLR 2020
EdgePredStrategies for Pre-training Graph Neural Networks. ICLR 2020
InfoGraphInfoGraph: Unsupervised and Semi-supervised Graph-Level Representation Learning via Mutual Information Maximization. ICLR 2020
SupervisedSupervised pretraining
PretrainedGPT2-ZINC-87M: GPT-2 based model (87M parameters) pretrained on ZINC dataset with ~480M SMILES strings.
RoBERTa-ZINC-480M: RoBERTa based model (102M parameters) pretrained on ZINC dataset with ~480M SMILES strings.
UniKi/bert-base-smiles: BERT model pretrained on SMILES strings.
ChemBERTa-zinc-base-v1: RoBERTa model pretrained on ZINC dataset with ~100k SMILES strings.
ChemBERTa series: Available in multiple sizes and training objectives (MLM/MTR). ChemBERTa-5M-MLM, ChemBERTa-5M-MTR, ChemBERTa-10M-MLM, ChemBERTa-10M-MTR, ChemBERTa-77M-MLM, ChemBERTa-77M-MTR.
ChemGPT series: GPT-Neo based models pretrained on PubChem10M dataset with SELFIES strings. ChemGPT-1.2B, ChemGPT-4.7B, ChemGPT-19B.

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
Man Hei Matthew Thom

Integrating Pretrained Generator (NovoMolGen, MolGen, Molexar, SAFE-GPT), dataset splitting modules
Eric Inae
Eric Inae

Integrating MoAMa, GraphMAE, AttrMasking, ContextPred, EdgePred
Yihan Zhu
Yihan Zhu

Integrating DeFoG, GRIN, BFGNN, RPGNN, Transformer (SMILES)
Monica C. S
Monica C. S

Debugging and integrating training logs (#19)
deep-learning
graph-neural-networks
inverse-design
molecule
property-prediction
representation-learning

Project health

Latest release

0.1.7 · Oct 8, 2025

Releases this year

0

Significant stargazers

Daniel van Strien

391 followers · starred Jun 2025

Bo li (李波)

68 followers · starred Apr 2025

Will Rice

85 followers · starred Jun 2025