Sweep configuration structure
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.
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 Jupyter notebook.
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
.
- CLI
- Python script or Jupyter notebook
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
:
sweep_configuration = {
"name": "sweepdemo",
"method": "bayes",
"metric": {"goal": "minimize", "name": "validation_loss"},
"parameters": {
"learning_rate": {"min": 0.0001, "max": 0.1},
"batch_size": {"values": [16, 32, 64]},
"epochs": {"values": [5, 10, 15]},
"optimizer": {"values": ["adam", "sgd"]},
},
}
program: train.py
name: sweepdemo
method: bayes
metric:
goal: minimize
name: validation_loss
parameters:
learning_rate:
min: 0.0001
max: 0.1
batch_size:
values: [16, 32, 64]
epochs:
values: [5, 10, 15]
optimizer:
values: ["adam", "sgd"]
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).
def main():
run = wandb.init(config={"nested_param": {"manual_key": 1}})
sweep_configuration = {
"top_level_param": 0,
"nested_param": {
"learning_rate": 0.01,
"double_nested_param": {"x": 0.9, "y": 0.8},
},
}
# Initialize sweep by passing in config.
sweep_id = wandb.sweep(sweep=sweep_configuration, project="<project>")
# Start sweep job.
wandb.agent(sweep_id, function=main, count=4)
The nested_param.manual_key
that is passed when the W&B run is initialized (line 2) is not accessible. The run.config
only possess the key-value pairs that are defined in the sweep configuration dictionary (lines 4-13).
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 <>
.
program: <insert>
method: <insert>
parameter:
hyperparameter_name0:
value: 0
hyperparameter_name1:
values: [0, 0, 0]
hyperparameter_name:
distribution: <insert>
value: <insert>
hyperparameter_name2:
distribution: <insert>
min: <insert>
max: <insert>
q: <insert>
hyperparameter_name3:
distribution: <insert>
values:
- <list_of_values>
- <list_of_values>
- <list_of_values>
early_terminate:
type: hyperband
s: 0
eta: 0
max_iter: 0
command:
- ${Command macro}
- ${Command macro}
- ${Command macro}
- ${Command macro}
Sweep configuration examples
- CLI
- Python script or Jupyter notebook
program: train.py
method: random
metric:
goal: minimize
name: loss
parameters:
batch_size:
distribution: q_log_uniform_values
max: 256
min: 32
q: 8
dropout:
values: [0.3, 0.4, 0.5]
epochs:
value: 1
fc_layer_size:
values: [128, 256, 512]
learning_rate:
distribution: uniform
max: 0.1
min: 0
optimizer:
values: ["adam", "sgd"]
sweep_config = {
"method": "random",
"metric": {"goal": "minimize", "name": "loss"},
"parameters": {
"batch_size": {
"distribution": "q_log_uniform_values",
"max": 256,
"min": 32,
"q": 8,
},
"dropout": {"values": [0.3, 0.4, 0.5]},
"epochs": {"value": 1},
"fc_layer_size": {"values": [128, 256, 512]},
"learning_rate": {"distribution": "uniform", "max": 0.1, "min": 0},
"optimizer": {"values": ["adam", "sgd"]},
},
}
Bayes hyperband example
program: train.py
method: bayes
metric:
goal: minimize
name: val_loss
parameters:
dropout:
values: [0.15, 0.2, 0.25, 0.3, 0.4]
hidden_layer_size:
values: [96, 128, 148]
layer_1_size:
values: [10, 12, 14, 16, 18, 20]
layer_2_size:
values: [24, 28, 32, 36, 40, 44]
learn_rate:
values: [0.001, 0.01, 0.003]
decay:
values: [1e-5, 1e-6, 1e-7]
momentum:
values: [0.8, 0.9, 0.95]
epochs:
value: 27
early_terminate:
type: hyperband
s: 2
eta: 3
max_iter: 27
The proceeding tabs show how to specify either a minimum or maximum number of iterations for early_terminate
:
- Minimum number of iterations specified
- Maximum number of iterations specified
early_terminate:
type: hyperband
min_iter: 3
The brackets for this example are: [3, 3*eta, 3*eta*eta, 3*eta*eta*eta]
, which equals [3, 9, 27, 81]
.
early_terminate:
type: hyperband
max_iter: 27
s: 2
The brackets for this example are [27/eta, 27/eta/eta]
, which equals [9, 3]
.
Command example
program: main.py
metric:
name: val_loss
goal: minimize
method: bayes
parameters:
optimizer.config.learning_rate:
min: !!float 1e-5
max: 0.1
experiment:
values: [expt001, expt002]
optimizer:
values: [sgd, adagrad, adam]
command:
- ${env}
- ${interpreter}
- ${program}
- ${args_no_hyphens}
- Unix
- Windows
/usr/bin/env python train.py --param1=value1 --param2=value2
python train.py --param1=value1 --param2=value2
The proceeding tabs show how to specify common command macros:
- Set python interpreter
- Add extra parameters
- Omit arguments
- Hydra
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:
command:
- ${env}
- python3
- ${program}
- ${args}
The following shows how to add extra command line arguments not specified by sweep configuration parameters:
command:
- ${env}
- ${interpreter}
- ${program}
- "--config"
- "your-training-config.json"
- ${args}
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:
command:
- ${env}
- ${interpreter}
- ${program}
You can change the command to pass arguments the way tools like Hydra expect. See Hydra with W&B for more information.
command:
- ${env}
- ${interpreter}
- ${program}
- ${args_no_hyphens}