{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 01. Basic Tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this tutorial, you can learn how to:\n", "\n", "* Define Search Space\n", "* Optimize Objective Function\n", "\n", "This tutorial describes how to optimize Hyperparameters using HyperOpt without having a mathematical understanding of any algorithm implemented in HyperOpt." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import HyperOpt Library\n", "from hyperopt import tpe, hp, fmin" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Declares a purpose function to optimize. In this tutorial, we will optimize a simple function called `objective`, which is a simple quadratic function.\n", "\n", "$$ y = (x-3)^2 + 2 $$" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "objective = lambda x: (x-3)**2 + 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's visualize this objective function." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3deXxU1f3/8dcnO1vCFiCsAdl3IYLiUhUXpAruxa24ldpqrV1d2m/d27rV2iruVLSKG9qqRQX3pYiGRbawBiKBkIQASci+nN8fGfzFNIEkM5M7mbyfj8c8MnPunbmf3Jm8c+fcc+815xwiIhJeIrwuQEREAk/hLiIShhTuIiJhSOEuIhKGFO4iImEoyusCALp37+6Sk5O9LkNEpFVZvnz5HudcYn3TQiLck5OTSU1N9boMEZFWxcwyGpp22G4ZM5tnZjlmtrZW20tmtsp3225mq3ztyWZWUmvaY4H5FUREpCkas+X+DPAw8OzBBufcDw7eN7MHgPxa8291zo0PVIEiItJ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O2pGOmwEzqjVvgjo7bs/iJrQ3wK8AsQGsdZngGvqtPUGFtWq5WvfbR013RPBXn/PAWuA1b4PV1LdunyPp1MzGmNrC9W1hZq+xVW+22N162qp9VXf7w7cQc0/HoA432dni++zNCjY68e33OOo+Uq+utZ6mg5cc/BzBlznWzdfU7NjekoL1FXv+1KnLgMe8a3TNdQa6Rbk2tpTE9YJtdpafH1R888lC6jw5ddV1OyneR/Y7PvZ1TdvCvBUrede6fusbQGuaM7ydfoBEZEw1Nq7ZUREpB4KdxGRMKRwFxEJQwp3EZEwpHAXEQlDCncRkTCkcBcRCUP/D6f06xK0Z9EJAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "x = np.linspace(-10, 10, 100)\n", "y = objective(x)\n", "\n", "fig = plt.figure()\n", "plt.plot(x, y)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are trying to optimize the objective function by changing the HyperParameter $x$. That's why we will declare a search space for $x$. The functions related to the search space are implemented in `hyperopt.hp`. The list is as follows.\n", "\n", "* `hp.randint(label, upper)` or `hp.randint(label, low, high)`\n", "* `hp.uniform(label, low, high)`\n", "* `hp.loguniform(label, low, high)` \n", "* `hp.normal(label, mu, sigma)`\n", "* `hp.lognormal(label, mu, sigma)`\n", "* `hp.quniform(label, low, high, q)`\n", "* `hp.qloguniform(label, low, high, q)`\n", "* `hp.qnormal(label, mu, sigma, q)`\n", "* `hp.qlognormal(label, mu, sigma, q)`\n", "* `hp.choice(label, list)`\n", "\n", "We will use the most basic `hp.uniform` in this tutorial.\n", " " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Define the search space of x between -10 and 10.\n", "space = hp.uniform('x', -10, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, there's only one last step left. So far, we have defined a function of purpose, and we have defined a search space for $x$. Now we can search through the search space $x$ and find the value of $x$ that can optimize the objective function. HyperOpt performs it using `fmin`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 1000/1000 [00:04<00:00, 228.56trial/s, best loss: 2.000001036046408]\n", "{'x': 3.0010178636491283}\n" ] } ], "source": [ "best = fmin(\n", " fn=objective, # Objective Function to optimize\n", " space=space, # Hyperparameter's Search Space\n", " algo=tpe.suggest, # Optimization algorithm\n", " max_evals=1000 # Number of optimization attempts\n", ")\n", "print(best)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The optimal $x$ value found by HyperOpt is approximately 3.0. This is very close to a solution of $y=(x-3)^2+2$." ] } ], "metadata": { "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }