Skill Tracer
Skills › Scientific Agent Skills › deepchem › workflows.md

workflows.md

137 lines · skills/deepchem/references/workflows.md at 5f2bc03

Open on GitHub
1# Additional DeepChem workflows
2
3All templates here target stable 2.8.0. External-data/backend-dependent examples are
4illustrative unless [review.md](review.md) lists an executed check. Use the executable
5scripts for molecular property training; the reference contracts in
6[api_reference.md](api_reference.md) apply to every route.
7

Additional DeepChem workflows

All templates here target stable 2.8.0. External-data/backend-dependent examples are illustrative unless review.md lists an executed check. Use the executable scripts for molecular property training; the reference contracts in api_reference.md apply to every route.

Raw lines 1 to 7
8## 1. Property prediction and 2. MoleculeNet benchmarks
9
10Follow [typical_workflows.md](typical_workflows.md). Define one consistent target
11family per model, preserve missing-label masks, and use one feature representation
12throughout training and prediction. Never combine a toxicity label and a solubility
13value in a single regression example without a justified multi-head loss.
15For a binary graph benchmark, the input must match the selected Torch model:
17```python
18# Illustrative: downloads BBBP and requires compatible Torch/DGL/DGL-LifeSci.
19import deepchem as dc
20featurizer = dc.feat.MolGraphConvFeaturizer()
21tasks, datasets, transformers = dc.molnet.load_bbbp(
22 featurizer=featurizer, splitter='scaffold', reload=True)
23train, valid, test = datasets
24model = dc.models.GCNModel(n_tasks=len(tasks), mode='classification')
25model.fit(train, nb_epoch=20)
26metric = dc.metrics.Metric(dc.metrics.roc_auc_score)
27# Inspect class support in each task before interpreting this aggregate.
28score = model.evaluate(test, [metric], transformers=transformers)
29```

1. Property prediction and 2. MoleculeNet benchmarks

Follow typical_workflows.md. Define one consistent target family per model, preserve missing-label masks, and use one feature representation throughout training and prediction. Never combine a toxicity label and a solubility value in a single regression example without a justified multi-head loss.

For a binary graph benchmark, the input must match the selected Torch model:

# Illustrative: downloads BBBP and requires compatible Torch/DGL/DGL-LifeSci.
import deepchem as dc
featurizer = dc.feat.MolGraphConvFeaturizer()
tasks, datasets, transformers = dc.molnet.load_bbbp(
    featurizer=featurizer, splitter='scaffold', reload=True)
train, valid, test = datasets
model = dc.models.GCNModel(n_tasks=len(tasks), mode='classification')
model.fit(train, nb_epoch=20)
metric = dc.metrics.Metric(dc.metrics.roc_auc_score)
# Inspect class support in each task before interpreting this aggregate.
score = model.evaluate(test, [metric], transformers=transformers)
Raw lines 8 to 30
31## 3. Hyperparameter search
33The following uses numeric data and the real callable/return contract. `train` and
34`valid` must already be independently defined, fully labeled single-task datasets;
35this template minimizes original-unit MAE. The test split is untouched.
37```python
38import deepchem as dc
39from sklearn.ensemble import RandomForestRegressor
41def builder(n_estimators, max_depth, model_dir=None):
42 estimator = RandomForestRegressor(n_estimators=n_estimators,
43 max_depth=max_depth, random_state=7)
44 return dc.models.SklearnModel(estimator, model_dir=model_dir)
46search = dc.hyper.GridHyperparamOpt(builder)
47metric = dc.metrics.Metric(dc.metrics.mean_absolute_error)
48best_model, best_params, all_scores = search.hyperparam_search(
49 {'n_estimators': [8, 16], 'max_depth': [2, None]},
50 train, valid, metric, output_transformers=[], use_max=False)
51print(best_params, all_scores)
52```

3. Hyperparameter search

The following uses numeric data and the real callable/return contract. train and valid must already be independently defined, fully labeled single-task datasets; this template minimizes original-unit MAE. The test split is untouched.

import deepchem as dc
from sklearn.ensemble import RandomForestRegressor

def builder(n_estimators, max_depth, model_dir=None):
    estimator = RandomForestRegressor(n_estimators=n_estimators,
                                      max_depth=max_depth, random_state=7)
    return dc.models.SklearnModel(estimator, model_dir=model_dir)

search = dc.hyper.GridHyperparamOpt(builder)
metric = dc.metrics.Metric(dc.metrics.mean_absolute_error)
best_model, best_params, all_scores = search.hyperparam_search(
    {'n_estimators': [8, 16], 'max_depth': [2, None]},
    train, valid, metric, output_transformers=[], use_max=False)
