W&B Quickstart - Weights & Biases Documentation (original) (raw)
Install W&B to track, visualize, and manage machine learning experiments of any size.
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
- Set the
WANDB_API_KEYenvironment variable.
export WANDB_API_KEY=<your_api_key> - Install the
wandblibrary and log in.
pip install wandb
wandb login import wandb
wandb.login()
!pip install wandb
import 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.
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.
Next steps
Explore more features of the W&B ecosystem:
- Read the W&B Integration tutorials that combine W&B with frameworks like PyTorch, libraries like Hugging Face, and services like SageMaker.
- Organize runs, automate visualizations, summarize findings, and share updates with collaborators using W&B Reports.
- Create W&B Artifacts to track datasets, models, dependencies, and results throughout your machine learning pipeline.
- Automate hyperparameter searches and optimize models with W&B Sweeps.
- Analyze runs, visualize model predictions, and share insights on a central dashboard.
- Visit W&B AI Academy to learn about LLMs, MLOps, and W&B Models through hands-on courses.
- Visit weave-docs.wandb.ai to learn how to track track, experiment with, evaluate, deploy, and improve your LLM-based applications using Weave.