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.
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 notebook
Define a sweep configuration in a YAML file if you want to manage sweeps interactively from the command line (CLI)
config.yaml
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 additionalparameters
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 The
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).train.py
nested_param.manual_key
that is passed when the W&B run is initialized is not accessible. The wandb.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. Replacehyperparameter_name
with the name of your hyperparameter and any values enclosed in <>
.
config.yaml
!!float
operator, which casts the value to a floating point number. For example, min: !!float 1e-5
. See Command example.
Sweep configuration examples
- CLI
- Python script or notebook
config.yaml
Bayes hyperband example
early_terminate
:
- Maximum number of iterations
- Minimum number of iterations
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
For more complex command line arguments, you can use macros to pass environment variables, the Python interpreter, and additional arguments. W&B supports pre defined 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
run.py
) can then parse these command line arguments using the argparse
module.
run.py
Boolean arguments
Theargparse
module does not support boolean arguments by default. To define a boolean argument, you can use the action
parameter or use a custom function to convert the string representation of the boolean value to a boolean type.
As an example, you can use the following code snippet to define a boolean argument. Pass store_true
or store_false
as an argument to ArgumentParser
.
str2bool
function, which converts a string to a boolean value.