Skip to main content

Define sweep configuration

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 your strategy in the form of a sweep configuration. Specify the configuration either in a:

  1. Python nested dictionary data structure if you use a Jupyter Notebook or Python script.
  2. YAML file if you use the command line (CLI).

The following code snippets demonstrate examples of how to define a Sweep configuration in a Jupyter Notebook or Python script or within a YAML file. Configuration keys are defined in detail in subsequent sections.

Within your Jupyter Notebook or Python script, define a sweep in a dictionary Python data structure.
sweep_configuration = {
"method": "bayes",
"name": "sweep",
"metric": {"goal": "minimize", "name": "validation_loss"},
"parameters": {
"batch_size": {"values": [16, 32, 64]},
"epochs": {"values": [5, 10, 15]},
"lr": {"max": 0.1, "min": 0.0001},
},
}
caution
  1. Ensure that you log (wandb.log) the exact metric name that you defined the sweep to optimize within your Python script or Jupyter Notebook.
  2. You cannot change the Sweep configuration once you start the W&B Sweep agent.

For example, suppose you want W&B Sweeps to maximize the validation accuracy during training. Within your Python script you store the validation accuracy in a variable val_loss. In your YAML configuration file you define this as:

metric:
goal: maximize
name: val_loss

You must log the variable val_loss (in this example) within your Python script or Jupyter Notebook to W&B.

wandb.log({"val_loss": validation_loss})

Defining the metric in the sweep configuration is only required when using the bayes method for the sweep.

Sweep configuration structure

Sweep configurations are nested; keys can have, as their values, further keys. The top-level keys are listed and briefly described below, and then detailed in the following section.

Top-Level KeyDescription
program(required) Training script to run.
method(required) Specify the search strategy.
parameters(required) Specify parameters bounds to search.
nameThe name of the sweep, displayed in the W&B UI.
descriptionText description of the sweep.
metricSpecify the metric to optimize (only used by certain search strategies and stopping criteria).
early_terminateSpecify any early stopping criteria.
commandSpecify command structure for invoking and passing arguments to the training script.
projectSpecify the project for this sweep.
entitySpecify the entity for this sweep.
run_capSpecify a maximum number of runs in a sweep.

Search type methods

The following list describes hyperparameter search methods. Specify the search strategy with the method:

  • grid – Iterate over every combination of hyperparameter values. Can be computationally costly.
  • random – Choose a random set of hyperparameter values on each iteration based on provided distributions.
  • bayes – Create a probabilistic model of a metric score as a function of the hyperparameters, and choose parameters with high probability of improving the metric. Bayesian hyperparameter search method uses a Gaussian Process to model the relationship between the parameters and the model metric and chooses parameters to optimize the probability of improvement. This strategy requires the metrickey to be specified. Works well for small numbers of continuous parameters but scales poorly.
method: random
caution

Random and Bayesian searches will run forever -- until you stop the process from the command line, within your python script, or the UI. Grid search will also run forever if it searches within in a continuous search space.

Configuration keys

method

Specify the search strategy with the method key in the sweep configuration.

methodDescription
gridGrid search iterates over all possible combinations of parameter values.
randomRandom search chooses a random set of values on each iteration.
bayesOur Bayesian hyperparameter search method uses a Gaussian Process to model the relationship between the parameters and the model metric and chooses parameters to optimize the probability of improvement. This strategy requires the metric key to be specified.

parameters

Describe the hyperparameters to explore during the sweep. For each hyperparameter, specify the name and the possible values as a list of constants (for any method) or specify a distribution for random or bayes.

ValuesDescription
valuesSpecifies all valid values for this hyperparameter. Compatible with grid.
valueSpecifies the single valid value for this hyperparameter. Compatible with grid.
distribution(str) Selects a distribution from the distribution table below. If not specified, will default to categorical if values is set, to int_uniform if max and min are set to integers, to uniform if max and min are set to floats, or toconstant if value is set.
probabilitiesSpecify the probability of selecting each element of values when using random.
min, max(intor float) Maximum and minimum values. If int, for int_uniform -distributed hyperparameters. If float, for uniform -distributed hyperparameters.
mu(float) Mean parameter for normal - or lognormal -distributed hyperparameters.
sigma(float) Standard deviation parameter for normal - or lognormal -distributed hyperparameters.
q(float) Quantization step size for quantized hyperparameters.
parametersNest other parameters inside a root level parameter.

Examples

parameter_name:
value: 1.618

distribution

Specify how to distribute values if you choose a random (random) or Bayesian (bayes) search method.

