Weights & Biases (wandb)

0.25.1 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Weights & Biases run, log hyperparameters using `wandb.config`, and track metrics like loss and accuracy using `run.log()` within a simulated training loop. Before running, ensure you have authenticated with `wandb.login()` or set the `WANDB_API_KEY` environment variable.

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!")

view raw JSON →