Basic structure
Sweep configurations use key-value pairs and nested structures. You can define your sweep configuration in a YAML file or in a Python dictionary. The structure of the sweep configuration is the same regardless of where you define it. Top-level keys 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.
The values associated with each key can be a string, a number, a list, or another nested key-value pair. The value type depends on the key.
For example, the following code snippet shows a sweep configuration with the method, metric, and parameters keys. The method key specifies the search strategy (bayes). The metric key specifies the metric to optimize and whether to minimize or maximize it. The parameters key specifies the hyperparameters to optimize and their values or distributions.
- CLI
- Python script or notebook
The following code snippet shows how to define a sweep configuration in a YAML file named Within the top level
config.yaml:config.yaml
parameters key (line 7), the following keys are nested: learning_rate (line 8), batch_size (line 11), epochs (line 14), and optimizer (line 17). For each of the nested keys you specify, you can provide one or more values, a distribution, a probability, and more.See Define sweep configuration options for a comprehensive list of top-level sweep configuration keys and their associated values.
Double-nested parameters
Sweep configurations support nested parameters. Double nested parameters are useful for organizing your hyperparameters into categories. For example, you can group hyperparameters related to the optimizer under anoptimizer category and group hyperparameters related to the model architecture under a model category.
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:
- CLI
- Python script or notebook
config.yaml
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: