Unverified Commit 355954b3 authored by peastman's avatar peastman Committed by GitHub
Browse files

Merge pull request #2315 from peastman/tutorials

Updated more tutorials
parents c3b5d77f 9093c9b2
Loading
Loading
Loading
Loading
+0 −214
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# Tutorial Part 10: Exploring Quantum Chemistry with GDB1k

%% Cell type:markdown id: tags:

Most of the tutorials we've walked you through so far have focused on applications to the drug discovery realm, but DeepChem's tool suite works for molecular design problems generally. In this tutorial, we're going to walk through an example of how to train a simple molecular machine learning for the task of predicting the atomization energy of a molecule. (Remember that the atomization energy is the energy required to form 1 mol of gaseous atoms from 1 mol of the molecule in its standard state under standard conditions).

## Colab

This tutorial and the rest in this sequence are designed to be done in Google colab. If you'd like to open this notebook in colab, you can use the following link.

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/deepchem/deepchem/blob/master/examples/tutorials/10_Exploring_Quantum_Chemistry_with_GDB1k.ipynb)

## Setup

To run DeepChem within Colab, you'll need to run the following cell of installation commands. This will take about 5 minutes to run to completion and install your environment.

%% Cell type:code id: tags:

``` python
!curl -Lo conda_installer.py https://raw.githubusercontent.com/deepchem/deepchem/master/scripts/colab_install.py
import conda_installer
conda_installer.install()
!/root/miniconda/bin/conda info -e
```

%% Output

      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  3489  100  3489    0     0  37923      0 --:--:-- --:--:-- --:--:-- 37923

    all packages is already installed

    # conda environments:
    #
    base                  *  /root/miniconda
    

%% Cell type:code id: tags:

``` python
!pip install --pre deepchem
import deepchem
deepchem.__version__
```

%% Output

    Requirement already satisfied: deepchem in /usr/local/lib/python3.6/dist-packages (2.4.0rc1.dev20200805143010)
    Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from deepchem) (1.4.1)
    Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from deepchem) (1.0.5)
    Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from deepchem) (1.18.5)
    Requirement already satisfied: joblib in /usr/local/lib/python3.6/dist-packages (from deepchem) (0.16.0)
    Requirement already satisfied: scikit-learn in /usr/local/lib/python3.6/dist-packages (from deepchem) (0.22.2.post1)
    Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->deepchem) (2018.9)
    Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas->deepchem) (2.8.1)
    Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.6.1->pandas->deepchem) (1.15.0)

    '2.4.0-rc1.dev'

%% Cell type:markdown id: tags:

With our setup in place, let's do a few standard imports to get the ball rolling.

%% Cell type:code id: tags:

``` python
import os
import unittest
import numpy as np
import deepchem as dc
import numpy.random
from deepchem.utils.evaluate import Evaluator
from sklearn.ensemble import RandomForestRegressor
from sklearn.kernel_ridge import KernelRidge
```

%% Cell type:markdown id: tags:

The ntext step we want to do is load our dataset. We're using a small dataset we've prepared that's pulled out of the larger GDB benchmarks. The dataset contains the atomization energies for 1K small molecules.

%% Cell type:code id: tags:

``` python
tasks = ["atomization_energy"]
dataset_file = "../../datasets/gdb1k.sdf"
smiles_field = "smiles"
mol_field = "mol"
```

%% Cell type:markdown id: tags:

We now need a way to transform molecules that is useful for prediction of atomization energy. This representation draws on foundational work [1] that represents a molecule's 3D electrostatic structure as a 2D matrix $C$ of distances scaled by charges, where the $ij$-th element is represented by the following charge structure.

$C_{ij} = \frac{q_i q_j}{r_{ij}^2}$

If you're observing carefully, you might ask, wait doesn't this mean that molecules with different numbers of atoms generate matrices of different sizes? In practice the trick to get around this is that the matrices are "zero-padded." That is, if you're making coulomb matrices for a set of molecules, you pick a maximum number of atoms $N$, make the matrices $N\times N$ and set to zero all the extra entries for this molecule. (There's a couple extra tricks that are done under the hood beyond this. Check out reference [1] or read the source code in DeepChem!)

DeepChem has a built in featurization class `dc.feat.CoulombMatrixEig` that can generate these featurizations for you.

%% Cell type:code id: tags:

``` python
featurizer = dc.feat.CoulombMatrixEig(23, remove_hydrogens=False)
```

%% Cell type:markdown id: tags:

Note that in this case, we set the maximum number of atoms to $N = 23$. Let's now load our dataset file into DeepChem. As in the previous tutorials, we use a `Loader` class, in particular `dc.data.SDFLoader` to load our `.sdf` file into DeepChem. The following snippet shows how we do this:

