Try in Colab

Sweeps: An Overview
Running a hyperparameter sweep with W&B is very easy. There are just 3 simple steps:- Define the sweep: we do this by creating a dictionary or a YAML file that specifies the parameters to search through, the search strategy, the optimization metric et all.
-
Initialize the sweep: with one line of code we initialize the sweep and pass in the dictionary of sweep configurations:
sweep_id = wandb.sweep(sweep_config)
-
Run the sweep agent: also accomplished with one line of code, we call
wandb.agent()
and pass thesweep_id
to run, along with a function that defines your model architecture and trains it:wandb.agent(sweep_id, function=train)
Before you get started
Install W&B and import the W&B Python SDK into your notebook:- Install with
!pip install
:
- Import W&B:
- Log in to W&B and provide your API key when prompted:
Step 1️: Define a sweep
A W&B Sweep combines a strategy for trying numerous hyperparameter values with the code that evaluates them. Before you start a sweep, you must define your sweep strategy with a sweep configuration.The sweep configuration you create for a sweep must be in a nested dictionary if you start a sweep in a Jupyter Notebook.If you run a sweep within the command line, you must specify your sweep config with a YAML file.
Pick a search method
First, specify a hyperparameter search method within your configuration dictionary. There are three hyperparameter search strategies to choose from: grid, random, and Bayesian search. For this tutorial, you will use a random search. Within your notebook, create a dictionary and specifyrandom
for the method
key.
Specify hyperparameters to search through
Now that you have a search method specified in your sweep configuration, specify the hyperparameters you want to search over. To do this, specify one or more hyperparameter names to theparameter
key and specify one or more hyperparameter values for the value
key.
The values you search through for a given hyperparameter depend on the type of hyperparameter you are investigating.
For example, if you choose a machine learning optimizer, you must specify one or more finite optimizer names such as the Adam optimizer and stochastic gradient dissent.
epochs
is set to 1.
random
search,
all the values
of a parameter are equally likely to be chosen on a given run.
Alternatively,
you can specify a named distribution
,
plus its parameters, like the mean mu
and standard deviation sigma
of a normal
distribution.
sweep_config
is a nested dictionary
that specifies exactly which parameters
we’re interested in trying
and the method
we’re going to use to try them.
Let’s see how the sweep configuration looks like:
For hyperparameters that have potentially infinite options,
it usually makes sense to try out
a few select
values
. For example, the preceding sweep configuration has a list of finite values specified for the layer_size
and dropout
parameter keys.Step 2️: Initialize the Sweep
Once you’ve defined the search strategy, it’s time to set up something to implement it. W&B uses a Sweep Controller to manage sweeps on the cloud or locally across one or more machines. For this tutorial, you will use a sweep controller managed by W&B. While sweep controllers manage sweeps, the component that actually executes a sweep is known as a sweep agent.By default, sweep controllers components are initiated on W&B’s servers and sweep agents, the component that creates sweeps, are activated on your local machine.
wandb.sweep
method. Pass your sweep configuration dictionary you defined earlier to the sweep_config
field:
wandb.sweep
function returns a sweep_id
that you will use at a later step to activate your sweep.
On the command line, this function is replaced with
Step 3: Define your machine learning code
Before you execute the sweep, define the training procedure that uses the hyperparameter values you want to try. The key to integrating W&B Sweeps into your training code is to ensure that, for each training experiment, that your training logic can access the hyperparameter values you defined in your sweep configuration. In the proceeding code example, the helper functionsbuild_dataset
, build_network
, build_optimizer
, and train_epoch
access the sweep hyperparameter configuration dictionary.
Run the proceeding machine learning training code in your notebook. The functions define a basic fully connected neural network in PyTorch.
train
function, you will notice the following W&B Python SDK methods:
wandb.init()
: Initialize a new W&B Run. Each run is a single execution of the training function.run.config
: Pass sweep configuration with the hyperparameters you want to experiment with.run.log()
: Log the training loss for each epoch.
build_dataset
, build_network
, build_optimizer
, and train_epoch
.
These functions are a standard part of a basic PyTorch pipeline,
and their implementation is unaffected by the use of W&B.
Step 4: Activate sweep agents
Now that you have your sweep configuration defined and a training script that can utilize those hyperparameter in an interactive way, you are ready to activate a sweep agent. Sweep agents are responsible for running an experiment with a set of hyperparameter values that you defined in your sweep configuration. Create sweep agents with thewandb.agent
method. Provide the following:
- The sweep the agent is a part of (
sweep_id
) - The function the sweep is supposed to run. In this example, the sweep will use the
train
function. - (optionally) How many configs to ask the sweep controller for (
count
)
You can start multiple sweep agents with the same
sweep_id
on different compute resources. The sweep controller ensures that they work together
according to the sweep configuration you defined.train
) 5 times:
Since the
random
search method was specified in the sweep configuration, the sweep controller provides randomly generated hyperparameter values.Visualize Sweep Results
Parallel Coordinates Plot
This plot maps hyperparameter values to model metrics. It’s useful for honing in on combinations of hyperparameters that led to the best model performance.
Hyperparameter Importance Plot
The hyperparameter importance plot surfaces which hyperparameters were the best predictors of your metrics. We report feature importance (from a random forest model) and correlation (implicitly a linear model).