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:
- Sign up for a free account at https://wandb.ai/site and then log in to your wandb account.
- 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:
- Notebook
- Command Line
Install the CLI and Python library for interacting with the Weights and Biases API:
pip install wandb
Install the CLI and Python library for interacting with the Weights and Biases API:
!pip install wandb
2. Log in to W&B
- Notebook
- Command Line
Next, log in to W&B:
wandb login
Or if you are using W&B Server (including Dedicated Cloud or Self-managed):
wandb login --relogin --host=http://your-shared-local-host.com
If needed, ask your deployment admin for the hostname.
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.
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.
- 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.
- Organize runs, embed and automate visualizations, describe your findings, and share updates with collaborators with W&B Reports.
- Create W&B Artifacts to track datasets, models, dependencies, and results through each step of your machine learning pipeline.
- Automate hyperparameter search and explore the space of possible models with W&B Sweeps.
- Understand your datasets, visualize model predictions, and share insights in a central dashboard.
- 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
.