Define a sweep configuration
6 minute read
A W&B 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).
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.
The following guide describes how to format your sweep configuration. See Sweep configuration options for a comprehensive list of top-level sweep configuration keys.
Basic structure
Both sweep configuration format options (YAML and Python dictionary) utilize key-value pairs and nested structures.
Use top-level keys within your sweep configuration to define qualities of your sweep search such as the name of the sweep (name
key), the parameters to search through (parameters
key), the methodology to search the parameter space (method
key), and more.
For example, the proceeding code snippets show the same sweep configuration defined within a YAML file and within a Python dictionary. Within the sweep configuration there are five top level keys specified: program
, name
, method
, metric
and parameters
.
Define a sweep configuration in a YAML file if you want to manage sweeps interactively from the command line (CLI)
Define a sweep in a Python dictionary data structure if you define training algorithm in a Python script or Jupyter notebook.
The proceeding code snippet stores a sweep configuration in a variable named sweep_configuration
:
Within the top level parameters
key, the following keys are nested: learning_rate
, batch_size
, epoch
, and optimizer
. For each of the nested keys 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
Sweep configurations support nested parameters. To delineate a nested parameter, use an additional parameters
key under the top level parameter name. Sweep configs support multi-level nesting.
Specify a probability distribution for your random variables if you use a Bayesian or random hyperparameter search. For each hyperparameter:
- Create a top level
parameters
key in your sweep config. - Within the
parameters
key, nest the following:- Specify the name of hyperparameter you want to optimize.
- Specify the distribution you want to use for the
distribution
key. Nest thedistribution
key-value pair underneath the hyperparameter name. - Specify one or more values to explore. The value (or values) should be inline with the distribution key.
- (Optional) Use an additional parameters key under the top level parameter name to delineate a nested parameter.
Nested parameters defined in sweep configuration overwrite keys specified in a W&B run configuration.
For example, suppose you initialize a W&B run with the following configuration in a train.py
Python script (see Lines 1-2). Next, you define a sweep configuration in a dictionary called sweep_configuration
(see Lines 4-13). You then pass the sweep config dictionary to wandb.sweep
to initialize a sweep config (see Line 16).
The nested_param.manual_key
that is passed when the W&B run is initialized is not accessible. The run.config
only possess the key-value pairs that are defined in the sweep configuration dictionary.
Sweep configuration template
The following template shows how you can configure parameters and specify search constraints. Replace hyperparameter_name
with the name of your hyperparameter and any values enclosed in <>
.
Sweep configuration examples
Bayes hyperband example
The proceeding 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]
.
The brackets for this example are [27/eta, 27/eta/eta]
, which equals [9, 3]
.
Command example
The proceeding tabs show how to specify common command macros:
Remove the {$interpreter}
macro and provide a value explicitly to hardcode the python interpreter. For example, the following code snippet demonstrates how to do this:
The following shows how to add extra command line arguments not specified by sweep configuration parameters:
If your program does not use argument parsing you can avoid passing arguments all together and take advantage of wandb.init
picking up sweep parameters into wandb.config
automatically:
You can change the command to pass arguments the way tools like Hydra expect. See Hydra with W&B for more information.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.