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:
- Define multiple objectives to optimize more than one metric.
- Define a custom sweep-level stopping function to control when the scheduler stops proposing new runs.
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.- Weights & Biases
- Optuna
- Ax
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
Setscheduler.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.
- Weights & Biases
- Optuna
- Ax
Configure the built-in Weights & Biases engine using a sweep configuration.Set For details about each search method, see Search methods.
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
Define multiple objectives
If you use Optuna or Ax, you can usemetrics (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.- Optuna
- Ax
For a multi-objective sweep with Optuna, define each objective in the The following demo script reads the suggested Within your The resulting study minimizes
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
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
optimizer.py file, pass the stored sweep configuration from sweep_info.config to sweep_directions() when you create the Optuna study:optimizer.py
validation_loss and maximizes accuracy.Define a conditional search space
Only the Optuna engine supports
scheduler.search_space.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.
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
define_search_space(trial) and uses the returned dictionary as the run configuration:
search.py
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.- Weights & Biases
- Optuna
- Ax
Use Hyperband for hyperparameter optimization with early stopping based on intermediate results.Set See
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
early_terminate for configuration options.Stop the sweep
Sweep-level stopping controls when the scheduler stops proposing new runs. Userun_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.- Optuna
- Ax
To enable sweep-level stopping, the optimization function you specify in After processing results, the scheduler passes the current study to the stopping function. The function returns In this example, The stopping function counts only successfully completed trials. It returns After each run finishes, the scheduler passes its logged loss to the Optuna study. Once three trials complete, the stopping function returns
scheduler.optimizer must return an Optuna study and a stopping function as a tuple: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
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
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
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 ascheduler.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.