How to use Weights and Biases for experiment tracking
Quick answer
Use
Weights and Biases (wandb) by installing the wandb Python package, initializing a run with wandb.init(), and logging metrics or artifacts with wandb.log(). This enables automatic experiment tracking, visualization, and collaboration.PREREQUISITES
Python 3.8+pip install wandbWeights and Biases account (free tier available)Basic Python knowledge
Setup
Install the wandb package and log in to your W&B account to enable experiment tracking.
pip install wandb
wandb login output
Enter your API key from https://wandb.ai/authorize: Successfully logged in to Weights & Biases!
Step by step
This example shows a simple training loop where metrics are logged to W&B for tracking.
import wandb
# Initialize a new W&B run
wandb.init(project="my-ml-project", entity="your-username")
for epoch in range(3):
# Simulate training metrics
loss = 0.1 * (3 - epoch)
accuracy = 0.8 + 0.05 * epoch
# Log metrics to W&B
wandb.log({"epoch": epoch, "loss": loss, "accuracy": accuracy})
wandb.finish() output
wandb: Currently logged in as: your-username wandb: Tracking run with id: <run_id> wandb: Run summary and metrics logged successfully.
Common variations
- Use
wandb.init(resume=True)to resume interrupted runs. - Log model checkpoints or datasets as artifacts with
wandb.Artifact. - Integrate with frameworks like PyTorch Lightning or TensorFlow using built-in W&B callbacks.
- Use asynchronous logging or customize the dashboard with tags and notes.
Troubleshooting
- If you see
wandb: ERROR API key not found, runwandb loginagain and verify your API key. - For network issues, check your firewall or proxy settings blocking W&B servers.
- If metrics don’t appear, ensure
wandb.log()is called inside your training loop.
Key Takeaways
- Install and authenticate with
wandb loginbefore tracking experiments. - Initialize runs with
wandb.init()and log metrics usingwandb.log()inside training loops. - Use artifacts to track datasets and model files for reproducibility.
- Leverage integrations with popular ML frameworks for streamlined tracking.
- Troubleshoot common issues by verifying API keys and network connectivity.