{ "cells": [ { "cell_type": "markdown", "id": "demographic-bulletin", "metadata": {}, "source": [ "# Getting Started with hpopt" ] }, { "cell_type": "markdown", "id": "81460b40", "metadata": {}, "source": [ "This is quick start for hpoopt. You can learn how to use hpopt standalone with your own traninig module." ] }, { "cell_type": "markdown", "id": "duplicate-plane", "metadata": {}, "source": [ "## 1. Installation" ] }, { "cell_type": "markdown", "id": "2d6ef719", "metadata": {}, "source": [ "\n", "You only need to run following code to install hpopt.\n", "\n", "``` bash\n", "git clone https://github.com/openvinotoolkit/hyper_parameter_optimization\n", "pip install -v -e ./hyper_parameter_optimization\n", "```" ] }, { "cell_type": "markdown", "id": "22ebd406", "metadata": {}, "source": [ "## 2. Run HPO" ] }, { "cell_type": "markdown", "id": "aca6bb13", "metadata": {}, "source": [ "First of all, you need to import hpopt to run HPO." ] }, { "cell_type": "code", "execution_count": 1, "id": "4be4967c", "metadata": {}, "outputs": [], "source": [ "import hpopt" ] }, { "cell_type": "markdown", "id": "c1ce246b", "metadata": {}, "source": [ "Then, make you own models. In this code, I implement very simple black-box function which has two hyper parameters 'a' and 'b'." ] }, { "cell_type": "code", "execution_count": 3, "id": "430cd36c", "metadata": {}, "outputs": [], "source": [ "\n", "import random\n", "\n", "def my_model(a, b, iter):\n", " return a**2 + (b - 10)**2 + iter * 0.1 + random.random() * 0.1\n", "\n", "def my_trainer(config):\n", " for iter in range(config[\"iterations\"]):\n", " score = my_model(**config[\"params\"], iter=iter)\n", " if hpopt.report(config=config, score=score) == hpopt.Status.STOP:\n", " break" ] }, { "cell_type": "markdown", "id": "b354b760", "metadata": {}, "source": [ "You should define hyper parameter search space to run HPO. Hyper parameter search space is dictionary which has hyper parameter name as key and search space sampling option as value.\n", "Please refer [](./apis.md) for more details." ] }, { "cell_type": "code", "execution_count": 4, "id": "e7b54790", "metadata": {}, "outputs": [], "source": [ "# make hyper parameter space\n", "search_space = {\"a\": hpopt.search_space(\"uniform\", [-5, 10]),\n", " \"b\": hpopt.search_space(\"uniform\", [0, 15])}" ] }, { "cell_type": "markdown", "id": "f1acf21f", "metadata": {}, "source": [ "Let's initiate hpopt class with hyper parameter search space we made. In this case, we run simple bayesian optimization(SMBO)." ] }, { "cell_type": "code", "execution_count": 5, "id": "ahead-visibility", "metadata": {}, "outputs": [], "source": [ "my_hpo = hpopt.create(save_path='./tmp/my_hpo', # where HPO progress is saved\n", " search_alg=\"bayes_opt\", # HPO algorithm\n", " search_space=search_space,\n", " early_stop=\"median_stop\", # early stopping method\n", " num_init_trials=5, num_trials=10, max_iterations=2,\n", " resume=False,\n", " num_full_iterations=2, # train iteration\n", " full_dataset_size=1) # train dataset size. In this case, you can ignore it." ] }, { "cell_type": "markdown", "id": "61fcc5f8", "metadata": {}, "source": [ "Finally, we can run HPO and print output." ] }, { "cell_type": "code", "execution_count": 6, "id": "e54d2cf6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| # | a | b | score |\n", "| 1 | 9.629164679496109 | 9.152868582262837 | 93.56057080971651 |\n", "| 2 | 9.503930895864768 | 10.477054966570938 | 90.6920277461489 |\n", "| 3 | 6.5275823645277775 | 14.59670633381528 | 63.90848097562949 |\n", "| 4 | 2.8808311670306797 | 9.058790892076829 | 9.29099328793063 |\n", "| 5 | 7.95029121913541 | 13.178755341124605 | 73.50752311230228 |\n", "| 6 | 10.0 | 0.0 | 200.10740027553825 |\n", "| 7 | 6.025026543316109 | 0.0 | 136.4928912794813 |\n", "| 8 | 10.0 | 1.6328830252328062 | 170.14644018670234 |\n", "| 9 | -5.0 | 0.0 | 125.12112966688557 |\n", "| 10 | 9.571587105736812 |0.004128397224773539 | 191.68849629266415 |\n", "best hp: {'a': 10.0, 'b': 0.0}\n" ] } ], "source": [ "while True:\n", " config = my_hpo.get_next_sample()\n", "\n", " if config is None:\n", " break\n", "\n", " my_trainer(config)\n", "\n", "best_config = my_hpo.get_best_config()\n", "\n", "my_hpo.print_results()\n", "\n", "print(\"best hp: \", best_config)" ] }, { "cell_type": "markdown", "id": "fa943338", "metadata": {}, "source": [ "As you can see, hpopt found best hyper parameters for black box function." ] } ], "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.8.11" } }, "nbformat": 4, "nbformat_minor": 5 }