Ax integrates easily with different scheduling frameworks and distributed training frameworks. In this example, Ax-driven optimization is executed in a distributed fashion using RayTune.
RayTune is a scalable framework for hyperparameter tuning that provides many state-of-the-art hyperparameter tuning algorithms and seamlessly scales from laptop to distributed cluster with fault tolerance. RayTune leverages Ray's Actor API to provide asynchronous parallel and distributed execution.
Ray 'Actors' are a simple and clean abstraction for replicating your Python classes across multiple workers and nodes. Each hyperparameter evaluation is asynchronously executed on a separate Ray actor and reports intermediate training progress back to RayTune. Upon reporting, RayTune then uses this information to performs actions such as early termination, re-prioritization, or checkpointing.
import logging
from ray import tune
from ray.tune import track
from ray.tune.suggest.ax import AxSearch
logger = logging.getLogger(tune.__name__)
logger.setLevel(level=logging.CRITICAL) # Reduce the number of Ray warnings that are not relevant here.
import torch
import numpy as np
from ax.plot.contour import plot_contour
from ax.plot.trace import optimization_trace_single_method
from ax.service.ax_client import AxClient
from ax.utils.notebook.plotting import render, init_notebook_plotting
from ax.utils.tutorials.cnn_utils import CNN, load_mnist, train, evaluate
init_notebook_plotting()
[INFO 04-17 18:35:34] ipy_plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
We specify enforce_sequential_optimization
as False, because Ray runs many trials in parallel. With the sequential optimization enforcement, AxClient
would expect the first few trials to be completed with data before generating more trials.
When high parallelism is not required, it is best to enforce sequential optimization, as it allows for achieving optimal results in fewer (but sequential) trials. In cases where parallelism is important, such as with distributed training using Ray, we choose to forego minimizing resource utilization and run more trials in parallel.
ax = AxClient(enforce_sequential_optimization=False)
[INFO 04-17 18:35:34] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 2 decimal points.
Here we set up the search space and specify the objective; refer to the Ax API tutorials for more detail.
ax.create_experiment(
name="mnist_experiment",
parameters=[
{"name": "lr", "type": "range", "bounds": [1e-6, 0.4], "log_scale": True},
{"name": "momentum", "type": "range", "bounds": [0.0, 1.0]},
],
objective_name="mean_accuracy",
)
[INFO 04-17 18:35:34] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 5 trials, GPEI for subsequent trials]). Iterations after 5 will take longer to generate due to model-fitting.
Since we use the Ax Service API here, we evaluate the parameterizations that Ax suggests, using RayTune. The evaluation function follows its usual pattern, taking in a parameterization and outputting an objective value. For detail on evaluation functions, see Trial Evaluation.
def train_evaluate(parameterization):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_loader, valid_loader, test_loader = load_mnist(data_path='~/.data')
net = train(net=CNN(), train_loader=train_loader, parameters=parameterization, dtype=torch.float, device=device)
track.log(
mean_accuracy=evaluate(
net=net,
data_loader=valid_loader,
dtype=torch.float,
device=device,
)
)
Execute the Ax optimization and trial evaluation in RayTune using AxSearch algorithm:
tune.run(
train_evaluate,
num_samples=30,
search_alg=AxSearch(ax), # Note that the argument here is the `AxClient`.
verbose=0, # Set this level to 1 to see status updates and to 2 to also see trial results.
# To use GPU, specify: resources_per_trial={"gpu": 1}.
)
2020-04-17 18:35:34,931 INFO resource_spec.py:212 -- Starting Ray with 4.35 GiB memory available for workers and up to 2.18 GiB for objects. You can adjust these settings with ray.init(memory=<bytes>, object_store_memory=<bytes>).
2020-04-17 18:35:35,279 INFO services.py:1148 -- View the Ray dashboard at localhost:8265
[INFO 04-17 18:35:35] ax.service.ax_client: Generated new trial 0 with parameters {'lr': 0.06, 'momentum': 0.37}.
[INFO 04-17 18:35:35] ax.service.ax_client: Generated new trial 1 with parameters {'lr': 0.0, 'momentum': 0.48}.
[INFO 04-17 18:35:35] ax.service.ax_client: Generated new trial 2 with parameters {'lr': 0.0, 'momentum': 0.41}.
[INFO 04-17 18:35:35] ax.service.ax_client: Generated new trial 3 with parameters {'lr': 0.0, 'momentum': 0.37}.
[INFO 04-17 18:35:35] ax.service.ax_client: Generated new trial 4 with parameters {'lr': 0.0, 'momentum': 0.57}.
[INFO 04-17 18:35:36] ax.service.ax_client: Generated new trial 5 with parameters {'lr': 0.0, 'momentum': 0.76}.
[INFO 04-17 18:35:36] ax.service.ax_client: Generated new trial 6 with parameters {'lr': 0.0, 'momentum': 0.31}.
[INFO 04-17 18:35:36] ax.service.ax_client: Generated new trial 7 with parameters {'lr': 0.0, 'momentum': 0.76}.
[INFO 04-17 18:35:36] ax.service.ax_client: Generated new trial 8 with parameters {'lr': 0.06, 'momentum': 0.67}.
[INFO 04-17 18:35:36] ax.service.ax_client: Generated new trial 9 with parameters {'lr': 0.0, 'momentum': 0.93}.
(pid=4021) Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw/train-images-idx3-ubyte.gz (pid=4021) 2020-04-17 18:35:39,705 INFO trainable.py:217 -- Getting current IP. (pid=4020) 2020-04-17 18:35:39,720 INFO trainable.py:217 -- Getting current IP. (pid=4020) Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw/train-images-idx3-ubyte.gz 0.4% 0.1% 8.3% 8.1% 55.5% 44.5% 80.7% 72.1% 84.7% 73.6% 75.4% 100.1% 100.1% (pid=4021) Extracting /home/travis/.data/MNIST/raw/train-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw (pid=4020) Extracting /home/travis/.data/MNIST/raw/train-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw (pid=4021) Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw/train-labels-idx1-ubyte.gz (pid=4020) Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw/train-labels-idx1-ubyte.gz (pid=4021) Extracting /home/travis/.data/MNIST/raw/train-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw (pid=4021) Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw/t10k-images-idx3-ubyte.gz 113.5% 85.1% 113.5% (pid=4020) Extracting /home/travis/.data/MNIST/raw/train-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw (pid=4020) Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw/t10k-images-idx3-ubyte.gz 0.5% 0.5% 20.4% 5.5% (pid=4021) Extracting /home/travis/.data/MNIST/raw/t10k-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw 100.4% 94.9% 100.4% (pid=4020) Extracting /home/travis/.data/MNIST/raw/t10k-images-idx3-ubyte.gz to /home/travis/.data/MNIST/raw (pid=4021) Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw/t10k-labels-idx1-ubyte.gz (pid=4020) Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw/t10k-labels-idx1-ubyte.gz (pid=4021) Extracting /home/travis/.data/MNIST/raw/t10k-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw (pid=4021) Processing... 180.4% 180.4% (pid=4020) Extracting /home/travis/.data/MNIST/raw/t10k-labels-idx1-ubyte.gz to /home/travis/.data/MNIST/raw (pid=4020) Processing... (pid=4021) Done! (pid=4020) Done!
[INFO 04-17 18:35:57] ax.service.ax_client: Completed trial 0 with data: {'mean_accuracy': (0.12, 0.0)}. [INFO 04-17 18:35:57] ax.service.ax_client: Generated new trial 10 with parameters {'lr': 0.0, 'momentum': 0.37}. [INFO 04-17 18:35:57] ax.service.ax_client: Completed trial 1 with data: {'mean_accuracy': (0.74, 0.0)}. [INFO 04-17 18:35:57] ax.service.ax_client: Generated new trial 11 with parameters {'lr': 0.0, 'momentum': 0.45}.
(pid=4057) 2020-04-17 18:35:59,554 INFO trainable.py:217 -- Getting current IP. (pid=4101) 2020-04-17 18:36:00,333 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:36:15] ax.service.ax_client: Completed trial 2 with data: {'mean_accuracy': (0.86, 0.0)}. [INFO 04-17 18:36:17] ax.service.ax_client: Generated new trial 12 with parameters {'lr': 0.0, 'momentum': 0.0}. [INFO 04-17 18:36:17] ax.service.ax_client: Completed trial 3 with data: {'mean_accuracy': (0.39, 0.0)}. [INFO 04-17 18:36:19] ax.service.ax_client: Generated new trial 13 with parameters {'lr': 0.0, 'momentum': 0.05}.
(pid=4124) 2020-04-17 18:36:20,449 INFO trainable.py:217 -- Getting current IP. (pid=4123) 2020-04-17 18:36:21,630 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:36:36] ax.service.ax_client: Completed trial 4 with data: {'mean_accuracy': (0.11, 0.0)}. [INFO 04-17 18:36:39] ax.service.ax_client: Generated new trial 14 with parameters {'lr': 0.0, 'momentum': 0.57}. [INFO 04-17 18:36:39] ax.service.ax_client: Completed trial 5 with data: {'mean_accuracy': (0.92, 0.0)}. [INFO 04-17 18:36:41] ax.service.ax_client: Generated new trial 15 with parameters {'lr': 0.02, 'momentum': 0.9}.
(pid=4153) 2020-04-17 18:36:42,596 INFO trainable.py:217 -- Getting current IP. (pid=4154) 2020-04-17 18:36:44,029 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:36:58] ax.service.ax_client: Completed trial 6 with data: {'mean_accuracy': (0.82, 0.0)}. [INFO 04-17 18:37:00] ax.service.ax_client: Generated new trial 16 with parameters {'lr': 0.0, 'momentum': 1.0}. [INFO 04-17 18:37:00] ax.service.ax_client: Completed trial 7 with data: {'mean_accuracy': (0.96, 0.0)}. [INFO 04-17 18:37:02] ax.service.ax_client: Generated new trial 17 with parameters {'lr': 0.4, 'momentum': 1.0}.
(pid=4185) 2020-04-17 18:37:03,916 INFO trainable.py:217 -- Getting current IP. (pid=4186) 2020-04-17 18:37:05,368 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:37:19] ax.service.ax_client: Completed trial 8 with data: {'mean_accuracy': (0.11, 0.0)}. [INFO 04-17 18:37:21] ax.service.ax_client: Generated new trial 18 with parameters {'lr': 0.4, 'momentum': 0.0}. [INFO 04-17 18:37:21] ax.service.ax_client: Completed trial 9 with data: {'mean_accuracy': (0.11, 0.0)}. [INFO 04-17 18:37:25] ax.service.ax_client: Generated new trial 19 with parameters {'lr': 0.0, 'momentum': 0.75}.
(pid=4220) 2020-04-17 18:37:25,470 INFO trainable.py:217 -- Getting current IP. (pid=4221) 2020-04-17 18:37:27,782 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:37:41] ax.service.ax_client: Completed trial 10 with data: {'mean_accuracy': (0.24, 0.0)}. [INFO 04-17 18:37:44] ax.service.ax_client: Generated new trial 20 with parameters {'lr': 0.0, 'momentum': 0.72}. [INFO 04-17 18:37:44] ax.service.ax_client: Completed trial 11 with data: {'mean_accuracy': (0.92, 0.0)}. [INFO 04-17 18:37:47] ax.service.ax_client: Generated new trial 21 with parameters {'lr': 0.0, 'momentum': 0.35}.
(pid=4250) 2020-04-17 18:37:47,911 INFO trainable.py:217 -- Getting current IP. (pid=4251) 2020-04-17 18:37:49,550 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:38:04] ax.service.ax_client: Completed trial 12 with data: {'mean_accuracy': (0.67, 0.0)}. [INFO 04-17 18:38:06] ax.service.ax_client: Generated new trial 22 with parameters {'lr': 0.0, 'momentum': 0.18}. [INFO 04-17 18:38:06] ax.service.ax_client: Completed trial 13 with data: {'mean_accuracy': (0.91, 0.0)}. [INFO 04-17 18:38:08] ax.service.ax_client: Generated new trial 23 with parameters {'lr': 0.0, 'momentum': 0.0}.
(pid=4281) 2020-04-17 18:38:10,011 INFO trainable.py:217 -- Getting current IP. (pid=4282) 2020-04-17 18:38:10,717 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:38:26] ax.service.ax_client: Completed trial 14 with data: {'mean_accuracy': (0.9, 0.0)}. [INFO 04-17 18:38:28] ax.service.ax_client: Generated new trial 24 with parameters {'lr': 0.0, 'momentum': 0.14}. [INFO 04-17 18:38:28] ax.service.ax_client: Completed trial 15 with data: {'mean_accuracy': (0.1, 0.0)}.
(pid=4313) 2020-04-17 18:38:31,715 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:38:32] ax.service.ax_client: Generated new trial 25 with parameters {'lr': 0.0, 'momentum': 0.9}.
(pid=4312) 2020-04-17 18:38:35,071 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:38:47] ax.service.ax_client: Completed trial 16 with data: {'mean_accuracy': (0.88, 0.0)}. [INFO 04-17 18:38:51] ax.service.ax_client: Generated new trial 26 with parameters {'lr': 0.0, 'momentum': 1.0}. [INFO 04-17 18:38:51] ax.service.ax_client: Completed trial 17 with data: {'mean_accuracy': (0.1, 0.0)}. [INFO 04-17 18:38:51] ModelBridge: Leaving out out-of-design observations for arms: 17_0
(pid=4348) 2020-04-17 18:38:54,954 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:38:56] ax.service.ax_client: Generated new trial 27 with parameters {'lr': 0.0, 'momentum': 0.85}.
(pid=4347) 2020-04-17 18:38:58,710 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:39:11] ax.service.ax_client: Completed trial 18 with data: {'mean_accuracy': (0.1, 0.0)}. [INFO 04-17 18:39:11] ModelBridge: Leaving out out-of-design observations for arms: 17_0 [INFO 04-17 18:39:14] ax.service.ax_client: Generated new trial 28 with parameters {'lr': 0.0, 'momentum': 0.68}. [INFO 04-17 18:39:15] ax.service.ax_client: Completed trial 19 with data: {'mean_accuracy': (0.97, 0.0)}. [INFO 04-17 18:39:15] ModelBridge: Leaving out out-of-design observations for arms: 17_0
(pid=4396) 2020-04-17 18:39:17,169 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:39:20] ax.service.ax_client: Generated new trial 29 with parameters {'lr': 0.0, 'momentum': 0.77}.
(pid=4411) 2020-04-17 18:39:23,688 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:39:33] ax.service.ax_client: Completed trial 20 with data: {'mean_accuracy': (0.87, 0.0)}.
(pid=4426) 2020-04-17 18:39:36,858 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:39:39] ax.service.ax_client: Completed trial 21 with data: {'mean_accuracy': (0.94, 0.0)}.
(pid=4441) 2020-04-17 18:39:43,288 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:39:53] ax.service.ax_client: Completed trial 22 with data: {'mean_accuracy': (0.93, 0.0)}.
(pid=4457) 2020-04-17 18:39:56,556 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:39:59] ax.service.ax_client: Completed trial 23 with data: {'mean_accuracy': (0.97, 0.0)}.
(pid=4473) 2020-04-17 18:40:02,902 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:40:12] ax.service.ax_client: Completed trial 24 with data: {'mean_accuracy': (0.85, 0.0)}.
(pid=4488) 2020-04-17 18:40:16,506 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:40:19] ax.service.ax_client: Completed trial 25 with data: {'mean_accuracy': (0.49, 0.0)}.
(pid=4508) 2020-04-17 18:40:22,621 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:40:32] ax.service.ax_client: Completed trial 26 with data: {'mean_accuracy': (0.86, 0.0)}.
(pid=4523) 2020-04-17 18:40:35,876 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:40:38] ax.service.ax_client: Completed trial 27 with data: {'mean_accuracy': (0.89, 0.0)}.
(pid=4539) 2020-04-17 18:40:42,068 INFO trainable.py:217 -- Getting current IP.
[INFO 04-17 18:40:52] ax.service.ax_client: Completed trial 28 with data: {'mean_accuracy': (0.93, 0.0)}. [INFO 04-17 18:40:56] ax.service.ax_client: Completed trial 29 with data: {'mean_accuracy': (0.25, 0.0)}.
<ray.tune.analysis.experiment_analysis.ExperimentAnalysis at 0x7f39aec68860>
best_parameters, values = ax.get_best_parameters()
best_parameters
{'lr': 0.0008918200730215226, 'momentum': 0.745181368720066}
means, covariances = values
means
{'mean_accuracy': 0.9738333633678309}
render(
plot_contour(
model=ax.generation_strategy.model, param_x='lr', param_y='momentum', metric_name='mean_accuracy'
)
)
# `plot_single_method` expects a 2-d array of means, because it expects to average means from multiple
# optimization runs, so we wrap out best objectives array in another array.
best_objectives = np.array([[trial.objective_mean * 100 for trial in ax.experiment.trials.values()]])
best_objective_plot = optimization_trace_single_method(
y=np.maximum.accumulate(best_objectives, axis=1),
title="Model performance vs. # of iterations",
ylabel="Accuracy",
)
render(best_objective_plot)
Total runtime of script: 5 minutes, 27.78 seconds.