Using Pre-trained Models
Relevant source files
The following files were used as context for generating this wiki page:
DGL-LifeSci provides an extensive suite of pre-trained models designed to accelerate research in life sciences by eliminating the need for training from scratch. These models cover a wide range of applications, including molecular property prediction, molecule embedding generation, generative modeling, and chemical reaction prediction.
The framework utilizes a centralized load_pretrained API to fetch and initialize weights for various architectures. This allows users to perform out-of-the-box inference or fine-tune models on custom datasets with minimal configuration.
Sources: README.md:144-147, examples/README.md:1-10
Core Loading Mechanism
The primary interface for accessing pre-trained weights is the load_pretrained function. This function handles the instantiation of the specific model architecture and the loading of associated weights based on a provided model name string.
The diagram shows the standard flow for initializing a model with pre-trained weights.
Sources: README.md:149-150, examples/property_prediction/moleculenet/classification.py:59-61
Pre-trained Model Categories
The library categorizes pre-trained models based on their architectural intent and the tasks they were trained on.
1. Molecular Property Prediction
These models are typically Graph Convolutional Networks (GCN), Graph Attention Networks (GAT), or Weave models trained on benchmark datasets like Tox21.
| Model Name | Architecture | Training Dataset | Primary Task |
|---|---|---|---|
GCN_Tox21 | GCNPredictor | Tox21 | Binary Classification |
GAT_Tox21 | GATPredictor | Tox21 | Binary Classification |
Weave_Tox21 | WeavePredictor | Tox21 | Binary Classification |
Sources: README.md:150-151, examples/property_prediction/moleculenet/README.md:21-25
2. Molecular Embeddings (GIN)
DGL-LifeSci includes Graph Isomorphism Networks (GIN) pre-trained using various self-supervised and semi-supervised strategies on massive molecular datasets (e.g., two million molecules).
| Pre-training Strategy | Model Identifier |
|---|---|
| Context Prediction | gin_supervised_contextpred |
| Information Maximization | gin_supervised_infomax |
| Edge Prediction | gin_supervised_edgepred |
| Attribute Masking | gin_supervised_masking |
Sources: examples/molecule_embeddings/README.md:1-15, examples/molecule_embeddings/main.py:100-103
3. Generative Models
Pre-trained models are available for Deep Generative Models of Graphs (DGMG) and Junction Tree Variational Autoencoders (JTVAE). These are used to generate new molecules with drug-like properties.
| Model Name | Dataset | Order |
|---|---|---|
DGMG_ZINC_canonical | ZINC | Canonical |
DGMG_ChEMBL_random | ChEMBL | Random |
JTNN_ZINC | ZINC | N/A |
Sources: README.md:167-169, examples/generative_models/dgmg/README.md:148-155, examples/generative_models/jtvae/README.md:65-70
4. Reaction Prediction
Models for reaction center prediction identify pairs of atoms likely to form or lose bonds. The Weisfeiler-Lehman Network (WLN) is the core architecture used for this task.
Sources: examples/reaction_prediction/rexgen_direct/README.md:10-25
Usage Workflows
Property Prediction Inference
When using a pre-trained property predictor, the user must ensure the input molecules are featurized consistently with the model's training requirements.
Sequence for performing inference on molecular properties.
Sources: README.md:149-160
Computing Molecule Embeddings
Users can compute embeddings for large lists of SMILES using pre-trained GIN models. The process involves parsing SMILES with RDKit, featurizing atoms/bonds with PretrainAtomFeaturizer and PretrainBondFeaturizer, and applying a readout function.
from dgllife.model import load_pretrained
from dgl.nn.pytorch.glob import AvgPooling
model = load_pretrained('gin_supervised_contextpred')
model.eval()
readout = AvgPooling()
# Inside inference loop
with torch.no_grad():
node_repr = model(bg, nfeats, efeats)
mol_emb = readout(bg, node_repr)
Sources: examples/molecule_embeddings/main.py:80-95, examples/molecule_embeddings/README.md:20-35
Molecule Generation
Generative models like DGMG can be used to sample new SMILES strings directly through the model's forward call after setting rdkit_mol=True.
from dgllife.model import load_pretrained
model = load_pretrained('DGMG_ZINC_canonical')
model.eval()
# Generate 4 molecules
smiles = [model(rdkit_mol=True) for _ in range(4)]
Sources: README.md:167-175, examples/generative_models/dgmg/eval.py:25-30
Implementation Details
Pre-trained models are integrated into the example scripts via the --pre-trained (or -p/-pr) flags. If these flags are set, the scripts bypass the training loop and load the specified weights.
- Weight Management: Weights are typically saved as state dictionaries in
.pthor.pklfiles. - Device Mapping: Models can be loaded onto CPU or GPU, though training scripts for models like DGMG emphasize CPU multiprocess training for speed.
- Featurization Alignment: Models like GIN require specific atom and bond featurizers (
PretrainAtomFeaturizer,PretrainBondFeaturizer) to ensure feature dimensions match the pre-trained weights.
Sources: examples/property_prediction/moleculenet/classification.py:73-75, examples/generative_models/dgmg/eval.py:38-46, examples/molecule_embeddings/main.py:82-88
Using pre-trained models in DGL-LifeSci provides a high-level abstraction for complex GNN architectures. By leveraging the load_pretrained utility, developers can integrate state-of-the-art molecular representations and predictors into their pipelines without the computational overhead of large-scale training.