Skip to main content
Try in Colab 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:
  1. Initialize a run and pass in the hyperparameters you want to track.
  2. Within your training loop, log metrics such as the accuracy and loss.
View how your machine learning performed 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 that show how your model performed. The following image shows what a dashboard can look like:
W&B experiment tracking dashboard
Now that we know how to integrate W&B into a pseudo 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 table 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 consists 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:
  1. Turn on Alerts in your W&B User Settings
  2. Add run.alert() to your code. For example:
The following cell shows a minimal example below to see how to use run.alert()
For more details, see the W&B Alerts overview.

Next steps

The next tutorial you will learn how to do hyperparameter optimization using W&B Sweeps: Hyperparameters sweeps using PyTorch