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 define a nested parameter, include an additionalparameters key under the top-level parameter name.
The following example shows a sweep configuration with three nested parameters: nested_category_1, nested_category_2, and nested_category_3. Each nested parameter includes two additional parameters: momentum and weight_decay.
nested_category_1, nested_category_2, and nested_category_3 are placeholders. Replace them with names that fit your use case.- CLI
- Python script or notebook
Nested parameters defined in sweep configuration overwrite keys specified in a W&B run configuration.As an example, suppose you have Your sweep configuration defines nested parameters under a top-level During a sweep run,
train.py script that initializes a run with a nested default:"parameters" key:run.config["nested_param"] reflects the subtree defined by the
sweep (learning_rate, double_nested_param) config and does not include manual_key defined
in wandb.init(config=...).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.