E(3) Equivariant Diffusion Model (EDM)

repository·main·Indexed 20 days ago

https://github.com/ehoogeboom/e3_diffusion_for_molecules

An E(3) Equivariant Diffusion Model for generating 3D molecules, supporting training on QM9 and GEOM-Drugs datasets. The library includes implementations for conditional generation based on molecular properties, E(n) Equivariant Graph Neural Networks (EGNN), Graph Autoencoders, and N-body system experiments. It provides tools for property prediction, sample analysis, and visualization of generated molecules.

Tokens
3.1K
Snippets
13
Records
13
Agent score
69%

What's inside e3_diffusion_for_molecules

  1. Analyze and visualize QM9 samples after training

    main

    After training a model on QM9, use the following scripts to evaluate quality and visualize results:

    Analyze sample quality:

    python eval_analyze.py --model_path outputs/edm_qm9 --n_samples 10_000

    Visualize molecules:

    python eval_sample.py --model_path outputs/edm_qm9 --n_samples 10_000
    python eval_analyze.py --model_path outputs/edm_qm9 --n_samples 10_000
    python eval_sample.py --model_path outputs/edm_qm9 --n_samples 10_000
  2. Train and evaluate EDM for GEOM-Drugs

    main

    To work with GEOM-Drugs, first follow the data setup instructions in data/geom/README.md.

    Training: Use main_geom_drugs.py. Note that the EGNN model uses fully connected message passing, which is memory intensive; if you encounter OOM errors, try reducing the batch size.

    python main_geom_drugs.py --n_epochs 3000 --exp_name edm_geom_drugs --n_stability_samples 500 --diffusion_noise_schedule polynomial_2 --diffusion_steps 1000 --diffusion_noise_precision 1e-5 --diffusion_loss_type l2 --batch_size 64 --nf 256 --n_layers 4 --lr 1e-4 --normalize_factors [1,4,10] --test_epochs 1 --ema_decay 0.9999 --normalization_factor 1 --model egnn_dynamics --visualize_every_batch 10000

    Analyze and Sample:

    python eval_analyze.py --model_path outputs/edm_geom_drugs --n_samples 10_000
    python eval_sample.py --model_path outputs/edm_geom_drugs
  3. Train a Conditional EDM

    main

    To train a model that generates molecules conditioned on specific properties, use main_qm9.py with the --conditioning flag.

    Supported properties for --conditioning include:

    • alpha
    • gap
    • homo
    • lumo
    • mu
    • Cv

    Example training command:

    python main_qm9.py --exp_name exp_cond_alpha  --model egnn_dynamics --lr 1e-4  --nf 192 --n_layers 9 --save_model True --diffusion_steps 1000 --sin_embedding False --n_epochs 3000 --n_stability_samples 500 --diffusion_noise_schedule polynomial_2 --diffusion_noise_precision 1e-5 --dequantization deterministic --include_charges False --diffusion_loss_type l2 --batch_size 64 --normalize_factors [1,8,1] --conditioning alpha --dataset qm9_second_half
  4. Train a property classifier network

    main

    To train a classifier to predict properties (e.g., for evaluating the EDM), navigate to the property prediction directory and use main_qm9_prop.py.

    Training with EGNN:

    cd qm9/property_prediction
    python main_qm9_prop.py --num_workers 2 --lr 5e-4 --property alpha --exp_name exp_class_alpha --model_name egnn

    Training with Node Count baseline: To train a baseline that only classifies based on the number of nodes, change --model_name to numnodes:

    python main_qm9_prop.py --num_workers 2 --lr 5e-4 --property alpha --exp_name exp_class_alpha --model_name numnodes
  5. Train the EDM on QM9

    main

    To train the E(3) Equivariant Diffusion Model on the QM9 dataset, use the main_qm9.py script. You can configure hyperparameters such as epochs, noise schedules, and diffusion steps via CLI flags.

    python main_qm9.py --n_epochs 3000 --exp_name edm_qm9 --n_stability_samples 1000 --diffusion_noise_schedule polynomial_2 --diffusion_noise_precision 1e-5 --diffusion_steps 1000 --diffusion_loss_type l2 --batch_size 64 --nf 256 --n_layers 9 --lr 1e-4 --normalize_factors [1,4,10] --test_epochs 20 --ema_decay 0.9999
  6. Run N-body system experiments

    main

    Experiments for N-body systems involve two steps: generating a dataset and running the model training.

    1. Create N-body dataset

    Use generate_dataset.py to create the data. Use --num-train to specify the number of training samples and --sufix for naming.

    2. Run training

    You can run several models using main_nbody.py:

    • EGNN: --model egnn_vel
    • GNN: --model gnn
    • Radial Field: --model rf_vel
    • Tensor Field Networks: --model tfn
    • SE3 Transformer: --model se3_transformer

    3. Sweep over training samples

    To perform a sweep, generate a larger dataset with --sample-freq and then run training commands adding the --dataset nbody flag. Control the sample size with --max_training_samples.

    # Create dataset
    cd n_body_system/dataset
    python -u generate_dataset.py --num-train 10000 --seed 43 --sufix small
    
    # Run EGNN experiment
    python -u main_nbody.py --exp_name exp_1_egnn_vel --model egnn_vel --max_training_samples 3000 --lr 5e-4
    
    # Run sweep experiment
    python -u main_nbody.py --exp_name exp_debug --model egnn_vel --max_training_samples 10000 --lr 5e-4 --dataset nbody
  7. Evaluate property classifier on EDM samples

    main

    To evaluate how well a trained property classifier performs on samples generated by a trained EDM model, use eval_conditional_qm9.py with both --generators_path and --classifiers_path.

    Example command:

    python eval_conditional_qm9.py --generators_path outputs/exp_cond_alpha --classifiers_path qm9/property_prediction/outputs/exp_class_alpha --property alpha  --iterations 100  --batch_size 100 --task edm
  8. Run QM9 property prediction experiments

    main

    Predict molecular properties on the QM9 dataset using main_qm9.py.

    Available Properties (--property): alpha, gap, homo, lumo, mu, Cv, G, H, r2, U, U0, zpve.

    Recommended Learning Rates (--lr):

    • Use 1e-3 for: gap, homo, lumo.
    • Use 5e-4 for all other properties (e.g., alpha, mu, Cv, G, H, r2, U, U0, zpve).

    Common Arguments:

    • --num_workers: Number of worker threads.
    • --exp_name: Unique name for the experiment.
    # Predict gap (requires 1e-3 lr)
    python -u main_qm9.py --num_workers 2 --lr 1e-3 --property gap --exp_name exp_1_gap
    
    # Predict alpha (requires 5e-4 lr)
    python -u main_qm9.py --num_workers 2 --lr 5e-4 --property alpha --exp_name exp_1_alpha
  9. Generate samples for conditional properties

    main

    Use eval_conditional_qm9.py to generate samples based on a trained conditional generator. You must specify the --property and the path to your generators.

    Example command:

    python eval_conditional_qm9.py --generators_path outputs/exp_cond_alpha --property alpha --n_sweeps 10 --task qualitative
  10. Run Graph Autoencoder (AE) experiments

    main

    Graph Autoencoder experiments are executed via main_ae.py. You can test different models on Erdos & Renyi or Community datasets.

    Supported Models (--model):

    • ae: Standard GNN Autoencoder
    • ae_rf: Radial Field Autoencoder
    • ae_egnn: EGNN Autoencoder

    Key Arguments:

    • --dataset: The dataset name (e.g., erdosrenyinodes_0.25_none or community_ours).
    • --K: Latent dimension/size.
    • --emb_nf: Embedding feature dimension.
    • --noise_dim: Dimension of noise (use 1 for Noise-GNN variants, 0 for standard).
    • --epochs: Number of training epochs.
    • --test_interval: Interval for testing.

    Overfitting Experiments: To run an overfitting experiment, use a dataset name containing _overfit (e.g., erdosrenyinodes_0.2_overfit).

    # EGNN Community experiment
    python -u main_ae.py --exp_name exp1_egnn_community --model ae_egnn --dataset community_ours --K 8 --emb_nf 8
    
    # Overfit EGNN Erdos&Renyi
    python -u main_ae.py --model ae_egnn --dataset erdosrenyinodes_0.2_overfit --epochs 10001 --test_interval 200 --K 16 --emb_nf 16 2>&1 | tee outputs_ae/$EXP.log &
  11. Build the GEOM-DRUGS dataset

    main

    To prepare the GEOM-DRUGS dataset for use with EDM, follow these steps to download, extract, and process the raw data:

    1. Download the dataset: Use wget to download the file from Dataverse. Note that the file is approximately 50GB.
    2. Extract and Organize: Untar the downloaded file and move the contents into the data/geom/ directory of your project.
    3. Install Dependencies: Ensure msgpack is installed in your Python environment.
    4. Run Build Script: Execute the build_geom_dataset.py script to process the raw files into the required format.
    # 1. Download
    wget https://dataverse.harvard.edu/api/access/datafile/4360331
    
    # 2. Untar and move to data/geom/
    tar -xzvf 4360331
    
    # 3. Install dependency
    pip install msgpack
    
    # 4. Build dataset
    python3 build_geom_dataset.py