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
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 sweepname, 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.
- CLI
- Python script or notebook
To manage sweeps interactively from the command line, define a sweep configuration in a YAML file.
config.yaml
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 additionalparameters 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:
- CLI
- Python script or notebook
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. Replacehyperparameter_name with the name of your hyperparameter and any values enclosed in brackets.
config.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.- CLI
- Python script or notebook
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.early_terminate:
- Minimum number of iterations
- Maximum 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
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
run.py can then parse these command-line arguments using the argparse module:
run.py
Boolean arguments
If your sweep passes boolean flags through command arguments, your training script needs extra handling becauseargparse 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:
str2bool function, which converts a string to a boolean value: