Skip to content

Instantly share code, notes, and snippets.

@borisdayma
Last active July 3, 2020 00:34
Show Gist options
  • Select an option

  • Save borisdayma/68ea13ec531ed0e5c8b2ee6dc9c759d3 to your computer and use it in GitHub Desktop.

Select an option

Save borisdayma/68ea13ec531ed0e5c8b2ee6dc9c759d3 to your computer and use it in GitHub Desktop.
Deepchem - wandb - Demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "DprlHnnr5xE4"
},
"source": [
"# Tutorial Part 2: Learning MNIST Digit Classifiers\n",
"\n",
"In the previous tutorial, we learned some basics of how to load data into DeepChem and how to use the basic DeepChem objects to load and manipulate this data. In this tutorial, you'll put the parts together and learn how to train a basic image classification model in DeepChem. You might ask, why are we bothering to learn this material in DeepChem? Part of the reason is that image processing is an increasingly important part of AI for the life sciences. So learning how to train image processing models will be very useful for using some of the more advanced DeepChem features.\n",
"\n",
"The MNIST dataset contains handwritten digits along with their human annotated labels. The learning challenge for this dataset is to train a model that maps the digit image to its true label. MNIST has been a standard benchmark for machine learning for decades at this point. \n",
"\n",
"![MNIST](https://github.com/deepchem/deepchem/blob/master/examples/tutorials/mnist_examples.png?raw=1)\n",
"\n",
"## Colab\n",
"\n",
"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.\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/deepchem/deepchem/blob/master/examples/tutorials/02_Learning_MNIST_Digit_Classifiers.ipynb)\n",
"\n",
"## Setup\n",
"\n",
"We recommend running this tutorial on Google colab. You'll need to run the following cell of installation commands on Colab to get your environment set up. If you'd rather run the tutorial locally, make sure you don't run these commands (since they'll download and install a new Anaconda python setup)"
]
},
{
"cell_type": "raw",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 462
},
"colab_type": "code",
"id": "UXJKRlAv5xFA",
"outputId": "40b16b6e-9346-403e-daae-86af016d45b4"
},
"source": [
"# Do not run this cell\n",
"%tensorflow_version 1.x\n",
"!curl -Lo deepchem_installer.py https://raw.githubusercontent.com/deepchem/deepchem/master/scripts/colab_install.py\n",
"import deepchem_installer\n",
"%time deepchem_installer.install(version='2.3.0')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "MsHJLy-35xFe"
},
"outputs": [],
"source": [
"import deepchem as dc\n",
"from deepchem.models.callbacks import ValidationCallback\n",
"import tensorflow as tf\n",
"from tensorflow.keras.layers import Reshape, Conv2D, Flatten, Dense, Softmax\n",
"import numpy as np\n",
"import wandb"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"mnist = tf.keras.datasets.mnist"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"mnist_data = mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "n0nJCPak5xFo"
},
"outputs": [],
"source": [
"train = dc.data.NumpyDataset(1.*mnist_data[0][0]*1., tf.one_hot(mnist_data[0][1], 10))\n",
"valid = dc.data.NumpyDataset(1.*mnist_data[1][0]*1., tf.one_hot(mnist_data[1][1], 10))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# hyperparameters\n",
"epochs = 1\n",
"filters_init = 32\n",
"kernel_size = 3\n",
"filters_end = 100"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Y5AfheB55xF1"
},
"outputs": [],
"source": [
"keras_model = tf.keras.Sequential([\n",
" Reshape((28, 28, 1)),\n",
" Conv2D(filters=filters_init, kernel_size=kernel_size, activation=tf.nn.relu),\n",
" Conv2D(filters=2*filters_init, kernel_size=kernel_size, activation=tf.nn.relu),\n",
" Flatten(),\n",
" Dense(filters_end, activation=tf.nn.relu),\n",
" Dense(10),\n",
" Softmax()\n",
"])\n",
"model = dc.models.KerasModel(keras_model, dc.models.losses.CategoricalCrossEntropy(), wandb=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" Logging results to <a href=\"https://wandb.com\" target=\"_blank\">Weights & Biases</a> <a href=\"https://docs.wandb.com/integrations/jupyter.html\" target=\"_blank\">(Documentation)</a>.<br/>\n",
" Project page: <a href=\"https://app.wandb.ai/borisd13/deepchem\" target=\"_blank\">https://app.wandb.ai/borisd13/deepchem</a><br/>\n",
" Run page: <a href=\"https://app.wandb.ai/borisd13/deepchem/runs/12fjcf05\" target=\"_blank\">https://app.wandb.ai/borisd13/deepchem/runs/12fjcf05</a><br/>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"W&B Run: https://app.wandb.ai/borisd13/deepchem/runs/12fjcf05"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Initialize W&B run and optionally log hyper-parameters\n",
"wandb.init(project=\"deepchem\", config={'epochs':epochs, 'kernel':kernel_size, 'filters_init':filters_init, 'filters_end':filters_end})"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"metric = dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 275
},
"colab_type": "code",
"id": "Xq9T4trd5xGD",
"outputId": "e626df29-14e6-46ad-e5db-a039de833366"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Step 100 validation: mean-roc_auc_score=0.998195\n",
"Step 200 validation: mean-roc_auc_score=0.999205\n",
"Step 300 validation: mean-roc_auc_score=0.99931\n",
"Step 400 validation: mean-roc_auc_score=0.999659\n",
"Step 500 validation: mean-roc_auc_score=0.999566\n",
"Step 600 validation: mean-roc_auc_score=0.99963\n"
]
},
{
"data": {
"text/plain": [
"0.0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.fit(train, nb_epoch=epochs, callbacks=[ValidationCallback(valid, model.log_frequency, [metric])])"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "ccdgh2Ni5xGx"
},
"source": [
"# Congratulations! Time to join the Community!\n",
"\n",
"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:\n",
"\n",
"## Star DeepChem on [GitHub](https://github.com/deepchem/deepchem)\n",
"This helps build awareness of the DeepChem project and the tools for open source drug discovery that we're trying to build.\n",
"\n",
"## Join the DeepChem Gitter\n",
"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!"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"name": "02_Learning_MNIST_Digit_Classifiers.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment