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 report
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 numpy as np
import torch
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 init_notebook_plotting, render
from ax.utils.tutorials.cnn_utils import CNN, evaluate, load_mnist, train
init_notebook_plotting()
[INFO 12-16 16:52:53] ax.utils.notebook.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 12-16 16:52:53] 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 6 decimal points.
Here we set up the search space and specify the objective; refer to the Ax API tutorials for more detail.
MINIMIZE = False # Whether we should be minimizing or maximizing the objective
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",
minimize=MINIMIZE,
)
[INFO 12-16 16:52:53] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter lr. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 12-16 16:52:53] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter momentum. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 12-16 16:52:53] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='lr', parameter_type=FLOAT, range=[1e-06, 0.4], log_scale=True), RangeParameter(name='momentum', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]). [INFO 12-16 16:52:53] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 12-16 16:52:53] 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.
ax.experiment.optimization_config.objective.minimize
False
load_mnist(data_path="~/.data") # Pre-load the dataset before the initial evaluations are executed.
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to /home/runner/.data/MNIST/raw/train-images-idx3-ubyte.gz
Extracting /home/runner/.data/MNIST/raw/train-images-idx3-ubyte.gz to /home/runner/.data/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to /home/runner/.data/MNIST/raw/train-labels-idx1-ubyte.gz
Extracting /home/runner/.data/MNIST/raw/train-labels-idx1-ubyte.gz to /home/runner/.data/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to /home/runner/.data/MNIST/raw/t10k-images-idx3-ubyte.gz
Extracting /home/runner/.data/MNIST/raw/t10k-images-idx3-ubyte.gz to /home/runner/.data/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to /home/runner/.data/MNIST/raw/t10k-labels-idx1-ubyte.gz
Extracting /home/runner/.data/MNIST/raw/t10k-labels-idx1-ubyte.gz to /home/runner/.data/MNIST/raw
(<torch.utils.data.dataloader.DataLoader at 0x7f0bb4060c50>, <torch.utils.data.dataloader.DataLoader at 0x7f0bb1029ad0>, <torch.utils.data.dataloader.DataLoader at 0x7f0bb1029dd0>)
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,
)
report(
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:
# Set up AxSearcher in RayTune
algo = AxSearch(ax_client=ax)
# Wrap AxSearcher in a concurrently limiter, to ensure that Bayesian optimization receives the
# data for completed trials before creating more trials
algo = tune.suggest.ConcurrencyLimiter(algo, max_concurrent=3)
tune.run(
train_evaluate,
num_samples=30,
search_alg=algo,
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}.
)
[INFO 12-16 16:52:56] ax.service.ax_client: Generated new trial 0 with parameters {'lr': 1e-06, 'momentum': 0.064122}. [INFO 12-16 16:52:56] ax.service.ax_client: Generated new trial 1 with parameters {'lr': 0.001146, 'momentum': 0.446298}. [INFO 12-16 16:52:57] ax.service.ax_client: Generated new trial 2 with parameters {'lr': 0.00045, 'momentum': 0.382397}. [INFO 12-16 16:53:07] ax.service.ax_client: Completed trial 0 with data: {'mean_accuracy': (0.117167, None)}. [INFO 12-16 16:53:07] ax.service.ax_client: Generated new trial 3 with parameters {'lr': 3e-06, 'momentum': 0.061471}. [INFO 12-16 16:53:09] ax.service.ax_client: Completed trial 1 with data: {'mean_accuracy': (0.962, None)}. [INFO 12-16 16:53:09] ax.service.ax_client: Generated new trial 4 with parameters {'lr': 1.4e-05, 'momentum': 0.305978}. [INFO 12-16 16:53:20] ax.service.ax_client: Completed trial 2 with data: {'mean_accuracy': (0.950667, None)}. [WARNING 12-16 16:53:20] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:53:20] ax.service.ax_client: Generated new trial 5 with parameters {'lr': 0.028965, 'momentum': 0.19097}. [INFO 12-16 16:53:21] ax.service.ax_client: Completed trial 3 with data: {'mean_accuracy': (0.313, None)}. [WARNING 12-16 16:53:21] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:53:22] ax.service.ax_client: Generated new trial 6 with parameters {'lr': 0.000234, 'momentum': 0.834794}. [INFO 12-16 16:53:33] ax.service.ax_client: Completed trial 4 with data: {'mean_accuracy': (0.495833, None)}. [WARNING 12-16 16:53:33] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:53:33] ax.service.ax_client: Generated new trial 7 with parameters {'lr': 0.001633, 'momentum': 0.0}. [INFO 12-16 16:53:34] ax.service.ax_client: Completed trial 5 with data: {'mean_accuracy': (0.087333, None)}. [WARNING 12-16 16:53:34] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:53:35] ax.service.ax_client: Generated new trial 8 with parameters {'lr': 0.000402, 'momentum': 0.578424}. [INFO 12-16 16:53:45] ax.service.ax_client: Completed trial 6 with data: {'mean_accuracy': (0.946, None)}. [WARNING 12-16 16:53:45] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:53:47] ax.service.ax_client: Generated new trial 9 with parameters {'lr': 0.0026, 'momentum': 0.899987}. [INFO 12-16 16:53:48] ax.service.ax_client: Completed trial 7 with data: {'mean_accuracy': (0.966167, None)}. [WARNING 12-16 16:53:48] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:53:49] ax.service.ax_client: Generated new trial 10 with parameters {'lr': 0.000453, 'momentum': 0.0}. [INFO 12-16 16:53:59] ax.service.ax_client: Completed trial 8 with data: {'mean_accuracy': (0.943333, None)}. [WARNING 12-16 16:53:59] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:01] ax.service.ax_client: Generated new trial 11 with parameters {'lr': 0.000658, 'momentum': 1.0}. [INFO 12-16 16:54:01] ax.service.ax_client: Completed trial 9 with data: {'mean_accuracy': (0.099833, None)}. [WARNING 12-16 16:54:01] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:03] ax.service.ax_client: Generated new trial 12 with parameters {'lr': 0.001163, 'momentum': 0.221115}. [INFO 12-16 16:54:13] ax.service.ax_client: Completed trial 11 with data: {'mean_accuracy': (0.101667, None)}. [WARNING 12-16 16:54:13] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:13] ax.service.ax_client: Generated new trial 13 with parameters {'lr': 6.1e-05, 'momentum': 0.739506}. [INFO 12-16 16:54:13] ax.service.ax_client: Completed trial 10 with data: {'mean_accuracy': (0.922, None)}. [WARNING 12-16 16:54:13] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:15] ax.service.ax_client: Generated new trial 14 with parameters {'lr': 1.3e-05, 'momentum': 0.931975}. [INFO 12-16 16:54:26] ax.service.ax_client: Completed trial 12 with data: {'mean_accuracy': (0.962167, None)}. [WARNING 12-16 16:54:26] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:28] ax.service.ax_client: Generated new trial 15 with parameters {'lr': 0.000132, 'momentum': 0.682863}. [INFO 12-16 16:54:28] ax.service.ax_client: Completed trial 13 with data: {'mean_accuracy': (0.9095, None)}. [WARNING 12-16 16:54:28] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:29] ax.service.ax_client: Generated new trial 16 with parameters {'lr': 1e-06, 'momentum': 1.0}. [INFO 12-16 16:54:40] ax.service.ax_client: Completed trial 14 with data: {'mean_accuracy': (0.907833, None)}. [WARNING 12-16 16:54:40] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:43] ax.service.ax_client: Generated new trial 17 with parameters {'lr': 3.8e-05, 'momentum': 1.0}. [INFO 12-16 16:54:43] ax.service.ax_client: Completed trial 15 with data: {'mean_accuracy': (0.9335, None)}. [WARNING 12-16 16:54:43] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:47] ax.service.ax_client: Generated new trial 18 with parameters {'lr': 0.000124, 'momentum': 0.80179}. [INFO 12-16 16:54:55] ax.service.ax_client: Completed trial 16 with data: {'mean_accuracy': (0.811833, None)}. [WARNING 12-16 16:54:55] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:54:57] ax.service.ax_client: Generated new trial 19 with parameters {'lr': 2e-06, 'momentum': 0.7678}. [INFO 12-16 16:54:58] ax.service.ax_client: Completed trial 17 with data: {'mean_accuracy': (0.873167, None)}. [WARNING 12-16 16:54:58] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:55:02] ax.service.ax_client: Generated new trial 20 with parameters {'lr': 0.000792, 'momentum': 0.109351}. [INFO 12-16 16:55:11] ax.service.ax_client: Completed trial 18 with data: {'mean_accuracy': (0.938333, None)}. [WARNING 12-16 16:55:11] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:55:15] ax.service.ax_client: Generated new trial 21 with parameters {'lr': 0.000397, 'momentum': 0.200012}. [INFO 12-16 16:55:15] ax.service.ax_client: Completed trial 19 with data: {'mean_accuracy': (0.487333, None)}. [WARNING 12-16 16:55:15] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:55:19] ax.service.ax_client: Generated new trial 22 with parameters {'lr': 0.000303, 'momentum': 0.704772}. [INFO 12-16 16:55:26] ax.service.ax_client: Completed trial 20 with data: {'mean_accuracy': (0.937333, None)}. [WARNING 12-16 16:55:26] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:55:29] ax.service.ax_client: Generated new trial 23 with parameters {'lr': 6e-06, 'momentum': 1.0}. [INFO 12-16 16:55:30] ax.service.ax_client: Completed trial 21 with data: {'mean_accuracy': (0.9245, None)}. [WARNING 12-16 16:55:30] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:55:37] ax.service.ax_client: Generated new trial 24 with parameters {'lr': 0.000874, 'momentum': 0.334742}. [INFO 12-16 16:55:43] ax.service.ax_client: Completed trial 22 with data: {'mean_accuracy': (0.95, None)}. [WARNING 12-16 16:55:44] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:55:47] ax.service.ax_client: Generated new trial 25 with parameters {'lr': 9.7e-05, 'momentum': 0.0}. [INFO 12-16 16:55:47] ax.service.ax_client: Completed trial 23 with data: {'mean_accuracy': (0.879667, None)}. [WARNING 12-16 16:55:47] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:55:52] ax.service.ax_client: Generated new trial 26 with parameters {'lr': 5.2e-05, 'momentum': 0.86372}. [INFO 12-16 16:55:58] ax.service.ax_client: Completed trial 24 with data: {'mean_accuracy': (0.957833, None)}. [WARNING 12-16 16:55:58] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:56:02] ax.service.ax_client: Generated new trial 27 with parameters {'lr': 0.002226, 'momentum': 0.290057}. [INFO 12-16 16:56:04] ax.service.ax_client: Completed trial 25 with data: {'mean_accuracy': (0.8855, None)}. [WARNING 12-16 16:56:04] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:56:07] ax.service.ax_client: Generated new trial 28 with parameters {'lr': 0.000693, 'momentum': 0.4724}. [INFO 12-16 16:56:14] ax.service.ax_client: Completed trial 26 with data: {'mean_accuracy': (0.929833, None)}. [WARNING 12-16 16:56:14] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>). [INFO 12-16 16:56:19] ax.service.ax_client: Generated new trial 29 with parameters {'lr': 0.000954, 'momentum': 0.569008}. [INFO 12-16 16:56:19] ax.service.ax_client: Completed trial 27 with data: {'mean_accuracy': (0.967833, None)}. [INFO 12-16 16:56:28] ax.service.ax_client: Completed trial 28 with data: {'mean_accuracy': (0.965, None)}. [INFO 12-16 16:56:30] ax.service.ax_client: Completed trial 29 with data: {'mean_accuracy': (0.963333, None)}.
<ray.tune.analysis.experiment_analysis.ExperimentAnalysis at 0x7f0bb0976f50>
best_parameters, values = ax.get_best_parameters()
best_parameters
[WARNING 12-16 16:56:31] ax.utils.common.kwargs: `<class 'ax.modelbridge.torch.TorchModelBridge'>` expected argument `transform_configs` to be of type typing.Union[typing.Dict[str, typing.Dict[str, typing.Union[int, float, str, botorch.acquisition.acquisition.AcquisitionFunction, typing.Dict[str, typing.Any], NoneType]]], NoneType]. Got {'Winsorize': {'optimization_config': OptimizationConfig(objective=Objective(metric_name="mean_accuracy", minimize=False), outcome_constraints=[])}} (type: <class 'dict'>).
{'lr': 0.0011459330336025936, 'momentum': 0.4462983813136816}
means, covariances = values
means
{'mean_accuracy': 0.9655014395713125}
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: 3 minutes, 44.64 seconds.