Crystal Graph Convolutional Neural Networks (CGCNN)

repository·master·Indexed 21 days ago

https://github.com/txie-93/cgcnn

A framework for predicting material properties from arbitrary crystal structures using graph convolutional neural networks. It supports both regression and classification tasks, providing pre-trained models for properties such as formation energy, band gap, and bulk moduli. The library integrates with PyTorch, scikit-learn, and pymatgen, utilizing CIF files and custom dataset configurations for training and prediction.

Tokens
3.1K
Snippets
8
Records
14
Agent score
75%

What's inside cgcnn

  1. Define a customized dataset for CGCNN

    master

    To use CGCNN for training or prediction, you must organize your crystal data into a root_dir containing the following files:

    1. id_prop.csv: A CSV file with two columns. The first column is a unique ID for each crystal, and the second column is the target property value. (For prediction tasks, the second column can contain placeholder/random numbers).
    2. atom_init.json: A JSON file storing the initialization vector for each element. You can use data/sample-regression/atom_init.json as a template.
    3. [ID].cif: One CIF file per crystal, where the filename matches the ID provided in id_prop.csv.

    Directory Structure:

    root_dir
    ├── id_prop.csv
    ├── atom_init.json
    ├── id0.cif
    ├── id1.cif
    └── ...
  2. Best practices before using pre-trained models

    master

    When using pre-trained CGCNN models, keep the following limitations in mind to ensure accurate predictions:

    1. Data Distribution Alignment: Models only generalize to crystals from the same distribution as their training data. Always check the Data Ref. (e.g., Materials Project/ICSD) associated with the model. Using a model trained on experimentally synthesized structures (like ICSD) to predict properties of imaginary or thermodynamically unstable crystals may result in significant errors.
    2. Accuracy Awareness: All CGCNN models have inherent prediction errors. Consult the Model Ref. provided in the model documentation to understand the expected accuracy and error margins before relying on results.
  3. Obtain materials data for CGCNN

    master

    The CGCNN model relies on material structures and properties sourced from external open datasets. Because these datasets cannot be redistributed directly, you must download them manually and convert them into the formats required by CGCNN.

    The primary sources are:

    To reproduce the exact results from the original paper, use the three CSV files provided in the repository which contain the specific material IDs used in the study. Note that since the Materials Project database is updated frequently, properties and structures for the same IDs may differ from those used in the original publication.

  4. How to share your pre-trained models

    master

    To contribute a pre-trained model to the repository, email txie@mit.edu. Note that only peer reviewed works are accepted.

    Your submission must include:

    1. A .pth.tar file containing the CGCNN model.
    2. The model type and the target property.
    3. Links to the data reference and the model reference.
  5. Install CGCNN via Conda

    master

    To set up the CGCNN environment, use conda to create a new environment named cgcnn with the required dependencies: pytorch, scikit-learn, torchvision, and pymatgen.

    Note: This package requires PyTorch v1.0.0+ and is incompatible with versions below v0.4.0.

    conda upgrade conda
    conda create -n cgcnn python=3 scikit-learn pytorch torchvision pymatgen -c pytorch -c conda-forge
    
    # Activate the environment
    source activate cgcnn
  6. How to cite pre-trained CGCNN models

    master
    If you use any pre-trained models in your work, you must cite both the Data Ref. (the dataset used for training) and the Model Ref. (the specific model architecture/paper). Both the data and the model are considered equally important components of the machine learning result.
  7. Understand the training and validation loop logic

    master

    The main.py script implements a standard PyTorch training loop with the following characteristics:

    1. Data Loading: Uses CIFData and get_train_val_test_loader to create data iterators.
    2. Task-Specific Loss:
      • regression: Uses nn.MSELoss().
      • classification: Uses nn.NLLLoss().
    3. Optimization: Supports SGD and Adam. A MultiStepLR scheduler is used to decay the learning rate at specified lr-milestones.
    4. Checkpointing: The script saves the best model based on the task:
      • For regression: The model with the lowest Mean Absolute Error (MAE).
      • For classification: The model with the highest AUC score.
      • Best models are saved to model_best.pth.tar.
    5. Evaluation: After training, the best model is reloaded and evaluated on the test set. If testing is enabled, results are written to test_results.csv containing cif_id, target, and pred.
  8. Available pre-trained CGCNN classification models

    master

    The following pre-trained model is available for categorical material property prediction.

    FilePositive ClassNegative Class
    semi-metal-classificationMetalSemiconductor
    | File                        | Positive | Negative      |
    | --------------------------- | -------- | ------------- |
    | `semi-metal-classification` | Metal    | Semiconductor |
  9. Available pre-trained CGCNN regression models

    master

    The following pre-trained models are available for predicting continuous material properties. Use these files to perform regression tasks on new crystal structures.

    FilePropertyUnits
    formation-energy-per-atomFormation EnergyeV/atom
    final-energy-per-atomAbsolute EnergyeV/atom
    band-gapBand GapeV
    efermiFermi EnergyeV/atom
    bulk-moduliBulk Modulilog(GPa)
    shear-moduliShear Modulilog(GPa)
    poisson-ratioPoisson Ratio
    | File                        | Property         | Units    |
    | --------------------------- | ---------------- | -------- |
    | `formation-energy-per-atom` | Formation Energy | eV/atom  |
    | ` final-energy-per-atom`    | Absolute Energy  | eV/atom  |
    | `band-gap`                  | Band Gap         | eV       |
    | `efermi`                    | Fermi Energy     | eV/atom  |
    | `bulk-moduli`               | Bulk Moduli      | log(GPa) |
    | `shear-moduli`              | Shear Moduli     | log(GPa) |
    | `poisson-ratio`             | Poisson Ratio    | —        |
  10. Train a CGCNN model

    master

    Train a model using main.py by passing the path to your customized dataset directory.

    Arguments:

    • --train-size, --val-size, --test-size: Specify the exact number of samples for each split.
    • --train-ratio, --val-ratio, --test-ratio: Specify the fraction of data for each split (cannot be used simultaneously with size flags).
    • --task classification: Use this flag to train a classification model instead of the default regression.

    Outputs: After training, the following files are generated in the cgcnn directory:

    • model_best.pth.tar: The model with the best validation accuracy.
    • checkpoint.pth.tar: The model at the last epoch.
    • test_results.csv: Contains ID, target value, and predicted value for the test set.
    # Training with specific sizes
    python main.py --train-size 6 --val-size 2 --test-size 2 data/sample-regression
    
    # Training with ratios
    python main.py --train-ratio 0.6 --val-ratio 0.2 --test-ratio 0.2 data/sample-regression
    
    # Training a classification model
    python main.py --task classification --train-size 5 --val-size 2 --test-size 3 data/sample-classification
  11. Predict material properties with a pre-trained model

    master

    Use predict.py to estimate properties for new crystals using a pre-trained model file (.pth.tar).

    Usage: python predict.py <path_to_pretrained_model> <path_to_customized_dataset_dir>

    Outputs:

    • test_results.csv: Stores the ID, the placeholder target value from your id_prop.csv, and the predicted value.
      • For regression, this is the predicted property.
      • For classification, this is a probability between 0 and 1 representing the likelihood of the crystal belonging to class 1.
    # Predict formation energies
    python predict.py pre-trained/formation-energy-per-atom.pth.tar data/sample-regression
    
    # Predict classification (e.g., metal vs semiconductor)
    python predict.py pre-trained/semi-metal-classification.pth.tar data/sample-classification
  12. Reference: CLI Arguments for main.py

    master

    The following arguments are available in the main.py CLI interface.

    --task {regression,classification}
    --disable-cuda
    --workers N
    --epochs N
    --start-epoch N
    --batch-size N
    --lr LR
    --lr-milestones N
    --momentum M
    --weight-decay W
    --print-freq N
    --resume PATH
    --train-ratio N
    --train-size N
    --val-ratio N
    --val-size N
    --test-ratio N
    --test-size N
    --optim {SGD,Adam}
    --atom-fea-len N
    --h-fea-len N
    --n-conv N
    --n-h N