How to use Weights and Biases for logging
· Category: AI & Machine Learning
Short answer
Weights and Biases is a machine learning experiment tracking platform that captures metrics, hyperparameters, model artifacts, and system resource usage in real time.
Steps
- Install the wandb library and authenticate with your API key.
- Initialize a run with wandb.init and set the project and run names.
- Log metrics during training using wandb.log and define custom step increments.
- Save model checkpoints, datasets, and configuration files as artifacts.
- Use the web dashboard to compare runs, visualize learning curves, and generate reports.
Tips
- Log both training and validation metrics at the same step for aligned comparisons.
- Use wandb.config to capture all hyperparameters automatically.
- Enable system metrics logging to detect GPU memory or CPU bottlenecks.
- Integrate with frameworks via autolog or lightweight custom callbacks.
Common issues
- Network interruptions causing delayed or missing logs.
- Excessive logging of high-frequency metrics slowing down training.
- Forgetting to call wandb.finish or properly handle run termination.
- Exceeding storage quotas with large artifact uploads.
Example
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Linear(784, 256),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(256, 10)
)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
This snippet defines a simple neural network with dropout for regularization, a cross-entropy loss, and the Adam optimizer in PyTorch.