print(best_params, all_scores)
Raw lines 31 to 53
54## 4. Transfer learning
56Use the shipped transfer script to construct HF network/tokenizer objects or load
57a compatible GROVER encoder component before training. `model_dir` alone loads
58nothing. The model card for `seyonec/ChemBERTa-zinc-base-v1` describes a RoBERTa
59encoder trained on 100k ZINC strings, not a 77M-molecule ChemBERTa model.
60[ChemBERTa card](https://huggingface.co/seyonec/ChemBERTa-zinc-base-v1)
62The current [MoLFormer card](https://huggingface.co/ibm-research/MoLFormer-XL-both-10pct)
63identifies the 10% ZINC/10% PubChem variant and its Transformers 5 implementation.
64For Transformers 4 it specifies `revision='compat-v4'`. Resolve that branch and pass
65its immutable commit SHA to the bundled helper. Record the resolved commit,
66review its repository code before `trust_remote_code=True`, and match chemical
67canonicalization to the encoder's training inputs. A newly initialized property
68head is expected; it has not inherited measured property calibration.
70The helper requires a full commit SHA whenever repository code is enabled, following
71the [Hugging Face custom-model revision guidance](https://huggingface.co/docs/transformers/models).
73GROVER checkpoint architecture, featurization and tensor dimensions must match.
74Only the encoder is restored by the shipped helper; this is deliberate transfer
75into a new task head. A synthetic checkpoint round-trip verifies loading mechanics,
76not any public pretrained model's performance.
77[Released GROVER implementation](https://github.com/deepchem/deepchem/blob/2.8.0/deepchem/models/torch_models/grover.py)

4. Transfer learning

Use the shipped transfer script to construct HF network/tokenizer objects or load a compatible GROVER encoder component before training. model_dir alone loads nothing. The model card for seyonec/ChemBERTa-zinc-base-v1 describes a RoBERTa encoder trained on 100k ZINC strings, not a 77M-molecule ChemBERTa model. ChemBERTa card

The current MoLFormer card identifies the 10% ZINC/10% PubChem variant and its Transformers 5 implementation. For Transformers 4 it specifies revision='compat-v4'. Resolve that branch and pass its immutable commit SHA to the bundled helper. Record the resolved commit, review its repository code before trust_remote_code=True, and match chemical canonicalization to the encoder's training inputs. A newly initialized property head is expected; it has not inherited measured property calibration.

The helper requires a full commit SHA whenever repository code is enabled, following the Hugging Face custom-model revision guidance.

GROVER checkpoint architecture, featurization and tensor dimensions must match. Only the encoder is restored by the shipped helper; this is deliberate transfer into a new task head. A synthetic checkpoint round-trip verifies loading mechanics, not any public pretrained model's performance. Released GROVER implementation

Raw lines 54 to 78
79## 5. Molecular generation
81`BasicMolGANModel` consumes graph tensors, not a normal GraphConv dataset. Use
82`MolGanFeaturizer`, check atom vocabulary and maximum atom count, then one-hot encode
83adjacency bond categories and node categories. The Torch `fit_gan` consumes a
84batch iterator of `{gan.data_inputs[0]: adjacency, gan.data_inputs[1]: nodes}`;
85epochs belong in that iterator, not an unsupported `nb_epoch` argument.
86`nodes` is the number of atom categories (an integer), not hidden layer sizes.
87`predict_gan_generator` returns `GraphMatrix` objects; convert using
88`featurizer.defeaturize`, retain failures, and separately calculate validity,
89uniqueness, novelty and applicability. It does not promise valid or synthesizable
90molecules. The stable implementation's `conditional_inputs` is documented unused;
91`conditional=True` does not implement property-conditioned generation.
93Use the released [Torch MolGAN worked example](https://github.com/deepchem/deepchem/blob/2.8.0/deepchem/models/torch_models/molgan.py)
94as the backend-specific starting point. Full GAN optimization was not run here.

5. Molecular generation

BasicMolGANModel consumes graph tensors, not a normal GraphConv dataset. Use MolGanFeaturizer, check atom vocabulary and maximum atom count, then one-hot encode adjacency bond categories and node categories. The Torch fit_gan consumes a batch iterator of {gan.data_inputs[0]: adjacency, gan.data_inputs[1]: nodes}; epochs belong in that iterator, not an unsupported nb_epoch argument. nodes is the number of atom categories (an integer), not hidden layer sizes. predict_gan_generator returns GraphMatrix objects; convert using featurizer.defeaturize, retain failures, and separately calculate validity, uniqueness, novelty and applicability. It does not promise valid or synthesizable molecules. The stable implementation's conditional_inputs is documented unused; conditional=True does not implement property-conditioned generation.

Use the released Torch MolGAN worked example as the backend-specific starting point. Full GAN optimization was not run here.

Raw lines 79 to 95
96## 6. Materials prediction
98Use `pymatgen.core.Structure.from_file` for CIF input, then
99`dc.feat.CGCNNFeaturizer().featurize(structures)` and an explicitly labeled
100`NumpyDataset`. `CGCNNModel(n_tasks=1, mode='regression')` requires DGL; verify its
101model/feature dimensions and convergence cutoff. Validate composition, periodic
102cell, occupancy, structure identity, target units and calculation provenance.
103Split related compositions/structures together when measuring transfer. There is
104no stable `CIFLoader` or universal `load_bandgap` shortcut in this workflow.
105[CGCNN featurizer](https://deepchem.readthedocs.io/en/2.8.0/api_reference/featurizers.html#cgcnnfeaturizer)

6. Materials prediction

Use pymatgen.core.Structure.from_file for CIF input, then dc.feat.CGCNNFeaturizer().featurize(structures) and an explicitly labeled NumpyDataset. CGCNNModel(n_tasks=1, mode='regression') requires DGL; verify its model/feature dimensions and convergence cutoff. Validate composition, periodic cell, occupancy, structure identity, target units and calculation provenance. Split related compositions/structures together when measuring transfer. There is no stable CIFLoader or universal load_bandgap shortcut in this workflow. CGCNN featurizer

Raw lines 96 to 106
107## 7. Protein sequence analysis
109For supervised protein prediction, construct raw sequence strings **with labels**;
110FASTA alone does not provide those labels. For ProtBERT the official tokenizer input
111uses space-separated uppercase residues, with uncommon residues handled according
112to its model card. Load a tokenizer and a task-specific model object via Transformers,
113then wrap in `HuggingFaceModel`. The same single-task, complete-label and logits
114limitations apply. Cluster/split homologs before model selection, record sequence
115length handling, and do not silently truncate biologically decisive regions.
116[ProtBERT model card](https://huggingface.co/Rostlab/prot_bert)

7. Protein sequence analysis

For supervised protein prediction, construct raw sequence strings with labels; FASTA alone does not provide those labels. For ProtBERT the official tokenizer input uses space-separated uppercase residues, with uncommon residues handled according to its model card. Load a tokenizer and a task-specific model object via Transformers, then wrap in HuggingFaceModel. The same single-task, complete-label and logits limitations apply. Cluster/split homologs before model selection, record sequence length handling, and do not silently truncate biologically decisive regions. ProtBERT model card

Raw lines 107 to 117
118## 8. Custom model integration
120Numeric scikit-learn models can be wrapped in `SklearnModel`. For a custom Torch
121network, this correct DeepChem loss contract retains sample weights:
123```python
124import torch
125import deepchem as dc
126model = dc.models.TorchModel(
127 model=torch.nn.Linear(2048, 1),
128 loss=dc.models.losses.L2Loss(),
129 output_types=['prediction'], device='cpu')
130# train must contain 2048 numeric features and one continuous target.
131model.fit(train, nb_epoch=1, checkpoint_interval=0)
132```
134A reduced scalar `torch.nn.MSELoss` passed as a custom DeepChem callable has the
135wrong `(outputs, labels, weights)` contract. For complex custom losses, explicitly
136verify masking and normalization with a zero-weight synthetic example.
137[Released TorchModel](https://github.com/deepchem/deepchem/blob/2.8.0/deepchem/models/torch_models/torch_model.py)

8. Custom model integration

Numeric scikit-learn models can be wrapped in SklearnModel. For a custom Torch network, this correct DeepChem loss contract retains sample weights:

import torch
import deepchem as dc
model = dc.models.TorchModel(
    model=torch.nn.Linear(2048, 1),
    loss=dc.models.losses.L2Loss(),
    output_types=['prediction'], device='cpu')
# train must contain 2048 numeric features and one continuous target.
model.fit(train, nb_epoch=1, checkpoint_interval=0)

A reduced scalar torch.nn.MSELoss passed as a custom DeepChem callable has the wrong (outputs, labels, weights) contract. For complex custom losses, explicitly verify masking and normalization with a zero-weight synthetic example. Released TorchModel

Raw lines 118 to 137