Weights & Biases (wandb)
Weights & Biases (wandb) is a MLOps platform for experiment tracking, model optimization, and collaboration, widely used by machine learning practitioners. It provides a CLI and Python library to log metrics, visualize results, and manage models from experimentation to production. The library is actively maintained with frequent releases, typically on a monthly or bi-monthly cadence, and is currently at version 0.25.1.
Warnings
- breaking Python 3.8 is no longer supported starting from `wandb` version 0.25.0.
- breaking The legacy `wandb.beta.workflows` module (including `log_model()`, `use_model()`, `link_model()`) was removed in version 0.24.0. These functions are no longer available and will cause `AttributeError`.
- breaking Version `0.24.0` was yanked from PyPI due to a critical bug that could cause silent failure to upload some run data. If used, data might be missing from your W&B dashboard.
- deprecated Several `wandb.Run` methods are deprecated in favor of direct properties, including `run.project_name()`, `run.get_url()`, `run.get_project_url()`, and `run.get_sweep_url()`.
- gotcha The `wandb: ERROR Run aborted` or `wandb: ERROR Failed to log data` messages indicate an unexpected termination or data logging failure. This can be caused by script errors, system resource constraints, or network connectivity issues.
- gotcha Programmatic dataset splitting (e.g., using `sklearn.model_selection.train_test_split` without fixing `random_state` or without managing splits as artifacts) can lead to inconsistent train/test sets when new data is added, invalidating comparisons between experiments.
Install
-
pip install wandb
Imports
- wandb
import wandb
- wandb.init
import wandb wandb.init(...)
- wandb.login
import wandb wandb.login()
Quickstart
import wandb
import os
# Authenticate with W&B. For automated environments, use an environment variable.
# wandb.login() will prompt for an API key if not set.
# Set WANDB_API_KEY environment variable for CI/CD or headless environments.
# For local development, running `wandb login` in your terminal is common.
# os.environ.get('WANDB_API_KEY', '') # Example for fetching from env, but wandb.login() handles this.
wandb.login()
# Initialize a new W&B run
project_name = os.environ.get('WANDB_PROJECT', 'my-awesome-project')
config = {
'epochs': 10,
'lr': 0.01,
'batch_size': 32
}
with wandb.init(project=project_name, config=config) as run:
# Access hyperparameters
epochs = run.config.epochs
learning_rate = run.config.lr
print(f"Starting training for {epochs} epochs with LR: {learning_rate}")
# Simulate a training loop
for epoch in range(epochs):
# Simulate loss and accuracy metrics
loss = 1.0 / (epoch + 1) + 0.1 * (epochs - epoch - 1) / epochs
accuracy = 0.5 + 0.5 * (epoch + 1) / epochs
# Log metrics to W&B
run.log({"epoch": epoch, "loss": loss, "accuracy": accuracy})
print(f"Epoch {epoch+1}/{epochs}: Loss = {loss:.4f}, Accuracy = {accuracy:.4f}")
print("Training complete!")