ValueDescription
constantConstant distribution. Must specify value.
categoricalCategorical distribution. Must specify values.
int_uniformDiscrete uniform distribution on integers. Must specify max and min as integers.
uniformContinuous uniform distribution. Must specify max and min as floats.
q_uniformQuantized uniform distribution. Returns round(X / q) * q where X is uniform. q defaults to 1.
log_uniformLog-uniform distribution. Returns a value X between exp(min) and exp(max)such that the natural logarithm is uniformly distributed between min and max.
log_uniform_valuesLog-uniform distribution. Returns a value X between min and max such that log(X) is uniformly distributed between log(min) and log(max).
q_log_uniformQuantized log uniform. Returns round(X / q) * q where X is log_uniform. q defaults to 1.
q_log_uniform_valuesQuantized log uniform. Returns round(X / q) * q where X is log_uniform_values. q defaults to 1.
inv_log_uniformInverse log uniform distribution. Returns X, where log(1/X) is uniformly distributed between min and max.
inv_log_uniform_valuesInverse log uniform distribution. Returns X, where log(1/X) is uniformly distributed between log(1/max) and log(1/min).
normalNormal distribution. Return value is normally-distributed with mean mu (default 0) and standard deviation sigma (default 1).
q_normalQuantized normal distribution. Returns round(X / q) * q where X is normal. Q defaults to 1.
log_normalLog normal distribution. Returns a value X such that the natural logarithm log(X) is normally distributed with mean mu (default 0) and standard deviation sigma (default 1).
q_log_normalQuantized log normal distribution. Returns round(X / q) * q where X is log_normal. q defaults to 1.

Examples

parameter_name:
distribution: constant
value: 2.71828

metric

Describes the metric to optimize. This metric should be logged explicitly to W&B by your training script.

KeyDescription
nameName of the metric to optimize.
goalEither minimize or maximize (Default is minimize).
targetGoal value for the metric you're optimizing. When any run in the sweep achieves that target value, the sweep's state will be set to finished. This means all agents with active runs will finish those jobs, but no new runs will be launched in the sweep.

For example, if you want to minimize the validation loss of your model:

# model training code that returns validation loss as valid_loss
wandb.log({"val_loss": valid_loss})

Examples

metric:
name: val_acc
goal: maximize
caution

The metric you optimize must be a top-level metric.

Do not log the metric for your sweep inside of a sub-directory. In the proceeding code example, we want to log the validation loss ("loss": val_loss). First we define it in a dictionary. However, the dictionary passed to wandb.log does not specify the key-value pair to track.

val_metrics = {
"loss": val_loss,
"acc": val_acc
}

# Incorrect. Dictionary key-value paired is not provided.
wandb.log({"val_loss", val_metrics})

Instead, log the metric at the top level. For example, after you create a dictionary, specify the key-value pair when you pass the dictionary to the wandb.log method:

val_metrics = {
"loss": val_loss,
"acc": val_acc
}

wandb.log({"val_loss", val_metrics["loss"]})

early_terminate

Early termination is an optional feature that speeds up hyperparameter search by stopping poorly-performing runs. When the early stopping is triggered, the agent stops the current run and gets the next set of hyperparameters to try.

KeyDescription
typeSpecify the stopping algorithm

We support the following stopping algorithm(s):

typeDescription
hyperbandUse the hyperband method.

hyperband

Hyperband stopping evaluates if a program should be stopped or permitted to continue at one or more pre-set iteration counts, called "brackets". When a run reaches a bracket, its metric value is compared to all previous reported metric values and the W&B Run is terminated if its value is too high (when the goal is minimization) or low (when the goal is maximization).

Brackets are based on the number of logged iterations. The number of brackets corresponds to the number of times you log the metric you are optimizing. The iterations can correspond to steps, epochs, or something in between. The numerical value of the step counter is not used in bracket calculations.

caution

Specify either min_iter or max_iter to create a bracket schedule.

KeyDescription
min_iterSpecify the iteration for the first bracket
max_iterSpecify the maximum number of iterations.
sSpecify the total number of brackets (required for max_iter)
etaSpecify the bracket multiplier schedule (default: 3).
strictEnable 'strict' mode that prunes runs aggressively, more closely following the original Hyperband paper. Defaults to false.
info

The hyperband early terminator checks what W&B Runs to terminate once every few minutes. The end run timestamp might differ from the specified brackets if your run or iteration are short.

Examples

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].

command

/usr/bin/env python train.py --param1=value1 --param2=value2
info

On UNIX systems, /usr/bin/env ensures the right Python interpreter is chosen based on the environment.

The format and contents can be modified by specifying values under the command key. Fixed components of the command, such as filenames, can be included directly (see examples below).

We support the following macros for variable components of the command:

Command MacroDescription
${env}/usr/bin/env on UNIX systems, omitted on Windows.
${interpreter}Expands to python.
${program}Training script filename specified by the sweep configuration program key.
${args}Hyperparameters and their values in the form --param1=value1 --param2=value2.
${args_no_boolean_flags}Hyperparameters and their values in the form --param1=value1 except boolean parameters are in the form --boolean_flag_param when True and omitted when False.
${args_no_hyphens}Hyperparameters and their values in the form param1=value1 param2=value2.
${args_json}Hyperparameters and their values encoded as JSON.
${args_json_file}The path to a file containing the hyperparameters and their values encoded as JSON.
${envvar}A way to pass environment variables. ${envvar:MYENVVAR} expands to the value of MYENVVAR environment variable.

The default command format is defined as:

command:
- ${env}
- ${interpreter}
- ${program}
- ${args}

Examples

Remove the {$interpreter} macro and provide a value explicitly in order to hardcode the python interpreter. For example, the following code snippet demonstrates how to do this:

command:
- ${env}
- python3
- ${program}
- ${args}
Was this page helpful?👍👎