%% Cell type:code id: tags:

``` python
# loader = dc.data.SDFLoader(
#       tasks=["atomization_energy"], smiles_field="smiles",
#       featurizer=featurizer,
#       mol_field="mol")
# dataset = loader.featurize(dataset_file)
```

%% Cell type:markdown id: tags:

For the purposes of this tutorial, we're going to do a random split of the dataset into training, validation, and test. In general, this split is weak and will considerably overestimate the accuracy of our models, but for now in this simple tutorial isn't a bad place to get started.

%% Cell type:code id: tags:

``` python
# random_splitter = dc.splits.RandomSplitter()
# train_dataset, valid_dataset, test_dataset = random_splitter.train_valid_test_split(dataset)
```

%% Cell type:markdown id: tags:

One issue that Coulomb matrix featurizations have is that the range of entries in the matrix $C$ can be large. The charge $q_1q_2/r^2$ term can range very widely. In general, a wide range of values for inputs can throw off learning for the neural network. For this, a common fix is to normalize the input values so that they fall into a more standard range. Recall that the normalization transform applies to each feature $X_i$ of datapoint $X$

$\hat{X_i} = \frac{X_i - \mu_i}{\sigma_i}$

where $\mu_i$ and $\sigma_i$ are the mean and standard deviation of the $i$-th feature. This transformation enables the learning to proceed smoothly. A second point is that the atomization energies also fall across a wide range. So we apply an analogous transformation normalization transformation to the output to scale the energies better. We use DeepChem's transformation API to make this happen:

%% Cell type:code id: tags:

``` python
# transformers = [
#     dc.trans.NormalizationTransformer(transform_X=True, dataset=train_dataset),
#     dc.trans.NormalizationTransformer(transform_y=True, dataset=train_dataset)]

# for dataset in [train_dataset, valid_dataset, test_dataset]:
#   for transformer in transformers:
#       dataset = transformer.transform(dataset)
```

%% Cell type:markdown id: tags:

Now that we have the data cleanly transformed, let's do some simple machine learning. We'll start by constructing a random forest on top of the data. We'll use DeepChem's hyperparameter tuning module to do this.

%% Cell type:code id: tags:

``` python
# def rf_model_builder(model_params, model_dir):
#   sklearn_model = RandomForestRegressor(**model_params)
#   return dc.models.SklearnModel(sklearn_model, model_dir)
# params_dict = {
#     "n_estimators": [10, 100],
#     "max_features": ["auto", "sqrt", "log2", None],
# }

# metric = dc.metrics.Metric(dc.metrics.mean_absolute_error)
# optimizer = dc.hyper.HyperparamOpt(rf_model_builder)
# best_rf, best_rf_hyperparams, all_rf_results = optimizer.hyperparam_search(
#     params_dict, train_dataset, valid_dataset, transformers,
#     metric=metric)
```

%% Cell type:markdown id: tags:

Let's build one more model, a kernel ridge regression, on top of this raw data.

%% Cell type:code id: tags:

``` python
# def krr_model_builder(model_params, model_dir):
#   sklearn_model = KernelRidge(**model_params)
#   return dc.models.SklearnModel(sklearn_model, model_dir)

# params_dict = {
#     "kernel": ["laplacian"],
#     "alpha": [0.0001],
#     "gamma": [0.0001]
# }

# metric = dc.metrics.Metric(dc.metrics.mean_absolute_error)
# optimizer = dc.hyper.HyperparamOpt(krr_model_builder)
# best_krr, best_krr_hyperparams, all_krr_results = optimizer.hyperparam_search(
#     params_dict, train_dataset, valid_dataset, transformers,
#     metric=metric)
```

%% Cell type:markdown id: tags:

# Congratulations! Time to join the Community!

Congratulations on completing this tutorial notebook! If you enjoyed working through the tutorial, and want to continue working with DeepChem, we encourage you to finish the rest of the tutorials in this series. You can also help the DeepChem community in the following ways:

## Star DeepChem on [GitHub](https://github.com/deepchem/deepchem)
This helps build awareness of the DeepChem project and the tools for open source drug discovery that we're trying to build.

## Join the DeepChem Gitter
The DeepChem [Gitter](https://gitter.im/deepchem/Lobby) hosts a number of scientists, developers, and enthusiasts interested in deep learning for the life sciences. Join the conversation!

# Bibliography:

[1] https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.98.146401
+0 −488

File deleted.

Preview size limit exceeded, changes collapsed.

+425 −0

File added.

Preview size limit exceeded, changes collapsed.

+399 −0

File added.

Preview size limit exceeded, changes collapsed.