Skip to main content
Try in Colab Use W&B for machine learning experiment tracking, dataset versioning, and project collaboration.
Benefits of using W&B
Use W&B Sweeps to automate hyperparameter optimization and explore model possibilities with interactive dashboards:
TensorFlow hyperparameter sweep results

Why use sweeps

  • Quick setup: Run W&B sweeps with a few lines of code.
  • Transparent: The project cites all algorithms used, and the code is open source.
  • Powerful: Sweeps provide customization options and can run on multiple machines or a laptop with ease.
For more information, see the Sweeps overview.

What this notebook covers

  • Steps to start with W&B Sweep and a custom training loop in TensorFlow.
  • Finding best hyperparameters for image classification tasks.
Note: Sections starting with Step show necessary code to perform a hyperparameter sweep. The rest sets up a simple example.

Install, import, and log in

Install W&B

Import W&B and log in

If you are new to W&B or not logged in, the link after running wandb.login() directs to the sign-up/login page.

Prepare dataset

Build a classifier MLP

Write a training loop

Configure the sweep

Steps to configure the sweep:
  • Define the hyperparameters to optimize
  • Choose the optimization method: random, grid, or bayes
  • Set a goal and metric for bayes, like minimizing val_loss
  • Use hyperband for early termination of performing runs
See more in the sweep configuration guide.

Wrap the training loop

Create a function, like sweep_train, which uses run.config() to set hyperparameters before calling train.

Initialize sweep and run personal digital assistant

Limit the number of runs with the count parameter. Set to 10 for quick execution. Increase as needed.

Visualize results

Click on the Sweep URL link preceding to view live results. Explore projects tracked and visualized with W&B in the Gallery.

Best practices

  1. Projects: Log multiple runs to a project to compare them. wandb.init(project="project-name")
  2. Groups: Log each process as a run for multiple processes or cross-validation folds, and group them. wandb.init(group='experiment-1')
  3. Tags: Use tags to track your baseline or production model.
  4. Notes: Enter notes in the table to track changes between runs.
  5. Reports: Use reports for progress notes, sharing with colleagues, and creating ML project dashboards and snapshots.

Advanced setup

  1. Environment variables: Set API keys for training on a managed cluster.
  2. Offline mode
  3. On-prem: Install W&B in a private cloud or air-gapped servers in your infrastructure. Local installations suit academics and enterprise teams.