Skip to main content
Article·wandb.ai
MLtrackingexperimentsmlmachine-learningdata-science

Weights & Biases

Set up Weights & Biases (W&B) to track and visualize your machine learning experiments. Gain immediate insights into model performance, hyperparameter tuning, and experiment reproducibility.

beginner10 min5 steps
The play
  1. Install Weights & Biases
    Add the `wandb` library to your Python environment using pip.
  2. Authenticate Your Account
    Run the login command. This will prompt you to enter your W&B API key, linking your local environment to your W&B account. (Create an account at wandb.ai if you don't have one).
  3. Initialize a New Run
    Import `wandb` and call `wandb.init()` at the beginning of your training script to start a new experiment run. Specify a project name.
  4. Log Metrics and Parameters
    Use `wandb.log()` to record key performance metrics (e.g., loss, accuracy) and `wandb.config` to save hyperparameters during your training loop.
  5. View Your Dashboard
    After your script runs (or even while it's running), W&B will provide a URL in your console. Open this URL in your browser to access your live experiment dashboard and visualize results.
Starter code
import wandb
import random

# Initialize a new W&B run
# Replace 'my-first-wandb-project' with your desired project name
wandb.init(project="my-first-wandb-project", name="simple-logging-example")

# Log some configuration parameters
wandb.config.learning_rate = 0.01
wandb.config.epochs = 5

print(f"Starting training for {wandb.config.epochs} epochs...")

# Simulate a training loop and log metrics
for epoch in range(wandb.config.epochs):
    # Simulate loss decreasing and accuracy increasing
    loss = 1.0 / (epoch + 1) + random.uniform(-0.1, 0.1)
    accuracy = 0.7 + (epoch * 0.05) + random.uniform(-0.02, 0.02)
    
    # Log current epoch, loss, and accuracy to W&B
    wandb.log({"epoch": epoch, "loss": loss, "accuracy": accuracy})
    print(f"Epoch {epoch}: Loss={loss:.4f}, Accuracy={accuracy:.4f}")

# Optional: Finish the run explicitly (though it usually finishes automatically)
wandb.finish()
print("Run finished. Check your W&B dashboard for results!")
Source
Weights & Biases — Action Pack