{ "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/goM4ODqAgYSFhomnrK4jTnCDhYeAgYUCb744OTyChIc4ODsAcMI5OTsBccM5OTt/gYSChIeFh4paW15/gYOChIcDbrs5OTv///+AgYT+/v6IiYw6OjyBgoX9/f47Oz09PT99foF+f4KDhIc5OTw8PD88PD73+Pj9/v6Oj5KCg4Z/gIMBccOJio1/gYTq6us7Oz56e3719fU6Oj2AgYV8fYCAgoR4e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p6h7ahQHO7zr/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 }