How to log metrics with Weights and Biases
Quick answer
Use the
wandb Python package to log metrics by initializing a run with wandb.init() and then calling wandb.log() with a dictionary of metric names and values. This enables real-time tracking and visualization of metrics like loss and accuracy during model training.PREREQUISITES
Python 3.8+pip install wandbWeights and Biases account (free tier available)Set environment variable <code>WANDB_API_KEY</code> with your API key
Setup
Install the wandb package and configure your API key as an environment variable to authenticate your runs.
pip install wandb Step by step
This example shows how to initialize a Weights and Biases run, log metrics in a loop, and finish the run cleanly.
import os
import wandb
# Ensure your API key is set in the environment
# export WANDB_API_KEY=os.environ["WANDB_API_KEY"]
# Initialize a new W&B run
wandb.init(project="my-project", entity="my-team", config={"epochs": 5, "learning_rate": 0.001})
for epoch in range(5):
# Simulate training metrics
loss = 0.1 * (5 - epoch)
accuracy = 0.2 * epoch
# Log metrics to W&B
wandb.log({"epoch": epoch + 1, "loss": loss, "accuracy": accuracy})
# Finish the run
wandb.finish() output
wandb: Currently logged in as: your-username (use `wandb login --relogin` to force relogin) wandb: Tracking run with wandb version 0.15.0 wandb: Run URL: https://wandb.ai/my-team/my-project/runs/xyz123 # Metrics logged for epochs 1 to 5 with loss and accuracy values
Common variations
- Use
wandb.init(mode="offline")to log metrics locally without syncing to the cloud. - Log additional data types like images or model checkpoints with
wandb.log(). - Integrate
wandblogging inside training loops of frameworks like PyTorch or TensorFlow.
Troubleshooting
- If you see
wandb: ERROR API key not found, ensureWANDB_API_KEYis set correctly in your environment. - For network issues, check your firewall or proxy settings blocking
wandbfrom syncing. - Use
wandb.init(mode="offline")to debug without internet connectivity.
Key Takeaways
- Initialize a W&B run with
wandb.init()before logging metrics. - Use
wandb.log()to send metric dictionaries for real-time tracking. - Always set your
WANDB_API_KEYenvironment variable for authentication. - Call
wandb.finish()to properly close the run and upload data. - You can log various data types beyond scalars, including images and model files.