Skip to main content
A sweep combines a strategy for exploring hyperparameter values with the code that evaluates them. The strategy can be as simple as trying every option or as complex as Bayesian Optimization and Hyperband (BOHB). This guide shows you how to define a sweep configuration that specifies which hyperparameters to search, which search strategy to use, and how to evaluate each run. Use it when you’re setting up a new sweep or adapting an existing configuration to a different search method or parameter space. Define a sweep configuration either in a Python dictionary or a YAML file. How you define your sweep configuration depends on how you want to manage your sweep.
Define your sweep configuration in a YAML file if you want to initialize a sweep and start a sweep agent from the command line. Define your sweep in a Python dictionary if you initialize a sweep and start a sweep entirely within a Python script or notebook.
The following sections describe how to format your sweep configuration. See Sweep configuration options for a comprehensive list of top-level sweep configuration keys.

Basic structure

Sweep configurations are defined using either YAML or a Python dictionary, with key-value pairs and nested structures. Use top-level keys within your sweep configuration to define qualities of your sweep search, such as the sweep name, the parameters to search through, and the search method. For example, the following code snippets show the same sweep configuration defined within a YAML file and within a Python dictionary. The sweep configuration specifies five top-level keys: program, name, method, metric, and parameters.
To manage sweeps interactively from the command line, define a sweep configuration in a YAML file.
config.yaml
The top-level parameters key nests the following keys: learning_rate, batch_size, epochs, and optimizer. For each nested key you specify, you can provide one or more values, a distribution, a probability, and more. For more information, see the parameters section in Sweep configuration options.

Double-nested parameters

Use nested parameters when you want to group related hyperparameters together or when your training code expects a nested configuration structure. To define a nested parameter, include an additional parameters key under the top-level parameter name. The following example shows a sweep configuration with nested parameters nested_category_1, nested_category_2, and nested_category_3, each including the additional parameters momentum and weight_decay. The following code examples show the configuration in both a YAML file and a Python dictionary:
Nested parameters defined in sweep configuration overwrite keys specified in a W&B run configuration.As an example, suppose you have train.py script that initializes a run with a nested default:
Your sweep configuration defines nested parameters under a top-level "parameters" key:
During a sweep run, run.config["nested_param"] reflects the subtree defined by the sweep configuration (learning_rate and double_nested_param). It doesn’t include manual_key, which is defined in wandb.init(config=...).

Sweep configuration template

Use this template as a starting point for a new sweep configuration. It illustrates the most common parameter and early-termination patterns. Replace hyperparameter_name with the name of your hyperparameter and any values enclosed in brackets.
config.yaml
To express a numeric value using scientific notation, add the YAML !!float operator, which casts the value to a floating-point number. For example, min: !!float 1e-5. For more information, see Macro and custom command arguments example.

Sweep configuration examples

The following sweep configurations illustrate common scenarios. Use them as references when adapting a sweep to your own training script.
config.yaml

Bayes hyperband example

The following example combines Bayesian search with Hyperband early termination to stop underperforming runs early and preserve resources for more promising configurations.
The following tabs show how to specify either a minimum or maximum number of iterations for early_terminate:
The brackets for this example are [3, 3*eta, 3*eta*eta, 3*eta*eta*eta], which equals [3, 9, 27, 81].

Macro and custom command arguments example

This example shows how to construct the command that the sweep agent runs for each trial when you need finer control than the default invocation provides. For more complex command-line arguments, you can use macros to pass environment variables, the Python interpreter, and additional arguments. W&B supports predefined macros and custom command-line arguments that you can specify in your sweep configuration. For example, the following sweep configuration (sweep.yaml) defines a command that runs a Python script (run.py) with the ${env}, ${interpreter}, and ${program} macros replaced with the appropriate values when the sweep runs. The --batch_size=${batch_size}, --test=True, and --optimizer=${optimizer} arguments use custom macros to pass the values of the batch_size, test, and optimizer parameters defined in the sweep configuration.
sweep.yaml
The associated Python script run.py can then parse these command-line arguments using the argparse module:
run.py
See the Command macros section in Sweep configuration options for a list of predefined macros you can use in your sweep configuration.

Boolean arguments

If your sweep passes boolean flags through command arguments, your training script needs extra handling because argparse doesn’t interpret boolean strings by default. The argparse module doesn’t support boolean arguments by default. To define a boolean argument, use the action parameter or use a custom function to convert the string representation of the boolean value to a boolean type. For example, you can use the following code snippet to define a boolean argument. Pass store_true or store_false as an argument to ArgumentParser:
You can also define a custom function to convert the string representation of the boolean value to a boolean type. For example, the following code snippet defines the str2bool function, which converts a string to a boolean value: