How to Use wandb for Tracking Machine Learning Experiments
To use
wandb for tracking, first initialize a project with wandb.init(). Then log metrics or parameters during training using wandb.log(). This helps you monitor and visualize your model's progress easily.Syntax
Here is the basic syntax to use wandb for tracking your machine learning experiments:
wandb.init(project='project_name'): Starts a new run in the specified project.wandb.config: Stores hyperparameters or settings.wandb.log({'metric_name': value}): Logs metrics like loss or accuracy during training.wandb.finish(): Ends the run and uploads data.
python
import wandb # Start a new run wandb.init(project='my_project') # Set hyperparameters wandb.config.learning_rate = 0.01 # Log metrics wandb.log({'loss': 0.5, 'accuracy': 0.8}) # Finish the run wandb.finish()
Example
This example shows how to track a simple training loop with wandb. It logs loss and accuracy for 5 steps.
python
import wandb import random # Initialize wandb run wandb.init(project='example_project') # Set hyperparameters wandb.config.batch_size = 32 wandb.config.epochs = 1 # Simulate training loop for step in range(5): loss = random.uniform(0.1, 1.0) # fake loss accuracy = random.uniform(0.5, 1.0) # fake accuracy wandb.log({'loss': loss, 'accuracy': accuracy, 'step': step}) # Finish run wandb.finish()
Output
wandb: Currently logged in as: <your_username>
wandb: Tracking run with wandb version 0.15.4
wandb: Run data is saved locally and will be synced to the cloud.
wandb: Run URL: https://wandb.ai/<your_username>/example_project/runs/<run_id>
Common Pitfalls
Common mistakes when using wandb include:
- Not calling
wandb.init()before logging, which causes errors. - Forgetting to call
wandb.finish(), so data may not upload properly. - Logging metrics without unique step values, making plots confusing.
- Not setting a project name, which scatters runs across default projects.
Always initialize wandb at the start, log with step info, and finish the run.
python
import wandb # Wrong: logging before init # wandb.log({'loss': 0.5}) # This will error # Right way: wandb.init(project='fix_pitfalls') wandb.log({'loss': 0.5, 'step': 1}) wandb.finish()
Quick Reference
| Command | Purpose |
|---|---|
| wandb.init(project='name') | Start a new tracking run in a project |
| wandb.config | Store hyperparameters or settings |
| wandb.log({'metric': value}) | Log metrics or values during training |
| wandb.finish() | End the run and upload data |
Key Takeaways
Always start with wandb.init() to create a new tracking run.
Use wandb.log() to record metrics like loss and accuracy during training.
Set wandb.config to save hyperparameters for easy comparison.
Call wandb.finish() to properly end and upload your run data.
Include step numbers in logs to keep metric plots clear and ordered.