Skip to main content
Article·wandb.ai
machine-learningdevopsdeploymentevaluationautomation

Weights & Biases

Track ML experiments and monitor deployed models with Weights & Biases (W&B). This platform centralizes hyperparameters, metrics, and code versions, enhancing reproducibility and collaboration. It streamlines MLOps, ensuring reliable, scalable AI deployments from research to production.

beginner15 min5 steps
The play
  1. Get Started with W&B
    Sign up for a free Weights & Biases account at `wandb.ai/signup` and install the library in your development environment.
  2. Initialize Your ML Project
    Add `import wandb` and `wandb.init()` to your training script to connect your experiment to the W&B server. Replace `your-username` with your W&B username.
  3. Log Hyperparameters & Configuration
    Capture all experiment hyperparameters and configuration settings using `wandb.config` to ensure reproducibility and easy comparison across runs.
  4. Track Metrics During Training
    Integrate `wandb.log()` into your training loop to record key performance metrics (e.g., loss, accuracy) in real-time.
  5. Visualize and Analyze Results
    Navigate to your W&B project dashboard in your browser to view real-time plots, compare different experiment runs, and gain insights into model performance.
Starter code
import wandb
import random # for simulating training

# 1. Initialize a new W&B run
# Replace "your-username" with your actual W&B username
wandb.init(project="simple-ml-demo", entity="your-username")

# 2. Define and log hyperparameters
config = {
    "learning_rate": 0.001,
    "epochs": 5,
    "batch_size": 32,
    "optimizer": "Adam",
    "activation": "relu"
}
wandb.config.update(config)

print(f"Starting training with config: {wandb.config}")

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

print("Training finished! Check your W&B dashboard.")
wandb.finish()
Source
Weights & Biases — Action Pack