Skip to main content
Choose an optimization engine for Weights & Biases Sweeps. Use the built-in Weights & Biases engine for random, grid, or Bayesian search, or use Optuna or Ax for their search algorithms and customization options. The engine, scheduler, and sweep agents work together: This page describes how to configure an engine for a scheduler running in your own environment. For complete YAML examples, see Sweep scheduler examples.

Choose an engine

Choose an engine based on the search methods and customization options you need: Use Optuna or Ax to: With Optuna, you can also define a conditional search space to select parameters based on earlier choices in a trial.

Install dependencies

Install Weights & Biases Python SDK version 0.31.0 or later with the extra dependencies for your engine. Run the installation command in the environment where you run the scheduler.
Use Python 3.10 or later. Install the SDK with the sweeps extra to include sweeps>=0.2.0 in the scheduler environment:

Configure optimization

Configure the search strategy, objectives, and search space for your engine. The built-in Weights & Biases engine uses the sweep configuration. For Optuna and Ax, you can provide a Python function that configures an Optuna study or Ax client.

Configure the search strategy

Set scheduler.engine to wandb, optuna, or ax. For Optuna and Ax, custom optimizer functions are optional. You can switch engines by changing scheduler.engine and use the search space and objectives defined in the sweep configuration, including multiple objectives defined with metrics. To use a custom optimizer function, set scheduler.source to your Python filename and scheduler.optimizer to the function name. For Optuna, you can also define a custom search space with scheduler.search_space.
Configure the built-in Weights & Biases engine using a sweep configuration.Set method to random, grid, or bayes. The engine uses method, metric, and parameters to select configurations for sweep runs.For example, to configure Bayesian search using the built-in Weights & Biases engine:
sweep.yaml
For details about each search method, see Search methods.
For individual-run and sweep-level stopping options, see Configure stopping.

Define multiple objectives

If you use Optuna or Ax, you can use metrics (plural) to define multiple objectives. When you create a sweep with wandb sweep, Weights & Biases stores the sweep configuration. When the scheduler starts, it retrieves that configuration and passes it to your custom optimizer function as part of a SweepInfo object. Use sweep_info.config to access the stored sweep configuration.
The built-in Weights & Biases engine supports only a single objective, defined with metric.
You don’t need a custom optimizer function to use multiple objectives. The following examples show how to configure the objectives when you provide one.
For a multi-objective sweep with Optuna, define each objective in the metrics list. Use the sweep_directions() helper from wandb.sdk.sweeps.scheduler.optuna to convert the goals to Optuna study directions in the same order.For example, the following sweep minimizes validation loss and maximizes accuracy:
sweep.yaml
The following demo script reads the suggested learning_rate and batch_size values, calculates two synthetic objective values, and logs them to the run. The final logged values appear in the run summary, where the scheduler can retrieve them.
train.py
Within your optimizer.py file, pass the stored sweep configuration from sweep_info.config to sweep_directions() when you create the Optuna study:
optimizer.py
The resulting study minimizes validation_loss and maximizes accuracy.

Define a conditional search space

Only the Optuna engine supports scheduler.search_space.
For Optuna, use scheduler.search_space to define conditional parameters in Python. For example, you can define different hyperparameters for different model types or vary the number of parameters based on earlier choices. Set scheduler.search_space to the name of a function in the file specified by scheduler.source. The scheduler passes an Optuna trial to the function and uses the returned dictionary as the run configuration.
For a fixed search space, define parameters in the sweep configuration.
When you set scheduler.search_space, the scheduler ignores the top-level parameters definitions. Define every parameter you want to pass to a run in the Python function. For example, suppose you have the following sweep configuration:
sweep.yaml
For each trial, the scheduler calls define_search_space(trial) and uses the returned dictionary as the run configuration:
search.py
Every run receives model and learning_rate. Runs with model="transformer" also receive num_layers and num_heads. Runs with model="linear" receive regularization instead.

Configure stopping

Stopping can apply to an individual run or to the overall sweep. Individual-run early stopping ends runs that are unlikely to produce a useful result. Sweep-level stopping prevents the scheduler from proposing new runs after a condition is met.

Stop individual runs

To evaluate a run’s progress, log intermediate values for the objective metric throughout training. Each engine provides its own mechanism for deciding whether to stop a run.
The top-level early_terminate key applies only to the built-in Weights & Biases engine. For Optuna or Ax, configure the engine’s native early-stopping mechanism.
Use Hyperband for hyperparameter optimization with early stopping based on intermediate results.Set early_terminate.type to hyperband and use min_iter to set the minimum number of iterations.For example, the following sweep configuration uses Hyperband for early stopping with a minimum of 3 iterations:
sweep.yaml
See early_terminate for configuration options.

Stop the sweep

Sweep-level stopping controls when the scheduler stops proposing new runs. Use run_cap to set a fixed maximum number of runs for any engine. For Optuna or Ax, you can also return a stopping function that evaluates the current state of the optimization.
In the following examples, the scheduler controls the search strategy, so it ignores method: random. However, the Weights & Biases sweep schema requires method.
The scheduler might already have runs queued or running when a stopping condition is met. Those runs can continue after the scheduler stops proposing new ones.
To enable sweep-level stopping, the optimization function you specify in scheduler.optimizer must return an Optuna study and a stopping function as a tuple:
After processing results, the scheduler passes the current study to the stopping function. The function returns True to stop the sweep or False to continue proposing new runs.For example, the following sweep configuration searches for x, limits the sweep to 20 runs, and instructs the Optuna scheduler to call make_study() from the optimizer.py file:
sweep.yaml
In this example, run_cap provides a fallback limit if the stopping function doesn’t end the sweep first.Next, define the Optuna study and stopping function. make_study() configures a seeded random sampler and returns the study with stop_after_three_completed_trials():
optimizer.py
The stopping function counts only successfully completed trials. It returns True after the third completed trial, which tells the scheduler to stop proposing configurations. Failed, pruned, and running trials don’t contribute to the count.Finally, define the objective that each sweep run evaluates. The training script retrieves the suggested value of x, calculates its loss, and logs that loss to Weights & Biases:
train.py
After each run finishes, the scheduler passes its logged loss to the Optuna study. Once three trials complete, the stopping function returns True and the scheduler stops proposing new runs. Runs that are already queued or running can still finish.

Handle scheduler restarts

The sweep scheduler operates on an existing sweep, so you can stop and restart the scheduler without creating a new sweep. Each time the scheduler starts, it reconstructs the optimization history from the sweep’s existing run data and then continues the optimization loop. It does not restore the engine from a saved optimizer checkpoint. When the scheduler starts or restarts, it calls your custom optimizer function to create a new engine instance. Return a fresh Optuna study or Ax client each time the function runs. Don’t load a separate optimizer checkpoint or reuse an engine instance from a previous scheduler process. If you use a scheduler.source file, the scheduler loads its current contents each time it starts. You can update the file between starts to fix bugs or modify the custom optimizer or search_space function. These changes can affect subsequent suggestions, so preserve the version of the source file used for each scheduler session if you need to reproduce the optimization.
Last modified on November 6, 2026