{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "TsHV-7cpVkyK" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "atWM-s8yVnfX" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "TB0wBWfcVqHz" }, "source": [ "# Hyperparameter Tuning with the HParams Dashboard\n", "\n", "\n", " \n", " \n", " \n", "
\n", " View on TensorFlow.org\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "elH58gbhWAmn" }, "source": [ "When building machine learning models, you need to choose various [hyperparameters](https://en.wikipedia.org/wiki/Hyperparameter_(machine_learning)), such as the dropout rate in a layer or the learning rate. These decisions impact model metrics, such as accuracy. Therefore, an important step in the machine learning workflow is to identify the best hyperparameters for your problem, which often involves experimentation. This process is known as \"Hyperparameter Optimization\" or \"Hyperparameter Tuning\".\n", "\n", "The HParams dashboard in TensorBoard provides several tools to help with this process of identifying the best experiment or most promising sets of hyperparameters. \n", "\n", "This tutorial will focus on the following steps:\n", "\n", "1. Experiment setup and HParams summary\n", "2. Adapt TensorFlow runs to log hyperparameters and metrics\n", "3. Start runs and log them all under one parent directory\n", "4. Visualize the results in TensorBoard's HParams dashboard\n", "\n", "Note: The HParams summary APIs and dashboard UI are in a preview stage and will change over time. \n", "\n", "Start by installing TF 2.0 and loading the TensorBoard notebook extension:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "8p3Tbx8cWEFA" }, "outputs": [], "source": [ "# Load the TensorBoard notebook extension\n", "%load_ext tensorboard" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "lEWCCQYkWIdA" }, "outputs": [], "source": [ "# Clear any logs from previous runs\n", "!rm -rf ./logs/ " ] }, { "cell_type": "markdown", "metadata": { "id": "9GtR_cTTkf9G" }, "source": [ "Import TensorFlow and the TensorBoard HParams plugin:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "mVtYvbbIWRkV" }, "outputs": [], "source": [ "import tensorflow as tf\n", "from tensorboard.plugins.hparams import api as hp" ] }, { "cell_type": "markdown", "metadata": { "id": "XfCa27_8kov6" }, "source": [ "Download the [FashionMNIST](https://github.com/zalandoresearch/fashion-mnist) dataset and scale it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "z8b82G7YksOS" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n", "32768/29515 [=================================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n", "26427392/26421880 [==============================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n", "8192/5148 [===============================================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n", "4423680/4422102 [==============================] - 0s 0us/step\n" ] } ], "source": [ "fashion_mnist = tf.keras.datasets.fashion_mnist\n", "\n", "(x_train, y_train),(x_test, y_test) = fashion_mnist.load_data()\n", "x_train, x_test = x_train / 255.0, x_test / 255.0" ] }, { "cell_type": "markdown", "metadata": { "id": "0tsTxO85WaYq" }, "source": [ "## 1. Experiment setup and the HParams experiment summary\n", "\n", "Experiment with three hyperparameters in the model:\n", "\n", "1. Number of units in the first dense layer\n", "2. Dropout rate in the dropout layer\n", "3. Optimizer\n", "\n", "List the values to try, and log an experiment configuration to TensorBoard. This step is optional: you can provide domain information to enable more precise filtering of hyperparameters in the UI, and you can specify which metrics should be displayed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "5Euw0agpWb4V" }, "outputs": [], "source": [ "HP_NUM_UNITS = hp.HParam('num_units', hp.Discrete([16, 32]))\n", "HP_DROPOUT = hp.HParam('dropout', hp.RealInterval(0.1, 0.2))\n", "HP_OPTIMIZER = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd']))\n", "\n", "METRIC_ACCURACY = 'accuracy'\n", "\n", "with tf.summary.create_file_writer('logs/hparam_tuning').as_default():\n", " hp.hparams_config(\n", " hparams=[HP_NUM_UNITS, HP_DROPOUT, HP_OPTIMIZER],\n", " metrics=[hp.Metric(METRIC_ACCURACY, display_name='Accuracy')],\n", " )" ] }, { "cell_type": "markdown", "metadata": { "id": "T_T95RrSIVO6" }, "source": [ "If you choose to skip this step, you can use a string literal wherever you would otherwise use an `HParam` value: e.g., `hparams['dropout']` instead of `hparams[HP_DROPOUT]`." ] }, { "cell_type": "markdown", "metadata": { "id": "va9XMh-dW4_f" }, "source": [ "## 2. Adapt TensorFlow runs to log hyperparameters and metrics\n", "\n", "The model will be quite simple: two dense layers with a dropout layer between them. The training code will look familiar, although the hyperparameters are no longer hardcoded. Instead, the hyperparameters are provided in an `hparams` dictionary and used throughout the training function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hG-zalNfW5Zl" }, "outputs": [], "source": [ "def train_test_model(hparams):\n", " model = tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(hparams[HP_NUM_UNITS], activation=tf.nn.relu),\n", " tf.keras.layers.Dropout(hparams[HP_DROPOUT]),\n", " tf.keras.layers.Dense(10, activation=tf.nn.softmax),\n", " ])\n", " model.compile(\n", " optimizer=hparams[HP_OPTIMIZER],\n", " loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'],\n", " )\n", "\n", " model.fit(x_train, y_train, epochs=1) # Run with 1 epoch to speed things up for demo purposes\n", " _, accuracy = model.evaluate(x_test, y_test)\n", " return accuracy" ] }, { "cell_type": "markdown", "metadata": { "id": "46oJF8seXM7v" }, "source": [ "For each run, log an hparams summary with the hyperparameters and final accuracy:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "8j-fO6nEXRfW" }, "outputs": [], "source": [ "def run(run_dir, hparams):\n", " with tf.summary.create_file_writer(run_dir).as_default():\n", " hp.hparams(hparams) # record the values used in this trial\n", " accuracy = train_test_model(hparams)\n", " tf.summary.scalar(METRIC_ACCURACY, accuracy, step=1)" ] }, { "cell_type": "markdown", "metadata": { "id": "2mYdW0VKLbFE" }, "source": [ "When training Keras models, you can use callbacks instead of writing these directly:\n", "\n", "```python\n", "model.fit(\n", " ...,\n", " callbacks=[\n", " tf.keras.callbacks.TensorBoard(logdir), # log metrics\n", " hp.KerasCallback(logdir, hparams), # log hparams\n", " ],\n", ")\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "u2nOgIKAXdcb" }, "source": [ "## 3. Start runs and log them all under one parent directory\n", "\n", "You can now try multiple experiments, training each one with a different set of hyperparameters. \n", "\n", "For simplicity, use a grid search: try all combinations of the discrete parameters and just the lower and upper bounds of the real-valued parameter. For more complex scenarios, it might be more effective to choose each hyperparameter value randomly (this is called a random search). There are more advanced methods that can be used.\n", "\n", "Run a few experiments, which will take a few minutes:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "KbqT5n-AXd0h" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Starting trial: run-0\n", "{'num_units': 16, 'dropout': 0.1, 'optimizer': 'adam'}\n", "60000/60000 [==============================] - 4s 62us/sample - loss: 0.6872 - accuracy: 0.7564\n", "10000/10000 [==============================] - 0s 35us/sample - loss: 0.4806 - accuracy: 0.8321\n", "--- Starting trial: run-1\n", "{'num_units': 16, 'dropout': 0.1, 'optimizer': 'sgd'}\n", "60000/60000 [==============================] - 3s 54us/sample - loss: 0.9428 - accuracy: 0.6769\n", "10000/10000 [==============================] - 0s 36us/sample - loss: 0.6519 - accuracy: 0.7770\n", "--- Starting trial: run-2\n", "{'num_units': 16, 'dropout': 0.2, 'optimizer': 'adam'}\n", "60000/60000 [==============================] - 4s 60us/sample - loss: 0.8158 - accuracy: 0.7078\n", "10000/10000 [==============================] - 0s 36us/sample - loss: 0.5309 - accuracy: 0.8154\n", "--- Starting trial: run-3\n", "{'num_units': 16, 'dropout': 0.2, 'optimizer': 'sgd'}\n", "60000/60000 [==============================] - 3s 50us/sample - loss: 1.1465 - accuracy: 0.6019\n", "10000/10000 [==============================] - 0s 36us/sample - loss: 0.7007 - accuracy: 0.7683\n", "--- Starting trial: run-4\n", "{'num_units': 32, 'dropout': 0.1, 'optimizer': 'adam'}\n", "60000/60000 [==============================] - 4s 65us/sample - loss: 0.6178 - accuracy: 0.7849\n", "10000/10000 [==============================] - 0s 38us/sample - loss: 0.4645 - accuracy: 0.8395\n", "--- Starting trial: run-5\n", "{'num_units': 32, 'dropout': 0.1, 'optimizer': 'sgd'}\n", "60000/60000 [==============================] - 3s 55us/sample - loss: 0.8989 - accuracy: 0.6896\n", "10000/10000 [==============================] - 0s 37us/sample - loss: 0.6335 - accuracy: 0.7853\n", "--- Starting trial: run-6\n", "{'num_units': 32, 'dropout': 0.2, 'optimizer': 'adam'}\n", "60000/60000 [==============================] - 4s 64us/sample - loss: 0.6404 - accuracy: 0.7782\n", "10000/10000 [==============================] - 0s 37us/sample - loss: 0.4802 - accuracy: 0.8265\n", "--- Starting trial: run-7\n", "{'num_units': 32, 'dropout': 0.2, 'optimizer': 'sgd'}\n", "60000/60000 [==============================] - 3s 54us/sample - loss: 0.9633 - accuracy: 0.6703\n", "10000/10000 [==============================] - 0s 36us/sample - loss: 0.6516 - accuracy: 0.7755\n" ] } ], "source": [ "session_num = 0\n", "\n", "for num_units in HP_NUM_UNITS.domain.values:\n", " for dropout_rate in (HP_DROPOUT.domain.min_value, HP_DROPOUT.domain.max_value):\n", " for optimizer in HP_OPTIMIZER.domain.values:\n", " hparams = {\n", " HP_NUM_UNITS: num_units,\n", " HP_DROPOUT: dropout_rate,\n", " HP_OPTIMIZER: optimizer,\n", " }\n", " run_name = \"run-%d\" % session_num\n", " print('--- Starting trial: %s' % run_name)\n", " print({h.name: hparams[h] for h in hparams})\n", " run('logs/hparam_tuning/' + run_name, hparams)\n", " session_num += 1\n" ] }, { "cell_type": "markdown", "metadata": { "id": "qSyjWQ3mPKT9" }, "source": [ "## 4. Visualize the results in TensorBoard's HParams plugin" ] }, { "cell_type": "markdown", "metadata": { "id": "5VBvplwyP8Vy" }, "source": [ "The HParams dashboard can now be opened. Start TensorBoard and click on \"HParams\" at the top." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Xf4KM-U2bbP_" }, "outputs": [], "source": [ "%tensorboard --logdir logs/hparam_tuning" ] }, { "cell_type": "markdown", "metadata": { "id": "UTWg9nXnxWWI" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "4RPGbR9EWd4o" }, "source": [ "The left pane of the dashboard provides filtering capabilities that are active across all the views in the HParams dashboard:\n", "\n", "- Filter which hyperparameters/metrics are shown in the dashboard\n", "- Filter which hyperparameter/metrics values are shown in the dashboard\n", "- Filter on run status (running, success, ...)\n", "- Sort by hyperparameter/metric in the table view\n", "- Number of session groups to show (useful for performance when there are many experiments)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Z3Q5v28XaCt8" }, "source": [ "The HParams dashboard has three different views, with various useful information:\n", "\n", "* The **Table View** lists the runs, their hyperparameters, and their metrics.\n", "* The **Parallel Coordinates View** shows each run as a line going through an axis for each hyperparemeter and metric. Click and drag the mouse on any axis to mark a region which will highlight only the runs that pass through it. This can be useful for identifying which groups of hyperparameters are most important. The axes themselves can be re-ordered by dragging them.\n", "* The **Scatter Plot View** shows plots comparing each hyperparameter/metric with each metric. This can help identify correlations. Click and drag to select a region in a specific plot and highlight those sessions across the other plots. \n", "\n", "A table row, a parallel coordinates line, and a scatter plot market can be clicked to see a plot of the metrics as a function of training steps for that session (although in this tutorial only one step is used for each run)." ] }, { "cell_type": "markdown", "metadata": { "id": "fh3p0DtcdOK1" }, "source": [ "To further explore the capabilities of the HParams dashboard, download a set of pregenerated logs with more experiments:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "oxrSUAnCeFmQ" }, "outputs": [], "source": [ "%%bash\n", "wget -q 'https://storage.googleapis.com/download.tensorflow.org/tensorboard/hparams_demo_logs.zip'\n", "unzip -q hparams_demo_logs.zip -d logs/hparam_demo" ] }, { "cell_type": "markdown", "metadata": { "id": "__8xQhjqgr3D" }, "source": [ "View these logs in TensorBoard:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "KBHp6M_zgjp4" }, "outputs": [], "source": [ "%tensorboard --logdir logs/hparam_demo" ] }, { "cell_type": "markdown", "metadata": { "id": "Po7rTfQswAMT" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "IlDz2oXBgnZ9" }, "source": [ "You can try out the different views in the HParams dashboard. \n", "\n", "For example, by going to the parallel coordinates view and clicking and dragging on the accuracy axis, you can select the runs with the highest accuracy. As these runs pass through 'adam' in the optimizer axis, you can conclude that 'adam' performed better than 'sgd' on these experiments." ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "hyperparameter_tuning_with_hparams.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }