Skip to main content
This guide shows how to add W&B Sweeps to an existing Python training script or notebook for hyperparameter optimization. You’ll start with an example training script, then adapt it to explore hyperparameter values, log metrics, and identify the best-performing configuration.

Original training script

Suppose you have a Python script that trains a model (see the following code). Your goal is to find the hyperparameters that maximize the validation accuracy (val_acc). In your Python script, you define two functions: train_one_epoch() and evaluate_one_epoch(). The train_one_epoch() function simulates training for one epoch and returns the training accuracy and loss. The evaluate_one_epoch() function simulates evaluation of the model on the validation data set and returns the validation accuracy and loss. You define a configuration dictionary named config that contains hyperparameter values such as the learning rate, batch size, and number of epochs. The values in the configuration dictionary control the training process. Next, you define a function called main that mimics a typical training loop. For each epoch, the script computes the accuracy and loss on the training and validation data sets.
This code is a mock training script. It doesn’t train a model, but simulates the training process by generating random accuracy and loss values. The purpose of this code is to demonstrate how to integrate W&B into your training script.
train.py
The following section shows how to add W&B to your Python script to track hyperparameters and metrics during training. The goal is to find the best hyperparameters that maximize the validation accuracy (val_acc).

Add W&B to your training script

This section shows how to modify the original training script so that the sweep agent can pass hyperparameter values into each run and W&B can record the resulting metrics. How you integrate W&B into your Python script or notebook depends on how you manage sweeps. To use the W&B Python SDK to start, stop, and manage sweeps, follow the instructions in the Python script or notebook tab. To use the W&B CLI instead, follow the instructions in the CLI tab.
Create a YAML configuration file with your sweep configuration. W&B uses this file to determine which hyperparameters and metric to optimize.Add the name of your Python script to the program key in the YAML file on line 1.
The sweep agent selects a value from the values list and passes it to the run config in the training script. For example, if you define the batch_size parameter with the values [16, 32, 64], the sweep agent selects one of those values and passes it to the training script as run.config.batch_size.
The following YAML file replicates to the config values in the Python script (see line 15) shown earlier. The YAML file defines the batch_size, lr, and epochs hyperparameters and specifies the values to try for each one on lines 8–14. On line 5, the YAML file configures the sweep to maximize val_acc.
config.yaml
For more information, see Define sweep configuration.After you define your sweep configuration in a YAML file, add W&B to your training script so that each sweep run can use the hyperparameters selected by the sweep agent and log the metric you want to optimize.Within your training script, add the following code snippets to integrate W&B:
  1. Import the W&B Python SDK (wandb).
  2. Initialize a run with wandb.init().
  3. Access the hyperparameter values from wandb.Run.config so that your script uses the suggested arguments for each run instead of hard-coded values.
  4. Log the metric you want to optimize with wandb.Run.log().
The following code snippet shows how to integrate W&B into your training script. When the sweep agent runs this script, it passes the selected hyperparameter values to wandb.Run.config for that run.
train.py
When you create and manage a sweep with the W&B CLI, do not read the sweep configuration file from your training script. Pass the YAML file to wandb sweep when you create the sweep. When a sweep agent starts a run, it automatically populates wandb.Run.config with the selected hyperparameter values.If you run the training script directly with python train.py, no sweep agent is present to populate those values. As a result, keys such as wandb.Run.config["lr"] are unavailable.
  1. Initialize the sweep with the wandb sweep command. Provide the name of the YAML file. Optionally, set the --project flag to the project name:
  2. Copy the sweep ID. Replace the placeholder values (entity_name, project_name, sweep_id) in the following command with your W&B entity, project, and sweep ID, then run wandb agent to start the sweep agent:
    If you want to limit how many sweep runs the agent executes, specify an integer with --count:
For more information, see Start a sweep agent.
Logging metrics to W&B in a sweepYou must name the metric you want to optimize in your sweep configuration and log that same metric key with wandb.Run.log(). For example, if you define the metric to optimize as val_acc within your sweep configuration, you must also log val_acc to W&B. If you don’t log the metric, W&B can’t perform optimization.
The following is an incorrect example of logging the metric to W&B. The sweep configuration optimizes for val_acc, but the code logs val_acc within a nested dictionary under the key validation. You must log the metric directly, not within a nested dictionary.