- Set up your training code
- Define the search space with a sweep configuration
- Initialize the sweep
- Start the sweep agent
Set up your training code
Define a training function that takes in hyperparameter values fromwandb.Run.config
and uses them to train a model and return metrics.
Optionally provide the name of the project where you want the output of the W&B Run to be stored (project parameter in wandb.init()
). If the project is not specified, the run is put in an “Uncategorized” project.
Both the sweep and the run must be in the same project. Therefore, the name you provide when you initialize W&B must match the name of the project you provide when you initialize a sweep.
Define the search space with a sweep configuration
Specify the hyperparameters to sweep in a dictionary. For configuration options, see Define sweep configuration. The proceeding example demonstrates a sweep configuration that uses a random search ('method':'random'
). The sweep will randomly select a random set of values listed in the configuration for the batch size, epoch, and the learning rate.
W&B minimizes the metric specified in the metric
key when "goal": "minimize"
is associated with it. In this case, W&B will optimize for minimizing the metric score
("name": "score"
).
Initialize the Sweep
W&B uses a Sweep Controller to manage sweeps on the cloud (standard), locally (local) across one or more machines. For more information about Sweep Controllers, see Search and stop algorithms locally. A sweep identification number is returned when you initialize a sweep:Start the Sweep
Use thewandb.agent()
API call to start a sweep.
MultiprocessingYou must wrap your Wrapping your code with this convention ensures that it is only executed when the script is run directly, and not when it is imported as a module in a worker process.See Python standard library
wandb.agent()
and wandb.sweep()
calls with if __name__ == '__main__':
if you use Python standard library’s multiprocessing
or PyTorch’s pytorch.multiprocessing
package. For example:multiprocessing
or PyTorch multiprocessing
for more information about multiprocessing. See https://realpython.com/if-name-main-python/ for information about the if __name__ == '__main__':
convention.Visualize results (optional)
Open your project to see your live results in the W&B App dashboard. With just a few clicks, construct rich, interactive charts like parallel coordinates plots, parameter importance analyzes, and additional chart types.
Stop the agent (optional)
In the terminal, pressCtrl+C
to stop the current run. Press it again to terminate the agent.