Tutorial to Generate Statistical Models

In this tutorial we explore how to create and train statistical models to predict molecular properties using the Pytorch library. We will use smiles to represent the molecules and use the csv file format to manipulate the molecules and their properties.

As an example, we will predict the activity coefficient_ for a subset of carboxylic acids taken from the GDB-13 database_. Firstly, We randomly takes a 1000 smiles from the database and compute the activity coefficient_ using the COSMO approach_. We store the values in the thousand.csv_ file.

A peek into the file will show you something like:


Where the first column contains the index of the row, the second the solvation energy and finally the activity coefficients_ denoted as gammas. Once we have the data we can start exploring different statistical methods.

swan offers a thin interface to Pytorch. It takes yaml file as input and either train an statistical model or generates a prediction using a previously trained model. Let’s briefly explore the swan input.

Simulation input

A typical swan input file looks like:

  - gammas

use_cuda: True

  fingerprint: atompair

  name: FingerprintFullyConnected
     input_features: 2048  # Fingerprint size
     hidden_cells: 200
     output_features: 1  # We are predicting a single property

  epochs: 100
  batch_size: 100
    name: sgd
    lr: 0.002

dataset_file: A csv file with the smiles and other molecular properties.

properties: the columns names of hte csv file representing the molecular properties to fit.

featurizer: The type of transformation to apply to the smiles to generates the features. Could be either fingerprint or graph.

Have a look at the Available models.

Training a model

In order to run the training, run the following command:

modeller --mode train -i input.yml

swan will generate a log file called output.log with a timestamp for the different steps during the training. Finally, you can see in your cwd a folder called swan_models containing the parameters of your statistical model.

It is possible to restart the training procedure by providing the --restart option like:

modeller --mode train -i input.yml --restart

Predicting new data

To predict new data you need to provide some smiles for which you want to compute the properties of interest, in this case the activity coefficient_. For doing so, you need to provide in the dataset_file entry of the input.yml file the path to a csv file containing the smiles, like the smiles.csv_:


Then run the command:

modeler --mode predict -i input.yml

swan will look for a swan_model.pt file with the previously trained model and will load it.

Finally, you will find a file called “predicted.csv” with the predicted values for the activity coefficients.