ray/doc/source/tune/examples/zoopt_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

391 lines
10 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": "a97c49a9",
"metadata": {},
"source": [
"# Running Tune experiments with ZOOpt\n",
"\n",
"In this tutorial we introduce ZOOpt, while running a simple Ray Tune experiment. Tunes Search Algorithms integrate with ZOOpt and, as a result, allow you to seamlessly scale up a ZOOpt optimization process - without sacrificing performance.\n",
"\n",
"Zeroth-order optimization (ZOOpt) does not rely on the gradient of the objective function, but instead, learns from samples of the search space. It is suitable for optimizing functions that are nondifferentiable, with many local minima, or even unknown but only testable. Therefore, zeroth-order optimization is commonly referred to as \"derivative-free optimization\" and \"black-box optimization\". In this example we minimize a simple objective to briefly demonstrate the usage of ZOOpt with Ray Tune via `ZOOptSearch`. 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 `zoopt==0.4.1` library is installed. To learn more, please refer to the [ZOOpt website](https://github.com/polixir/ZOOpt)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58fee596",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"# !pip install ray[tune]\n",
"!pip install zoopt==0.4.1"
]
},
{
"cell_type": "markdown",
"id": "a9c8a34b",
"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": "d05017fe",
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"import time\n",
"\n",
"import ray\n",
"from ray import tune\n",
"from ray.tune.suggest.zoopt import ZOOptSearch\n",
"from zoopt import ValueType"
]
},
{
"cell_type": "markdown",
"id": "5a41255a",
"metadata": {},
"source": [
"Let's start by defining a simple evaluation function.\n",
"We artificially sleep for a bit (`0.1` seconds) to simulate a long-running ML experiment.\n",
"This setup assumes that we're running multiple `step`s of an experiment and try to tune two hyperparameters,\n",
"namely `width` and `height`, and `activation`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61db0806",
"metadata": {},
"outputs": [],
"source": [
"def evaluate(step, width, height):\n",
" time.sleep(0.1)\n",
" return (0.1 + width * step / 100) ** (-1) + height * 0.1"
]
},
{
"cell_type": "markdown",
"id": "a979f791",
"metadata": {},
"source": [
"Next, our ``objective`` function 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": "3b451a2c",
"metadata": {},
"outputs": [],
"source": [
"def objective(config):\n",
" for step in range(config[\"steps\"]):\n",
" score = evaluate(step, config[\"width\"], config[\"height\"])\n",
" tune.report(iterations=step, mean_loss=score)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad4f9faf",
"metadata": {
"lines_to_next_cell": 0,
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"ray.init(configure_logging=False)"
]
},
{
"cell_type": "markdown",
"id": "036e0085",
"metadata": {},
"source": [
"Next we define a search space. The critical assumption is that the optimal hyperparameters 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": "b28469ce",
"metadata": {},
"outputs": [],
"source": [
"search_config = {\n",
" \"steps\": 100,\n",
" \"width\": tune.randint(0, 10),\n",
" \"height\": tune.quniform(-10, 10, 1e-2),\n",
" \"activation\": tune.choice([\"relu, tanh\"])\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "599b1ece",
"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": "94fcbc63",
"metadata": {},
"outputs": [],
"source": [
"num_samples = 1000"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33f11052",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"# If 1000 samples take too long, you can reduce this number.\n",
"# We override this number here for our smoke tests.\n",
"num_samples = 10"
]
},
{
"cell_type": "markdown",
"id": "c5b47448",
"metadata": {},
"source": [
"Next we define the search algorithm built from `ZOOptSearch`, constrained to a maximum of `8` concurrent trials via ZOOpt's internal `\"parallel_num\"`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22c15bc5",
"metadata": {},
"outputs": [],
"source": [
"zoopt_config = {\n",
" \"parallel_num\": 8\n",
"}\n",
"algo = ZOOptSearch(\n",
" algo=\"Asracos\", # only supports ASRacos currently\n",
" budget=num_samples,\n",
" **zoopt_config,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "f5147bec",
"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": "b51e7a31",
"metadata": {},
"outputs": [],
"source": [
"analysis = tune.run(\n",
" objective,\n",
" search_alg=algo,\n",
" metric=\"mean_loss\",\n",
" mode=\"min\",\n",
" name=\"zoopt_exp\",\n",
" num_samples=num_samples,\n",
" config=search_config\n",
")"
]
},
{
"cell_type": "markdown",
"id": "8b1080a3",
"metadata": {},
"source": [
"Here are the hyperparamters found to minimize the mean loss of the defined objective."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2fb0f9c",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [],
"source": [
"print(\"Best hyperparameters found were: \", analysis.best_config)"
]
},
{
"cell_type": "markdown",
"id": "48d0547d",
"metadata": {},
"source": [
"## Optional: passing the parameter space into the search algorithm\n",
"\n",
"We can also pass the parameter space ourselves in the following formats: \n",
"- continuous dimensions: (continuous, search_range, precision)\n",
"- discrete dimensions: (discrete, search_range, has_order)\n",
"- grid dimensions: (grid, grid_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00763339",
"metadata": {},
"outputs": [],
"source": [
"space = {\n",
" \"height\": (ValueType.CONTINUOUS, [-10, 10], 1e-2),\n",
" \"width\": (ValueType.DISCRETE, [0, 10], True),\n",
" \"layers\": (ValueType.GRID, [4, 8, 16])\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "54ecd17b",
"metadata": {},
"source": [
"ZOOpt again handles constraining the amount of concurrent trials with `\"parallel_num\"`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ea6d7be",
"metadata": {},
"outputs": [],
"source": [
"zoopt_search_config = {\n",
" \"parallel_num\": 8,\n",
" \"metric\": \"mean_loss\",\n",
" \"mode\": \"min\"\n",
"}\n",
"algo = ZOOptSearch(\n",
" algo=\"Asracos\",\n",
" budget=num_samples,\n",
" dim_dict=space,\n",
" **zoopt_search_config\n",
")"
]
},
{
"cell_type": "markdown",
"id": "b0b0be8b",
"metadata": {},
"source": [
"This time we pass only `\"steps\"` and `\"activation\"` to the Tune `config` because `\"height\"` and `\"width\"` have been passed into `ZOOptSearch` to create the `search_algo`. \n",
"Again, we run the experiment to `\"min\"`imize the \"mean_loss\" of the `objective` by searching `search_config` via `algo`, `num_samples` times."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7995ede5",
"metadata": {},
"outputs": [],
"source": [
"analysis = tune.run(\n",
" objective,\n",
" search_alg=algo,\n",
" metric=\"mean_loss\",\n",
" mode=\"min\",\n",
" name=\"zoopt_exp\",\n",
" num_samples=num_samples,\n",
" config={\n",
" \"steps\": 100,\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "01e3b036",
"metadata": {},
"source": [
"Here are the hyperparamters found to minimize the mean loss of the defined objective."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c11178dd",
"metadata": {},
"outputs": [],
"source": [
"print(\"Best hyperparameters found were: \", analysis.best_config)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23eab59f",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"ray.shutdown()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8cd01164",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5fb2c1b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"orphan": true
},
"nbformat": 4,
"nbformat_minor": 5
}