SchNetPack - Deep Neural Networks for Atomistic Systems

repository·master·Indexed 21 days ago

https://github.com/atomistic-machine-learning/schnetpack

A toolbox for developing and applying deep neural networks to predict potential energy surfaces and quantum-chemical properties of molecules and materials. It features building blocks like SchNet and PaiNN, a Hydra-based CLI (spktrain) for training via PyTorch Lightning, and a GPU-accelerated molecular dynamics framework including integrators for NVE, NVT, and NPT ensembles, as well as specialized atomistic output and physical prior layers.

Tokens
37.1K
Snippets
102
Records
150
Agent score
75%

What's inside SchNetPack

  1. Overview of SchNetPack

    master

    SchNetPack is a toolbox designed for developing and applying deep neural networks to predict potential energy surfaces and quantum-chemical properties of molecules and materials.

    Key capabilities include:

    • Providing basic building blocks for atomistic neural networks.
    • Managing the training process of these models.
    • Providing simple access to common benchmark datasets.
    • Enabling easy implementation and evaluation of new models.
  2. Use atomistic transformations in SchNetPack

    master

    The schnetpack.transform module provides a suite of transformation classes used to manipulate atomistic data (coordinates, species, etc.) during data loading or preprocessing. These transformations are typically applied to datasets to ensure physical consistency or to prepare data for specific model architectures.

    Atomistic Transformations

    These classes modify the physical properties of the system:

    • AddOffsets: Adds offsets to coordinates.
    • RemoveOffsets: Removes offsets from coordinates.
    • SubtractCenterOfMass: Translates the system so the center of mass is at the origin.
    • SubtractCenterOfGeometry: Translates the system so the geometric center is at the origin.

    Casting Transformations

    These classes manage data types (precision) for tensors:

    • CastMap: Maps specific keys to specific types.
    • CastTo32: Casts tensors to float32.
    • CastTo64: Casts tensors to float64.

    Neighbor List Transformations

    These classes handle the construction and filtering of neighbor lists, which are critical for message-passing neural networks:

    • MatScipyNeighborList: Uses SciPy for neighbor list construction.
    • ASENeighborList: ASE-based neighbor list.
    • VesinNeighborList: Vesin-based neighbor list.
    • TorchNeighborList: PyTorch-native neighbor list.
    • CachedNeighborList: A neighbor list that caches results.
    • CountNeighbors: Counts the number of neighbors for each atom.
    • FilterNeighbors: Filters neighbors based on specific criteria.
    • WrapPositions: Wraps positions according to periodic boundary conditions.
    • CollectAtomTriples: Collects atom triples for specific interaction types.
  3. Manage atomistic data with schnetpack.data

    master

    The schnetpack.data module provides the core abstractions for handling atomistic datasets. It includes classes for loading atoms from various formats (like ASE), creating datasets, and managing data modules for training workflows. Key components include:

    • Atoms Data Handling: Use BaseAtomsData, ASEAtomsData, or AtomsLoader to interface with atomic structures.
    • Dataset Lifecycle: Use create_dataset to build new datasets and load_dataset to retrieve existing ones.
    • Training Integration: Use AtomsDataModule to wrap datasets into a format suitable for machine learning training pipelines.
    • Data Statistics: Use calculate_stats to compute dataset properties and Criterion classes to filter or sample data based on specific properties.
  4. Configure SchNetPack using Hydra and YAML

    master

    SchNetPack uses hierarchical Hydra configuration files in YAML format to define models and tasks. These configurations can be modified dynamically via command-line arguments.

    Key features include:

    • Hierarchical Structure: Configs are organized into groups (e.g., data, model, task) that can be nested.
    • Interpolation: Use ${group.variable} to reference values from other parts of the configuration (e.g., ${globals.cutoff}).
    • Resolvers: Use ${resolver:arguments} to evaluate functions during config construction. Built-in resolvers include ${hydra:runtime.cwd} for the current working directory and custom resolvers like ${uuid:1} for generating cached unique identifiers.
    • Object Instantiation: The special key _target_ specifies the class to be instantiated, while other keys provide the arguments for that class's __init__ method.
    # Example command to run a training experiment using a specific config group
    $ spktrain experiment=qm9_atomwise
  5. Structure a model using NeuralNetworkPotential

    master

    While AtomisticModel is general, SchNetPack models should ideally follow the structure of the NeuralNetworkPotential subclass to ensure compatibility with Hydra configuration templates. The structure consists of:

    1. Input modules: A sequence of PyTorch modules that sequentially modify the input dictionary.
    2. Representation: A module (e.g., SchNet or PaiNN) that computes atomwise representations and adds them to the dictionary.
    3. Output modules: A sequence of PyTorch modules that extract specific outputs and store them in the dictionary.
  6. Use message-passing neural networks in schnetpack.representation

    master

    The schnetpack.representation module provides implementations of message-passing neural networks (MPNNs) designed for atomistic systems. The primary architectures available are:

    • SchNet: A continuous-filter convolutional neural network designed for learning representations of molecular systems.
    • PaiNN: A equivariant message-passing neural network that accounts for directional information (vectors) in addition to scalar properties.
  7. Define experiments using hierarchical configuration

    master

    SchNetPack uses a hierarchical configuration system (via Hydra) to assemble experiments. You can define an experiment config that overrides defaults for models, datasets, and tasks.

    Key concepts:

    • # @package _global_: Placing this at the top of your config file ensures the configuration is placed at the base level of the hierarchy, allowing it to directly override train.yaml.
    • defaults: Used to select and override predefined configuration groups (e.g., model, data, task).
    • _target_: Specifies the Python class/module to instantiate for a specific component.
    • Interpolation (${...}): Allows referencing variables defined elsewhere in the config (e.g., ${globals.property}).

    An experiment config typically defines:

    1. Defaults: Which model and data modules to use.
    2. Globals: Shared variables like cutoff, lr, or the target property.
    3. Data Transforms: A list of pre-processing steps (e.g., SubtractCenterOfMass, RemoveOffsets, CastTo32).
    4. Model Architecture: Output modules (e.g., Atomwise) and post-processors (e.g., AddOffsets).
    5. Task Definition: The loss function (e.g., torch.nn.MSELoss) and metrics (e.g., MeanAbsoluteError).
    # @package _global_
    
    defaults:
      - override /model: nnp
      - override /data: qm9
    
    run.path: runs/qm9_${globals.property}
    
    globals:
      cutoff: 5.
      lr: 5e-4
      property: energy_U0
    
    data:
      transforms:
        - _target_: schnetpack.transform.SubtractCenterOfMass
        - _target_: schnetpack.transform.RemoveOffsets
          property: ${globals.property}
          remove_atomrefs: True
          remove_mean: True
        - _target_: schnetpack.transform.MatScipyNeighborList
          cutoff: ${globals.cutoff}
        - _target_: schnetpack.transform.CastTo32
    
    model:
      output_modules:
        - _target_: schnetpack.atomistic.Atomwise
          output_key: ${globals.property}
          n_in: ${model.representation.n_atom_basis}
          aggregation_mode: sum
      postprocessors:
        - _target_: schnetpack.transform.CastTo64
        - _target_: schnetpack.transform.AddOffsets
          property: ${globals.property}
          add_mean: True
          add_atomrefs: True
    
    task:
      outputs:
        - _target_: schnetpack.task.ModelOutput
          name: ${globals.property}
          loss_fn:
            _target_: torch.nn.MSELoss
          metrics:
            mae:
              _target_: torchmetrics.regression.MeanAbsoluteError
            mse:
              _target_: torchmetrics.regression.MeanSquaredError
          loss_weight: 1.
  8. Calculate dataset statistics and use criteria

    master

    To prepare a dataset for training, you can compute statistical properties and use criteria to filter or sample data.

    • calculate_stats: Computes statistics for the provided dataset.
    • Criteria: Use these to define selection logic for data:
      • NumberOfAtomsCriterion: Filters or selects data based on the number of atoms in the system.
      • PropertyCriterion: Filters or selects data based on specific target properties (e.g., energy, forces).
  9. Understand the SchNetPack architecture

    master

    SchNetPack is designed to be used both as a command-line tool (configured via Hydra) and as a Python library. It is built on top of PyTorch and uses PyTorchLightning as its training framework. The architecture is organized into four main pillars:

    1. Data: Handles dataset loading, preprocessing (via Transforms), and partitioning (via AtomsDataModule).
    2. Model: The neural network architecture, centered around the AtomisticModel class.
    3. Task: The training logic that connects the model, outputs, loss functions, and optimizers using AtomisticTask (a LightningModule).
    4. Configuration: Uses Hydra for command-line configuration and management.
  10. Understand SchNetPack configuration groups

    master

    SchNetPack training runs are based on a train.yaml file which sets up default configuration groups. Instead of writing a full configuration from scratch, you can use these groups to predefine templates.

    Default config groups in train.yaml include:

    GroupPurpose
    runDefines run-specific variables like id, working directories, and data directories.
    globalsDefines reusable custom variables used via interpolation (e.g., ${globals.variable}).
    dataDefines the data.AtomsDataModule to be used.
    modelDefines the model.AtomisticModel to be used.
    taskDefines the task.AtomisticTask.
    trainerConfigures the PyTorchLightning Trainer.
    callbacksA list of callbacks for the PyTorchLightning Trainer.
    loggerA dictionary of training loggers passed to the trainer.
    seedSets the random seed.
    experimentUsed to overwrite defaults in train.yaml to create pre-defined experiment templates (e.g., QM9).

    Default configurations are located in src/schnetpack/configs.

  11. Manage atomistic data with ASEAtomsData and AtomsDataModule

    master

    SchNetPack manages datasets through several specialized classes:

    • ASEAtomsData: The primary class for loading datasets stored in ASE format. You can implement custom formats by subclassing BaseAtomsData.
    • Transform: PyTorch modules used for preprocessing data before batching (e.g., removing property offsets or calculating neighbor lists). These typically run on the CPU.
    • AtomsDataModule: A PyTorch Lightning datamodule that wraps ASEAtomsData to handle data preparation, setup, and splitting into training, validation, and test sets.
  12. Perform molecular dynamics simulations with the `schnetpack.md` module

    master
    The schnetpack.md module provides a complete framework for performing molecular dynamics (MD) simulations. It orchestrates several key components: a System to define the atoms, InitialConditions to set starting states, Integrators to evolve the system in time, and Calculators to provide the forces and energies. The simulation is managed by the Simulator class, which can be extended using SimulationHook objects for thermostats, barostats, and logging.