ray/doc/source/tune/examples/skopt_example.ipynb
Brett Göhre 9e0a59d94a
[docs] search algorithm notebook examples (#23924)
Co-authored-by: brettskymind <brett@pathmind.com>
Co-authored-by: Max Pumperla <max.pumperla@googlemail.com>
2022-04-25 11:10:58 -07:00

356 lines
9.7 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "61966d26",
"metadata": {},
"source": [
"# Running Tune experiments with Skopt\n",
"\n",
"In this tutorial we introduce Skopt, while running a simple Ray Tune experiment. Tunes Search Algorithms integrate with Skopt and, as a result, allow you to seamlessly scale up a Skopt optimization process - without sacrificing performance.\n",
"\n",
"Scikit-Optimize, or skopt, is a simple and efficient library to optimize expensive and noisy black-box functions, e.g. large-scale ML experiments. It implements several methods for sequential model-based optimization. Noteably, skopt does not perform gradient-based optimization, and instead uses computationally cheap surrogate models to\n",
"approximate the expensive function. In this example we minimize a simple objective to briefly demonstrate the usage of Skopt with Ray Tune via `SkOptSearch`. It's useful to keep in mind that despite the emphasis on machine learning experiments, Ray Tune optimizes any implicit or explicit objective. Here we assume `scikit-opitmize==0.8.1` library is installed. To learn more, please refer to the [Scikit-Optimize website](https://scikit-optimize.github.io).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d3b7ff9",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"# !pip install ray[tune]\n",
"!pip install scikit-optimize==0.8.1\n",
"!pip install sklearn==0.18.2"
]
},
{
"cell_type": "markdown",
"id": "a730341c",
"metadata": {},
"source": [
"Click below to see all the imports we need for this example.\n",
"You can also launch directly into a Binder instance to run this notebook yourself.\n",
"Just click on the rocket symbol at the top of the navigation."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1591bb7e",
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"import time\n",
"from typing import Dict, Optional, Any\n",
"\n",
"import ray\n",
"\n",
"import skopt\n",
"from ray import tune\n",
"from ray.tune.suggest import ConcurrencyLimiter\n",
"from ray.tune.suggest.skopt import SkOptSearch"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f538eecb",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"ray.init(configure_logging=False)"
]
},
{
"cell_type": "markdown",
"id": "c18346e5",
"metadata": {},
"source": [
"Let's start by defining a simple evaluation function. Again, an explicit math formula is queried here for demonstration, yet in practice this is typically a black-box function-- e.g. the performance results after training an ML model. We artificially sleep for a bit (`0.1` seconds) to simulate a long-running ML experiment. This setup assumes that we're running multiple `step`s of an experiment while tuning three hyperparameters, namely `width`, `height`, and `activation`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cb44451",
"metadata": {},
"outputs": [],
"source": [
"def evaluate(step, width, height, activation):\n",
" time.sleep(0.1)\n",
" activation_boost = 10 if activation==\"relu\" else 0\n",
" return (0.1 + width * step / 100) ** (-1) + height * 0.1 + activation_boost"
]
},
{
"cell_type": "markdown",
"id": "9ac14837",
"metadata": {},
"source": [
"Next, our `objective` function to be optimized takes a Tune `config`, evaluates the `score` of your experiment in a training loop,\n",
"and uses `tune.report` to report the `score` back to Tune."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4b27c17",
"metadata": {},
"outputs": [],
"source": [
"def objective(config):\n",
" for step in range(config[\"steps\"]):\n",
" score = evaluate(step, config[\"width\"], config[\"height\"], config[\"activation\"])\n",
" tune.report(iterations=step, mean_loss=score)"
]
},
{
"cell_type": "markdown",
"id": "2b48209c",
"metadata": {},
"source": [
"Next we define a search space. The critical assumption is that the optimal hyperparamters live within this space. Yet, if the space is very large, then those hyperparameters may be difficult to find in a short amount of time."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1cf2e16",
"metadata": {},
"outputs": [],
"source": [
"search_space = {\n",
" \"steps\": 100,\n",
" \"width\": tune.uniform(0, 20),\n",
" \"height\": tune.uniform(-100, 100),\n",
" \"activation\": tune.choice([\"relu\", \"tanh\"]),\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "2892b243",
"metadata": {},
"source": [
"The search algorithm is instantiated from the `SkOptSearch` class. We also constrain the the number of concurrent trials to `4` with a `ConcurrencyLimiter`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b560697",
"metadata": {},
"outputs": [],
"source": [
"algo = SkOptSearch()\n",
"algo = ConcurrencyLimiter(algo, max_concurrent=4)\n"
]
},
{
"cell_type": "markdown",
"id": "573a6c97",
"metadata": {},
"source": [
"The number of samples is the number of hyperparameter combinations that will be tried out. This Tune run is set to `1000` samples.\n",
"(you can decrease this if it takes too long on your machine)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bebc40db",
"metadata": {},
"outputs": [],
"source": [
"num_samples = 1000"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a86c5a19",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"# We override here for our smoke tests.\n",
"num_samples = 10"
]
},
{
"cell_type": "markdown",
"id": "35c5823b",
"metadata": {},
"source": [
"Finally, we run the experiment to `\"min\"`imize the \"mean_loss\" of the `objective` by searching `search_config` via `algo`, `num_samples` times. This previous sentence is fully characterizes the search problem we aim to solve. With this in mind, notice how efficient it is to execute `tune.run()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c86c6919",
"metadata": {},
"outputs": [],
"source": [
"analysis = tune.run(\n",
" objective,\n",
" search_alg=algo,\n",
" metric=\"mean_loss\",\n",
" mode=\"min\",\n",
" name=\"skopt_exp\",\n",
" num_samples=num_samples,\n",
" config=search_space\n",
")"
]
},
{
"cell_type": "markdown",
"id": "5f387c95",
"metadata": {},
"source": [
"We now have hyperparameters found to minimize the mean loss."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19fff170",
"metadata": {},
"outputs": [],
"source": [
"print(\"Best hyperparameters found were: \", analysis.best_config)"
]
},
{
"cell_type": "markdown",
"id": "7e7f43bb",
"metadata": {},
"source": [
"## Providing an initial set of hyperparameters\n",
"\n",
"While defining the search algorithm, we may choose to provide an initial set of hyperparameters that we believe are especially promising or informative, and\n",
"pass this information as a helpful starting point for the `SkOptSearch` object. We also can pass the known rewards for these initial params to save on unnecessary computation."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c44a3ad2",
"metadata": {},
"outputs": [],
"source": [
"initial_params = [\n",
" {\"width\": 10, \"height\": 0, \"activation\": \"relu\"},\n",
" {\"width\": 15, \"height\": -20, \"activation\": \"tanh\"}\n",
"]\n",
"known_rewards = [-189, -1144]"
]
},
{
"cell_type": "markdown",
"id": "30e19a4b",
"metadata": {},
"source": [
"Now the `search_alg` built using `SkOptSearch` takes `points_to_evaluate`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19308593",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [],
"source": [
"algo = SkOptSearch(points_to_evaluate=initial_params)\n",
"algo = ConcurrencyLimiter(algo, max_concurrent=4)"
]
},
{
"cell_type": "markdown",
"id": "bd022018",
"metadata": {},
"source": [
"And again run the experiment, this time with initial hyperparameter evaluations:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9c7c8d4",
"metadata": {},
"outputs": [],
"source": [
"analysis = tune.run(\n",
" objective,\n",
" search_alg=algo,\n",
" metric=\"mean_loss\",\n",
" mode=\"min\",\n",
" name=\"skopt_exp_with_warmstart\",\n",
" num_samples=num_samples,\n",
" config=search_space\n",
")"
]
},
{
"cell_type": "markdown",
"id": "04c9d550",
"metadata": {},
"source": [
"And we again show the ideal hyperparameters."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d7267095",
"metadata": {},
"outputs": [],
"source": [
"print(\"Best hyperparameters found were: \", analysis.best_config)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23739237",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"ray.shutdown()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"orphan": true
},
"nbformat": 4,
"nbformat_minor": 5
}