Skip to main content

Quick start

This guide walks you through creating a complete molecular optimization pipeline using ChemLactica. You’ll learn how to define an oracle function, configure the optimization algorithm, and generate molecules optimized for specific properties.

Overview

Molecular optimization with ChemLactica requires two main components:
  1. Oracle: Evaluates the quality of generated molecules based on your target properties
  2. Configuration: Defines hyperparameters for the optimization algorithm
The optimization process uses a genetic-like algorithm that iteratively generates and refines molecules to maximize the oracle score.

Step 1: Define your oracle

The oracle is responsible for evaluating molecules and assigning scores. Here’s a complete example that optimizes for TPSA (Topological Polar Surface Area) and molecular weight:
The oracle’s __call__ method receives a list of MoleculeEntry objects and must return a list of scores. Set takes_entry = True to receive MoleculeEntry objects instead of SMILES strings.

Step 2: Create a configuration file

Create a YAML file to define the optimization hyperparameters. Here’s an example configuration for Chemlactica-125M:
model_hparams.yaml

Key configuration parameters

  • pool_size: Number of top molecules to maintain in the pool
  • num_similars: Number of similar molecules to include in prompts
  • num_gens_per_iter: Number of molecules to generate per iteration
  • generation_temperature: Sampling temperature for diversity
  • strategy: Use [rej-sample-v2] for fine-tuning during optimization, or [default] for generation-only
Using strategy: [rej-sample-v2] enables rejection sampling with fine-tuning during optimization, which improves results but requires more computational resources.

Step 3: Run the optimization

Now put it all together in a complete script:
example_run.py

Step 4: Execute the optimization

Run your optimization script:
This will run the optimization 3 times with different random seeds and save the results to the ./results directory.

Understanding the output

During optimization, you’ll see periodic logs showing:
  • avg_top1: Best molecule score found so far
  • avg_top10: Average score of top 10 molecules
  • avg_top100: Average score of top 100 molecules

Advanced usage

Using different models

To use Chemlactica-1.3B or Chemma-2B, simply change the checkpoint paths in your config:

Custom property constraints

Modify the oracle to optimize for different properties:

Generation-only mode

For faster execution without fine-tuning, use the default strategy:
This disables the rejection sampling and fine-tuning steps.

Next steps

Advanced optimization

Explore complex oracles for docking, multi-objective optimization, and more

Fine-tuning

Learn how to fine-tune ChemLactica on your own molecular datasets

Test suite

Check out comprehensive examples in the ChemLacticaTestSuite repository

Paper

Read the full research paper for technical details