Add W&B (wandb) to your code
8 minute read
There are numerous ways to add the W&B Python SDK to your script or notebook. This section provides a “best practice” example that shows how to integrate the W&B Python SDK into your own code.
Original training script
Suppose you have the following code in a Python script. We define a function called main
that mimics a typical training loop. For each epoch, the accuracy and loss is computed on the training and validation data sets. The values are randomly generated for the purpose of this example.
We defined a dictionary called config
where we store hyperparameters values. At the end of the cell, we call the main
function to execute the mock training code.
Training script with W&B Python SDK
The following code examples demonstrate how to add the W&B Python SDK into your code. If you start W&B Sweep jobs in the CLI, you will want to explore the CLI tab. If you start W&B Sweep jobs within a Jupyter notebook or Python script, explore the Python SDK tab.
To create a W&B Sweep, we added the following to the code example:
- Import the Weights & Biases Python SDK.
- Create a dictionary object where the key-value pairs define the sweep configuration. In the proceeding example, the batch size (
batch_size
), epochs (epochs
), and the learning rate (lr
) hyperparameters are varied during each sweep. For more information on how to create a sweep configuration, see Define sweep configuration. - Pass the sweep configuration dictionary to
wandb.sweep
. This initializes the sweep. This returns a sweep ID (sweep_id
). For more information on how to initialize sweeps, see Initialize sweeps. - Use the
wandb.init()
API to generate a background process to sync and log data as a W&B Run. - (Optional) define values from
wandb.config
instead of defining hard coded values. - Log the metric we want to optimize with
wandb.log
. You must log the metric defined in your configuration. Within the configuration dictionary (sweep_configuration
in this example) we defined the sweep to maximize theval_acc
value. - Start the sweep with the
wandb.agent
API call. Provide the sweep ID, the name of the function the sweep will execute (function=main
), and set the maximum number of runs to try to four (count=4
). For more information on how to start W&B Sweep, see Start sweep agents.
wandb.init()
API within a with
context manager statement to generate a background process to sync and log data
as a W&B Run. This ensures the run is
properly terminated after uploading the logged values. An alternative approach
is to call wandb.init()
and wandb.finish()
at the beginning and end of the
training script, respectively.To create a W&B Sweep, we first create a YAML configuration file. The
configuration file contains he hyperparameters we want the sweep to explore. In
the proceeding example, the batch size (batch_size
), epochs (epochs
), and
the learning rate (lr
) hyperparameters are varied during each sweep.
For more information on how to create a W&B Sweep configuration, see Define sweep configuration.
You must provide the name of your Python script for the program
key
in your YAML file.
Next, we add the following to the code example:
- Import the Wieghts & Biases Python SDK (
wandb
) and PyYAML (yaml
). PyYAML is used to read in our YAML configuration file. - Read in the configuration file.
- Use the
wandb.init()
API to generate a background process to sync and log data as a W&B Run. We pass the config object to the config parameter. - Define hyperparameter values from
wandb.config
instead of using hard coded values. - Log the metric we want to optimize with
wandb.log
. You must log the metric defined in your configuration. Within the configuration dictionary (sweep_configuration
in this example) we defined the sweep to maximize theval_acc
value.
In your CLI, set a maximum number of runs for the sweep agent to try. This is optional. This example we set the maximum number to 5.
Next, initialize the sweep with the wandb sweep
command. Provide the name of the YAML file. Optionally provide the name of the project for the project flag (--project
):
This returns a sweep ID. For more information on how to initialize sweeps, see Initialize sweeps.
Copy the sweep ID and replace sweepID
in the proceeding code snippet to start
the sweep job with the wandb agent
command:
For more information, see Start sweep jobs.
Consideration when logging metrics
Be sure to log the sweep’s metric to W&B explicitly. Do not log metrics for your sweep inside a subdirectory.
For example, consider the proceeding psuedocode. A user wants to log the validation loss ("val_loss": loss
). First they pass the values into a dictionary. However, the dictionary passed to wandb.log
does not explicitly access the key-value pair in the dictionary:
Instead, explicitly access the key-value pair within the Python dictionary. For example, the proceeding code specifies the key-value pair when you pass the dictionary to the wandb.log
method:
Feedback
Was this page helpful?
Glad to hear it! If you have further feedback, please let us know.
Sorry to hear that. Please tell us how we can improve.