Track experiments
7 minute read
Use W&B for machine learning experiment tracking, model checkpointing, collaboration with your team and more.
In this notebook, you will create and track a machine learning experiment using a simple PyTorch model. By the end of the notebook, you will have an interactive project dashboard that you can share and customize with other members of your team. View an example dashboard here.
Prerequisites
Install the W&B Python SDK and log in:
Simulate and track a machine learning experiment with W&B
Create, track, and visualize a machine learning experiment. To do this:
- Initialize a W&B run and pass in the hyperparameters you want to track.
- Within your training loop, log metrics such as the accuruacy and loss.
import random
import math
# Launch 5 simulated experiments
total_runs = 5
for run in range(total_runs):
# 1️. Start a new run to track this script
wandb.init(
# Set the project where this run will be logged
project="basic-intro",
# We pass a run name (otherwise it’ll be randomly assigned, like sunshine-lollypop-10)
name=f"experiment_{run}",
# Track hyperparameters and run metadata
config={
"learning_rate": 0.02,
"architecture": "CNN",
"dataset": "CIFAR-100",
"epochs": 10,
})
# This simple block simulates a training loop logging metrics
epochs = 10
offset = random.random() / 5
for epoch in range(2, epochs):
acc = 1 - 2 ** -epoch - random.random() / epoch - offset
loss = 2 ** -epoch + random.random() / epoch + offset
# 2️. Log metrics from your script to W&B
wandb.log({"acc": acc, "loss": loss})
# Mark the run as finished
wandb.finish()
View how your machine learning peformed in your W&B project. Copy and paste the URL link that is printed from the previous cell. The URL will redirect you to a W&B project that contains a dashboard showing graphs the show how
The following image shows what a dashboard can look like:

Now that we know how to integrate W&B into a psuedo machine learning training loop, let’s track a machine learning experiment using a basic PyTorch neural network. The following code will also upload model checkpoints to W&B that you can then share with other teams in your organization.
Track a machine learning experiment using Pytorch
The following code cell defines and trains a simple MNIST classifier. During training, you will see W&B prints out URLs. Click on the project page link to see your results stream in live to a W&B project.
W&B runs automatically log metrics, system information, hyperparameters, terminal output and you’ll see an interactive table with model inputs and outputs.
Set up PyTorch Dataloader
The following cell defines some useful functions that we will need to train our machine learning model. The functions themselves are not unique to W&B so we’ll not cover them in detail here. See the PyTorch documentation for more information on how to define forward and backward training loop, how to use PyTorch DataLoaders to load data in for training, and how define PyTorch models using the torch.nn.Sequential
Class.
Create a teble to compare the predicted values versus the true value
The following cell is unique to W&B, so let’s go over it.
In the cell we define a function called log_image_table
. Though technically, optional, this function creates a W&B Table object. We will use the table object to create a table that shows what the model predicted for each image.
More specifically, each row will conists of the image fed to the model, along with predicted value and the actual value (label).
Train your model and upload checkpoints
The following code trains and saves model checkpoints to your project. Use model checkpoints like you normally would to assess how the model performed during training.
W&B also makes it easy to share your saved models and model checkpoints with other members of your team or organization. To learn how to share your model and model checkpoints with members outside of your team, see W&B Registry.
You have now trained your first model using W&B. Click on one of the links above to see your metrics and see your saved model checkpoints in the Artifacts tab in the W&B App UI
(Optional) Set up a W&B Alert
Create a W&B Alerts to send alerts to your Slack or email from your Python code.
There are 2 steps to follow the first time you’d like to send a Slack or email alert, triggered from your code:
- Turn on Alerts in your W&B User Settings
- Add
wandb.alert()
to your code. For example:
The following cell shows a minimal example below to see how to use wandb.alert
You can find the full docs for W&B Alerts here.
Next steps
The next tutorial you will learn how to do hyperparameter optimization using W&B Sweeps: Hyperparameters sweeps using PyTorch
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.