From 57e46c6160a9b6d366b1ec85d0c89b0cac9045dc Mon Sep 17 00:00:00 2001 From: Yuwen Hu <54161268+Oscilloscope98@users.noreply.github.com> Date: Thu, 1 Sep 2022 11:54:24 +0800 Subject: [PATCH] [Documentation] Add Extension nbsphinx-link (#5596) * Add extension nbsphinx-link: link notebook out of the sphinx root folder to the sphinx project * Link Notebook custom.ipynb directly from python folder to the sphinx project * Link Notebook seq_and_func.ipynb directly from python folder to the sphinx project * Small title fix * Make interactive graphs visible in readthedocs webpage for seq_and_func.ipynb * Regenerate outputs for custom.ipynb and remove parts of them --- docs/readthedocs/requirements-doc.txt | 1 + docs/readthedocs/source/conf.py | 3 +- .../source/doc/Nano/Tutorials/custom.ipynb | 393 ------------ .../source/doc/Nano/Tutorials/custom.nblink | 3 + .../doc/Nano/Tutorials/seq_and_func.ipynb | 574 ------------------ .../doc/Nano/Tutorials/seq_and_func.nblink | 3 + 6 files changed, 9 insertions(+), 968 deletions(-) delete mode 100644 docs/readthedocs/source/doc/Nano/Tutorials/custom.ipynb create mode 100644 docs/readthedocs/source/doc/Nano/Tutorials/custom.nblink delete mode 100644 docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.ipynb create mode 100644 docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.nblink diff --git a/docs/readthedocs/requirements-doc.txt b/docs/readthedocs/requirements-doc.txt index 15e97692..57b5b471 100644 --- a/docs/readthedocs/requirements-doc.txt +++ b/docs/readthedocs/requirements-doc.txt @@ -39,3 +39,4 @@ sphinx-design==0.2.0 sphinx-external-toc==0.3.0 nbsphinx==0.8.9 sphinx-design==0.2.0 +nbsphinx-link==1.3.0 \ No newline at end of file diff --git a/docs/readthedocs/source/conf.py b/docs/readthedocs/source/conf.py index 277de7fc..ec9e1f1b 100644 --- a/docs/readthedocs/source/conf.py +++ b/docs/readthedocs/source/conf.py @@ -93,7 +93,8 @@ extensions = [ 'sphinx_design', 'sphinx_external_toc', 'sphinx_design', - 'nbsphinx' + 'nbsphinx', + 'nbsphinx_link' ] # Add any paths that contain templates here, relative to this directory. diff --git a/docs/readthedocs/source/doc/Nano/Tutorials/custom.ipynb b/docs/readthedocs/source/doc/Nano/Tutorials/custom.ipynb deleted file mode 100644 index dfb67858..00000000 --- a/docs/readthedocs/source/doc/Nano/Tutorials/custom.ipynb +++ /dev/null @@ -1,393 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "yzsDlbsUBsuF" - }, - "source": [ - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/intel-analytics/BigDL/blob/main/python/nano/notebooks/hpo/custom.ipynb)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "zcHLkcVjB7Jg" - }, - "source": [ - "![image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJsAAABHCAMAAAAnQ8XqAAAACXBIWXMAAA7DAAAOwwHHb6hkAAADAFBMVEVHcEyAgYR+gYU0OD85OTuOkZSChYk5OTs5OTs5OTuAgYR/gYM5OTuAgYSAgYSBgoWAgoU5OTs+NTg5OTs4ODs5OTsAccQ1NTk3Nzo4ODuBg4U4ODs5OTs4ODs5OTuRlJY6OjwAccM4ODs5OTs3Nzo4ODuBgoQ3Nzo4OTo3NzqSlJc5OTsBccOAgYSRlJeRk5Y5OTs5OTs5OTs4ODuPkpQ4ODo4ODs5OTs5OTs4ODuRlJeSlJeJio4BccM5OTsDbrw4ODuPkpU4ODuVmJs4ODsBccOTlpk2Njo3OTwBccOSlZiUmJo5OTs5OTs5OTyPkZSRk5eTlpk5OTw4ODs5OTw2Njk5OTuRlJeUl5pzi6EAccM5OTsBcMM4ODs4ODs4ODs4ODs4ODs5OTs4ODuAgYSAgYM4ODs4ODuTl5kBccM4ODo4ODs4ODs5OTs4ODs5OTs5OTuSlJc4ODs4ODs5OTs4ODuAgYSRk5aFh4o4ODucn6I4ODs4ODv7/P4BccM5OTuAgoVDQ0c4ODsBccM4ODtFf7ABcMM5OTuTlZh/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tDvteemhrUio07XlgzYSoAZvVTu4XVP0nzTl44oewrYPbid0Z1Nn3dvjXw90YPZDt9W+fuSZEtZER1ZTar9oeTwWY7LL1OXGcIPCxp6r9bHwZx2+p7L/8dQwXHj7188Ofu3zUHtoBqzWXbNz84eOw4Kjh28NXqRROWCDDoyeMeKmcQRgYKCgqCmEsKFBgkd3Kyg+e6wCQoKx9xYhACzxnp6h7ahQHO7zr/cNeWzUBJMDi06xA62HVIV5dxSSpDB6WrU4QYZhyxAI9IrIUMTwCzsimDECtDJ2MKIyM/LsAIAzhku6/M1XNjoHBtniBDV/7ZBTyQCWoOTp6lZ6ODgKEJDLclhMAlPFKXzvV744lRoh1nOsnwyPo9oEn9netWKmdrQiYEHfnU+EAIBKAUH7KAGp+aGpoAisrZ1FhzBMwewVPajUTt7OyqzQu6sGTlAQRCoHBSMQUCcO6AZRgmygAblVwnqCAIYwjRJTgAjNdLil1g3K4AAAAASUVORK5CYII=)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "rICdZgjQBfUl" - }, - "source": [ - "# BigDL-Nano Hyperparameter Tuning (Tensorflow Subclassing Model) Quickstart\n", - "In this notebook we demonstrates how to use Nano HPO to tune the hyperparameters in tensorflow training. The model is built by subclassing tensorflow.keras.Model.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XZHU5E3ABfUm" - }, - "source": [ - "## Step 0: Prepare Environment\n", - "You can install the latest pre-release version with nano support using below commands.\n", - "\n", - "We recommend to run below commands, especially `source bigdl-nano-init` before jupyter kernel is started, or some of the optimizations may not take effect." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "3UyXJWA4Cjx2", - "outputId": "4518ce08-cfc8-4f6c-c40c-01006ffabc81" - }, - "outputs": [], - "source": [ - "# Install latest pre-release version of bigdl-nano\n", - "!pip install --pre bigdl-nano[tensorflow]\n", - "!pip install setuptools==58.0.4\n", - "!pip install protobuf==3.20.1\n", - "!source bigdl-nano-init" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "phw-whHaCnpR", - "outputId": "b517e45c-c98a-4b12-820b-a1a8009fc5aa" - }, - "outputs": [], - "source": [ - "# Install other dependecies for Nano HPO\n", - "!pip install ConfigSpace\n", - "!pip install optuna" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "xKKCHTU9BfUn" - }, - "source": [ - "## Step 1: Init Nano AutoML\n", - "We need to enable Nano HPO before we use it for tensorflow training." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "gF1XGVtuBfUn", - "outputId": "c610c446-328d-43e4-e701-51fe180aef1f" - }, - "outputs": [], - "source": [ - "import bigdl.nano.automl as automl\n", - "import bigdl.nano.automl.hpo as hpo\n", - "automl.hpo_config.enable_hpo_tf()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3InpJGOuBfUp" - }, - "source": [ - "## Step 2: Prepare data\n", - "We use fashion MNIST dataset for demonstration." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "A4AjqFBTBfUp", - "outputId": "d80596f1-98e4-4358-a583-706e24c3da0c" - }, - "outputs": [], - "source": [ - "from tensorflow import keras\n", - "(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()\n", - "\n", - "CLASSES = 10\n", - "\n", - "img_x, img_y = x_train.shape[1], x_train.shape[2]\n", - "x_train = x_train.reshape(-1, img_x, img_y,1).astype(\"float32\") / 255\n", - "x_test = x_test.reshape(-1, img_x, img_y,1).astype(\"float32\") / 255" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "hwrtVD9yBfUq" - }, - "source": [ - "## Step 3: Build model and specify search spaces\n", - "We now create our model. \n", - "\n", - "Decorate the model class with hpo.tfmodel, and you will be able to specify search spaces in init arguments when creating the model, as shown below. For more details, refer to [user doc](https://bigdl.readthedocs.io/en/latest/doc/Nano/QuickStart/hpo.html).\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "uRFIRllGr0xu" - }, - "outputs": [], - "source": [ - "import tensorflow as tf\n", - "from tensorflow.keras.datasets import mnist\n", - "from tensorflow.keras.layers import Conv2D, Dropout, MaxPooling2D\n", - "from tensorflow.keras.layers import Dense\n", - "from tensorflow.keras.layers import Flatten" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "SNmZHFMMBfUr" - }, - "outputs": [], - "source": [ - "import tensorflow as tf\n", - "@hpo.tfmodel()\n", - "class MyModel(tf.keras.Model):\n", - "\n", - " def __init__(self, filters, kernel_size, strides, activation):\n", - " super().__init__()\n", - " self.conv1 = Conv2D(\n", - " filters=filters,\n", - " kernel_size=kernel_size,\n", - " strides=strides,\n", - " activation=activation)\n", - " self.pool1 = MaxPooling2D(pool_size=2)\n", - " self.drop1 = Dropout(0.3)\n", - " self.flat = Flatten()\n", - " self.dense1 = Dense(256, activation='relu')\n", - " self.drop3 = Dropout(0.5)\n", - " self.dense2 = Dense(CLASSES, activation=\"softmax\")\n", - "\n", - " def call(self, inputs):\n", - " x = self.conv1(inputs)\n", - " x = self.pool1(x)\n", - " x = self.drop1(x)\n", - " x = self.flat(x)\n", - " x = self.dense1(x)\n", - " x = self.drop3(x)\n", - " x = self.dense2(x)\n", - " return x\n", - "model = MyModel(\n", - " filters=hpo.space.Categorical(32, 64),\n", - " kernel_size=hpo.space.Categorical(2, 4),\n", - " strides=hpo.space.Categorical(1, 2),\n", - " activation=hpo.space.Categorical(\"relu\", \"linear\")\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "msSa9fyeGHTU" - }, - "source": [ - "## Step 4: Compile model\n", - "We now compile our model with loss function, optimizer and metrics. If you want to tune learning rate and batch size, refer to [user guide](https://bigdl.readthedocs.io/en/latest/doc/Nano/QuickStart/hpo.html#search-the-learning-rate)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "PfeBIEmoGHxA" - }, - "outputs": [], - "source": [ - "from tensorflow.keras.optimizers import RMSprop\n", - "model.compile(\n", - " loss=\"sparse_categorical_crossentropy\",\n", - " optimizer=RMSprop(learning_rate=0.001),\n", - " metrics=[\"accuracy\"],\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tuZXTIjXBfUs" - }, - "source": [ - "## Step 5: Run hyperparameter search\n", - "Run hyperparameter search by calling `model.search`. Set the `target_metric` and `direction` so that HPO optimizes the `target_metric` in the specified `direction`. Each trial will use a different set of hyperparameters in the search space range. Use `n_parallels` to set the nubmer of parallel processes to run trials. After search completes, you can use `search_summary` to retrive the search results for analysis. For more details, refer to [user doc](https://bigdl.readthedocs.io/en/latest/doc/Nano/QuickStart/hpo.html)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "hfBS_1JLBfUs", - "outputId": "897ac4d1-5766-44f2-d6fb-ed7b2a437b9e" - }, - "outputs": [], - "source": [ - "%%time\n", - "from bigdl.nano.automl.hpo.backend import PrunerType\n", - "model.search(\n", - " n_trials=5,\n", - " target_metric='val_accuracy',\n", - " direction=\"maximize\",\n", - " pruner=PrunerType.HyperBand,\n", - " pruner_kwargs={'min_resource':1, 'max_resource':100, 'reduction_factor':3},\n", - " x=x_train,\n", - " y=y_train,\n", - " batch_size=128,\n", - " epochs=5,\n", - " validation_split=0.2,\n", - " verbose=False,\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "jeK1hT42HCD5", - "outputId": "204acc43-94db-4cb8-f058-46c02e43c6ab" - }, - "outputs": [], - "source": [ - "print(model.search_summary())" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 345 - }, - "id": "n_vwMwJVLaCc", - "outputId": "8996e8ac-5ced-4cfb-c454-de4df5860012" - }, - "outputs": [], - "source": [ - "study = model.search_summary()\n", - "study.trials_dataframe(attrs=(\"number\", \"value\", \"params\", \"state\"))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "NVDG1hl-BfUt" - }, - "source": [ - "## Step 6: fit with the best hyperparameters\n", - "After search, `model.fit` will autotmatically use the best hyperparmeters found in search to fit the model." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "aAet3uX5BfUt", - "outputId": "2401cd38-09a6-4251-b459-c97e64d89cc9" - }, - "outputs": [], - "source": [ - "history = model.fit(x_train, y_train,\n", - " batch_size=128, epochs=5, validation_split=0.2)\n", - "\n", - "test_scores = model.evaluate(x_test, y_test, verbose=2)\n", - "print(\"Test loss:\", test_scores[0])\n", - "print(\"Test accuracy:\", test_scores[1])" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9JKTKAGQBfUt" - }, - "source": [ - "Check out the summary of the model. The model has already been built with the best hyperparameters found by nano hpo." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "d8O_cz4DBfUu", - "outputId": "dd7d6add-a32c-4618-dc88-d19bec8e4c3e" - }, - "outputs": [], - "source": [ - "print(model.summary())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can find the running output from [here](https://github.com/intel-analytics/BigDL/blob/main/python/nano/notebooks/hpo/custom.ipynb), or run the notebook by yourself in [Google Colab](https://colab.research.google.com/github/intel-analytics/BigDL/blob/main/python/nano/notebooks/hpo/custom.ipynb)." - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "custom.ipynb", - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.7.13" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/docs/readthedocs/source/doc/Nano/Tutorials/custom.nblink b/docs/readthedocs/source/doc/Nano/Tutorials/custom.nblink new file mode 100644 index 00000000..f3150216 --- /dev/null +++ b/docs/readthedocs/source/doc/Nano/Tutorials/custom.nblink @@ -0,0 +1,3 @@ +{ + "path": "../../../../../../python/nano/notebooks/hpo/custom.ipynb" +} \ No newline at end of file diff --git a/docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.ipynb b/docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.ipynb deleted file mode 100644 index 816ef75c..00000000 --- a/docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.ipynb +++ /dev/null @@ -1,574 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "pDIKqAcwmexW" - }, - "source": [ - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/intel-analytics/BigDL/blob/main/python/nano/notebooks/hpo/seq_and_func.ipynb)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "SQRh9TDkmexb" - }, - "source": [ - "![image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJsAAABHCAMAAAAnQ8XqAAAACXBIWXMAAA7DAAAOwwHHb6hkAAADAFBMVEVHcEyAgYR+gYU0OD85OTuOkZSChYk5OTs5OTs5OTuAgYR/gYM5OTuAgYSAgYSBgoWAgoU5OTs+NTg5OTs4ODs5OTsAccQ1NTk3Nzo4ODuBg4U4ODs5OTs4ODs5OTuRlJY6OjwAccM4ODs5OTs3Nzo4ODuBgoQ3Nzo4OTo3NzqSlJc5OTsBccOAgYSRlJeRk5Y5OTs5OTs5OTs4ODuPkpQ4ODo4ODs5OTs5OTs4ODuRlJeSlJeJio4BccM5OTsDbrw4ODuPkpU4ODuVmJs4ODsBccOT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CbwtgAG46pcg2oecdg+1uPQ5ELOmxg5tIQ24OFQrkSw4pEZLqzUsB8af+NclYlrZg+fXpNTc2c1MmgcLU43pzVGt9UaLYjts/aG5nwvulWXZGNokySEImRtdPb4Tpry3W3y4U+7u7Cwha7C+9gOpNAFYU3+mH/OUZYy/TovpdBrvXZ7lxwcEwo33CeJoaMKABVnXaZ3C43n/GjUXB9GvfDOdNF1UydnODE3JneGVM34gEK82X2SNG3Q7Y6PUHg9292NKJ3Rlc0b+P6Ar6BZUtDvteemhrUio07XlgzYSoAZvVTu4XVP0nzTl44oewrYPbid0Z1Nn3dvjXw90YPZDt9W+fuSZEtZER1ZTar9oeTwWY7LL1OXGcIPCxp6r9bHwZx2+p7L/8dQwXHj7188Ofu3zUHtoBqzWXbNz84eOw4Kjh28NXqRROWCDDoyeMeKmcQRgYKCgqCmEsKFBgkd3Kyg+e6wCQoKx9xYhACzxnp6h7ahQHO7zr/cNeWzUBJMDi06xA62HVIV5dxSSpDB6WrU4QYZhyxAI9IrIUMTwCzsimDECtDJ2MKIyM/LsAIAzhku6/M1XNjoHBtniBDV/7ZBTyQCWoOTp6lZ6ODgKEJDLclhMAlPFKXzvV744lRoh1nOsnwyPo9oEn9netWKmdrQiYEHfnU+EAIBKAUH7KAGp+aGpoAisrZ1FhzBMwewVPajUTt7OyqzQu6sGTlAQRCoHBSMQUCcO6AZRgmygAblVwnqCAIYwjRJTgAjNdLil1g3K4AAAAASUVORK5CYII=)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "onZXfhnkmexc" - }, - "source": [ - "# BigDL-Nano Hyperparameter Tuning (Tensorflow Sequential/Functional API) Quickstart\n", - "In this notebook we demonstrates how to use Nano HPO to tune the hyperparameters in tensorflow training. The model is built using either tensorflow keras sequential API or functional API.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "i8kjsyL4mexd" - }, - "source": [ - "## Step 0: Prepare Environment\n", - "You can install the latest pre-release version with nano support using below commands.\n", - "\n", - "We recommend to run below commands, especially `source bigdl-nano-init` before jupyter kernel is started, or some of the optimizations may not take effect.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "collapsed": true, - "id": "NC4pcIizmexe", - "outputId": "d266f123-ba84-422c-82c3-cb12c2768776" - }, - "outputs": [], - "source": [ - "# Install latest pre-release version of bigdl-nano\n", - "!pip install --pre bigdl-nano[tensorflow]\n", - "!pip install setuptools==58.0.4\n", - "!pip install protobuf==3.20.1\n", - "!source bigdl-nano-init" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "collapsed": true, - "id": "Y8mUaHScU61U", - "outputId": "5cb15a29-a5e4-4ffd-90ac-50ff8427ab87" - }, - "outputs": [], - "source": [ - "# Install other dependecies for Nano HPO\n", - "!pip install ConfigSpace\n", - "!pip install optuna" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "IswBKf39mexg" - }, - "source": [ - "## Step 1: Init Nano AutoML\n", - "We need to enable Nano HPO before we use it for tensorflow training." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "yc34x9dcmexg", - "outputId": "bba44274-e6d0-408a-bf9b-32d549fe1ea9" - }, - "outputs": [], - "source": [ - "import bigdl.nano.automl as automl\n", - "automl.hpo_config.enable_hpo_tf()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ZiolCDSmmexj" - }, - "source": [ - "## Step 2: Prepare data\n", - "We use MNIST dataset for demonstration." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "o7jWojYNmexk", - "outputId": "eb4cc68f-4d38-410d-b007-c507da30b97f" - }, - "outputs": [], - "source": [ - "from tensorflow import keras\n", - "\n", - "(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n", - "\n", - "CLASSES = 10\n", - "img_x, img_y = x_train.shape[1], x_train.shape[2]\n", - "input_shape = (img_x, img_y, 1)\n", - "x_train = x_train.reshape(-1, img_x, img_y,1).astype(\"float32\") / 255\n", - "x_test = x_test.reshape(-1, img_x, img_y,1).astype(\"float32\") / 255" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Pxvb3gKcmexk" - }, - "source": [ - "## Step 3: Build model and specify search spaces" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9liaVsdhBwDF" - }, - "source": [ - "We now create our model. \n", - "\n", - "Change the imports from tensorflow.keras to bigdl.nano as below, and you will be able to specify search spaces as you define the model. For how to specify search space, refer to [user doc](https://bigdl.readthedocs.io/en/latest/doc/Nano/QuickStart/hpo.html)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "Mv1R2_SWS0rn" - }, - "outputs": [], - "source": [ - "from bigdl.nano.automl.tf.keras import Sequential\n", - "from bigdl.nano.tf.keras.layers import Dense, Flatten, Conv2D\n", - "from bigdl.nano.tf.keras import Input\n", - "from bigdl.nano.automl.tf.keras import Model\n", - "import bigdl.nano.automl.hpo.space as space" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "YgXu2BvLBuAo" - }, - "source": [ - "Below two cells show how to define the model with search spaces using either sequential or functional API respectively. You can choose one of them to run." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "XgIpL38-mexl" - }, - "outputs": [], - "source": [ - "model = Sequential()\n", - "model.add(Conv2D(\n", - " filters=space.Categorical(32, 64),\n", - " kernel_size=space.Categorical(3, 5),\n", - " strides=space.Categorical(1, 2),\n", - " activation=space.Categorical(\"relu\", \"linear\"),\n", - " input_shape=input_shape))\n", - "model.add(Flatten())\n", - "model.add(Dense(CLASSES, activation=\"softmax\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "Gn95Gp2Rmexl" - }, - "outputs": [], - "source": [ - "inputs = Input(shape=(28,28,1))\n", - "x = Conv2D(\n", - " filters=space.Categorical(32, 64),\n", - " kernel_size=space.Categorical(3, 5),\n", - " strides=space.Categorical(1, 2),\n", - " activation=space.Categorical(\"relu\", \"linear\"),\n", - " input_shape=input_shape)(inputs)\n", - "x = Flatten()(x)\n", - "outputs = Dense(CLASSES, activation=\"softmax\")(x)\n", - "model = Model(inputs=inputs, outputs=outputs, name=\"mnist_model\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "O5SBRAawmexm" - }, - "source": [ - "## Step 4: Compile model\n", - "We now compile our model with loss function, optimizer and metrics. If you want to tune learning rate and batch size, refer to [user guide](https://bigdl.readthedocs.io/en/latest/doc/Nano/QuickStart/hpo.html#search-the-learning-rate)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "U9HXvfIlmexm" - }, - "outputs": [], - "source": [ - "from tensorflow.keras.optimizers import RMSprop\n", - "model.compile(\n", - " loss=\"sparse_categorical_crossentropy\",\n", - " optimizer=RMSprop(learning_rate=0.001),\n", - " metrics=[\"accuracy\"]\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "fW1b_B9Kmexn" - }, - "source": [ - "## Step 5: Run hyperparameter search\n", - "Run hyperparameter search by calling `model.search`. Set `n_trials` to the number of trialials you want to run, and set the `target_metric` and `direction` so that HPO optimizes the `target_metric` in the specified `direction`. Each trial will use a different set of hyperparameters in the search space range. After search completes, you can use `search_summary` to retrive the search results for analysis. For more details, refer to [user doc](https://bigdl.readthedocs.io/en/latest/doc/Nano/QuickStart/hpo.html)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "FKdwjvMJmexn", - "outputId": "9faa9254-c148-4a40-baef-3f39bf89e6e0", - "scrolled": true - }, - "outputs": [], - "source": [ - "%%time\n", - "from bigdl.nano.automl.hpo.backend import PrunerType\n", - "model.search(\n", - " n_trials=8,\n", - " target_metric='val_accuracy',\n", - " direction=\"maximize\",\n", - " pruner=PrunerType.HyperBand,\n", - " pruner_kwargs={'min_resource':1, 'max_resource':100, 'reduction_factor':3},\n", - " x=x_train,\n", - " y=y_train,\n", - " batch_size=128,\n", - " epochs=5,\n", - " validation_split=0.2,\n", - " verbose=False\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "MpFgoN4TwZHk", - "outputId": "e157fe1d-5628-4795-d3b9-40db199ae9b3" - }, - "outputs": [], - "source": [ - "print(model.search_summary())" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "xU_PnDXnmexo" - }, - "source": [ - "## Step 6: (Optional) Resume training from memory\n", - "You can resume the previous search when a search completes by setting `resume=True`. Refer to [user doc](https://bigdl.readthedocs.io/en/latest/doc/Nano/QuickStart/hpo.html) for more details." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "mvHyvEDomexo", - "outputId": "350caead-932c-4b69-a9b6-4592dc12da72" - }, - "outputs": [], - "source": [ - "%%time\n", - "model.search(\n", - " n_trials=4,\n", - " target_metric='val_accuracy',\n", - " direction=\"maximize\",\n", - " pruner=PrunerType.HyperBand,\n", - " pruner_kwargs={'min_resource':1, 'max_resource':100, 'reduction_factor':3},\n", - " x=x_train,\n", - " y=y_train,\n", - " batch_size=128,\n", - " epochs=5,\n", - " validation_split=0.2,\n", - " verbose=False,\n", - " resume = True\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "xQEDtjj1mexx", - "outputId": "11040b63-a3fc-4570-d2a9-02c681900cca" - }, - "outputs": [], - "source": [ - "print(model.search_summary())" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "uI05vzTgmexy" - }, - "source": [ - "## Step 7: fit with the best hyperparameters\n", - "After search, `model.fit` will autotmatically use the best hyperparmeters found in search to fit the model." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "jTJ8FGnAmexy", - "outputId": "86d9215e-b20f-45b7-c171-8c4b31fe466f", - "scrolled": true - }, - "outputs": [], - "source": [ - "history = model.fit(x_train, y_train,\n", - " batch_size=128, epochs=5, validation_split=0.2)\n", - "\n", - "test_scores = model.evaluate(x_test, y_test, verbose=2)\n", - "print(\"Test loss:\", test_scores[0])\n", - "print(\"Test accuracy:\", test_scores[1])" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "xYhKbOjYDzMv" - }, - "source": [ - "## Step 8: HPO Result Analysis and Visualization\n", - "Check out the summary of the model. The model has already been built with the best hyperparameters found by nano hpo." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "wQDqKt8Emexz", - "outputId": "7e33c852-cea8-4204-f2fa-f1f265759426", - "scrolled": true - }, - "outputs": [], - "source": [ - "print(model.summary())\n", - "study = model.search_summary()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 426 - }, - "id": "EZZZgiWeNfuN", - "outputId": "b99dade8-1838-4b19-c4c3-2afbffd801f5" - }, - "outputs": [], - "source": [ - "study.trials_dataframe(attrs=(\"number\", \"value\", \"params\", \"state\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 542 - }, - "id": "mukvET_7mexz", - "outputId": "b80a13a5-3cf0-40b0-c33c-b9b83de43c1a", - "scrolled": false - }, - "outputs": [], - "source": [ - "from bigdl.nano.automl.hpo.visualization import plot_optimization_history\n", - "plot_optimization_history(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 542 - }, - "id": "QdZhgElEmexz", - "outputId": "3da0abb3-bc1b-4412-8834-0e995ea27857" - }, - "outputs": [], - "source": [ - "from bigdl.nano.automl.hpo.visualization import plot_parallel_coordinate\n", - "plot_parallel_coordinate(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 542 - }, - "id": "rL8-6vGYmex0", - "outputId": "c7ac5ec6-2af9-4b9d-9815-e98b4466d732" - }, - "outputs": [], - "source": [ - "from bigdl.nano.automl.hpo.visualization import plot_intermediate_values\n", - "plot_intermediate_values(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 542 - }, - "id": "A5mqqXg8mex0", - "outputId": "325accdc-c371-48d1-d9e2-ce332759d9bb" - }, - "outputs": [], - "source": [ - "from bigdl.nano.automl.hpo.visualization import plot_contour\n", - "plot_contour(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 542 - }, - "id": "MUmsUyAfmex0", - "outputId": "ea74030c-309f-468f-80e6-718956a44cc2" - }, - "outputs": [], - "source": [ - "from bigdl.nano.automl.hpo.visualization import plot_param_importances\n", - "plot_param_importances(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 542 - }, - "id": "Y-oXajiTmex1", - "outputId": "e678b912-7946-41c1-e431-8e61e9a28dca", - "scrolled": true - }, - "outputs": [], - "source": [ - "plot_param_importances(study, target=lambda t: t.duration.total_seconds(), target_name=\"duration\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can find the running output from [here](https://github.com/intel-analytics/BigDL/blob/main/python/nano/notebooks/hpo/seq_and_func.ipynb), or run the notebook by yourself in [Google Colab](https://colab.research.google.com/github/intel-analytics/BigDL/blob/main/python/nano/notebooks/hpo/seq_and_func.ipynb)." - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "seq_and_func.ipynb", - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3.9.12 ('base': conda)", - "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.9.12" - }, - "vscode": { - "interpreter": { - "hash": "f6c4fac624a9bd3b1c7bcafb358e36fcd9daccaa962ba059d07bbc89607fe634" - } - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.nblink b/docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.nblink new file mode 100644 index 00000000..065776d1 --- /dev/null +++ b/docs/readthedocs/source/doc/Nano/Tutorials/seq_and_func.nblink @@ -0,0 +1,3 @@ +{ + "path": "../../../../../../python/nano/notebooks/hpo/seq_and_func.ipynb" +} \ No newline at end of file