Skip to main content

W&B Quickstart

Install W&B and start tracking your machine learning experiments in minutes.

1. Create an account and install W&B

Before you get started, make sure you create an account and install W&B:

  1. Sign up for a free account at https://wandb.ai/site and then log in to your wandb account.
  2. Install the wandb library on your machine in a Python 3 environment using pip.

The following code snippets demonstrate how to install and log into W&B using the W&B CLI and Python Library:

Install the CLI and Python library for interacting with the Weights and Biases API:

!pip install wandb

2. Log in to W&B

Next, import the W&B Python SDK and log in:

wandb.login()

Provide your API key when prompted.

3. Start a run and track hyperparameters

Initialize a W&B Run object in your Python script or notebook with wandb.init() and pass a dictionary to the config parameter with key-value pairs of hyperparameter names and values:

run = wandb.init(
# Set the project where this run will be logged
project="my-awesome-project",
# Track hyperparameters and run metadata
config={
"learning_rate": 0.01,
"epochs": 10,
},
)

A run is the basic building block of W&B. You will use them often to track metrics, create logs, create jobs, and more.

Putting it all together

Putting it all together, your training script might look similar to the following code example. The highlighted code shows W&B-specific code. Note that we added code that mimics machine learning training.

# train.py
import wandb
import random # for demo script

wandb.login()

epochs = 10
lr = 0.01

run = wandb.init(
# Set the project where this run will be logged
project="my-awesome-project",
# Track hyperparameters and run metadata
config={
"learning_rate": lr,
"epochs": epochs,
},
)

offset = random.random() / 5
print(f"lr: {lr}")

# simulating a training run
for epoch in range(2, epochs):
acc = 1 - 2**-epoch - random.random() / epoch - offset
loss = 2**-epoch + random.random() / epoch + offset
print(f"epoch={epoch}, accuracy={acc}, loss={loss}")
wandb.log({"accuracy": acc, "loss": loss})

# run.log_code()

That's it! Navigate to the W&B App at https://wandb.ai/home to view how the metrics we logged with W&B (accuracy and loss) improved during each training step.

Shows the loss and accuracy that was tracked from each time we ran the script above.

The image above (click to expand) shows the loss and accuracy that was tracked from each time we ran the script above. Each run object that was created is show within the Runs column. Each run name is randomly generated.

What's next?

Explore the rest of the W&B ecosystem.

  1. Check out W&B Integrations to learn how to integrate W&B with your ML framework such as PyTorch, ML library such as Hugging Face, or ML service such as SageMaker.
  2. Organize runs, embed and automate visualizations, describe your findings, and share updates with collaborators with W&B Reports.
  3. Create W&B Artifacts to track datasets, models, dependencies, and results through each step of your machine learning pipeline.
  4. Automate hyperparameter search and explore the space of possible models with W&B Sweeps.
  5. Understand your datasets, visualize model predictions, and share insights in a central dashboard.
  6. Navigate to W&B AI Academy and learn about LLMs, MLOps and W&B Models from hands-on courses.

Common Questions

Where do I find my API key? Once you've signed in to www.wandb.ai, the API key will be on the Authorize page.

How do I use W&B in an automated environment? If you are training models in an automated environment where it's inconvenient to run shell commands, such as Google's CloudML, you should look at our guide to configuration with Environment Variables.

Do you offer local, on-prem installs? Yes, you can privately host W&B locally on your own machines or in a private cloud, try this quick tutorial notebook to see how.

How do I turn off wandb logging temporarily? If are testing code and want to disable wandb syncing, set the environment variable WANDB_MODE=offline.

Was this page helpful?👍👎