Skip to main content
Install W&B to track, visualize, and manage machine learning experiments of any size.
Are you looking for information on W&B Weave? See the Weave Python SDK quickstart or Weave TypeScript SDK quickstart.

Sign up and create an API key

To authenticate your machine with W&B, generate an API key from your user profile or at wandb.ai/authorize. Copy the API key and store it securely.

Install the wandb library and log in

  • Command Line
  • Python
  • Python notebook
  1. Set the WANDB_API_KEY environment variable.
    export WANDB_API_KEY=<your_api_key>
    
  2. Install the wandb library and log in.
    pip install wandb
    wandb login
    

Initialize a run and track hyperparameters

In your Python script or notebook, initialize a W&B run object with wandb.init(). Use a dictionary for the config parameter to specify hyperparameter names and values. Within the with statement, you can log metrics and other information to W&B.
import wandb

wandb.login()

# Project that the run is recorded to
project = "my-awesome-project"

# Dictionary with hyperparameters
config = {
    'epochs' : 10,
    'lr' : 0.01
}

with wandb.init(project=project, config=config) as run:
    # Training code here
    # Log values to W&B with run.log()
    run.log({"accuracy": 0.9, "loss": 0.1})
See the next section for a complete example that simulates a training run and logs accuracy and loss metrics to W&B.
A run is a core element of W&B. You use runs to track metrics, create logs, track artifacts, and more.

Create a machine learning training experiment

This mock training script logs simulated accuracy and loss metrics to W&B. Copy and paste the following code into a Python script or notebook cell and run it:
import wandb
import random

wandb.login()

# Project that the run is recorded to
project = "my-awesome-project"

# Dictionary with hyperparameters
config = {
    'epochs' : 10,
    'lr' : 0.01
}

with wandb.init(project=project, config=config) as run:
    offset = random.random() / 5
    print(f"lr: {config['lr']}")
    
    # Simulate a training run
    for epoch in range(2, config['epochs']):
        acc = 1 - 2**-config['epochs'] - random.random() / config['epochs'] - offset
        loss = 2**-config['epochs'] + random.random() / config['epochs'] + offset
        print(f"epoch={config['epochs']}, accuracy={acc}, loss={loss}")
        run.log({"accuracy": acc, "loss": loss})
Visit wandb.ai/home to view recorded metrics such as accuracy and loss and how they changed during each training step. The following image shows the loss and accuracy tracked from each run. Each run object appears in the Runs column with generated names.
Shows loss and accuracy tracked from each run.

Next steps

Explore more features of the W&B ecosystem:
  1. Read the W&B Integration tutorials that combine W&B with frameworks like PyTorch, libraries like Hugging Face, and services like SageMaker.
  2. Organize runs, automate visualizations, summarize findings, and share updates with collaborators using W&B Reports.
  3. Create W&B Artifacts to track datasets, models, dependencies, and results throughout your machine learning pipeline.
  4. Automate hyperparameter searches and optimize models with W&B Sweeps.
  5. Analyze runs, visualize model predictions, and share insights on a central dashboard.
  6. Visit W&B AI Academy to learn about LLMs, MLOps, and W&B Models through hands-on courses.
  7. Visit weave-docs.wandb.ai to learn how to track track, experiment with, evaluate, deploy, and improve your LLM-based applications using Weave.