How to log hyperparameters with wandb
Quick answer
Use wandb.init() with the config parameter to log hyperparameters at the start of your run. You can also update hyperparameters anytime with wandb.config.update(). This enables easy experiment tracking and visualization.
PREREQUISITES
Python 3.8+pip install wandbwandb account (free)wandb login via CLI
Setup
Install wandb via pip and authenticate your account to enable logging.
pip install wandb
wandb login Step by step
Initialize a wandb run with hyperparameters passed as a dictionary to config. Log metrics during training and finish the run.
import wandb
# Define hyperparameters
hyperparams = {
"learning_rate": 0.001,
"batch_size": 32,
"epochs": 10
}
# Initialize a new wandb run with config
wandb.init(project="my-project", config=hyperparams)
# Access hyperparameters via wandb.config
config = wandb.config
print(f"Learning rate: {config.learning_rate}")
# Simulate training loop
for epoch in range(config.epochs):
# Dummy metric
accuracy = 0.8 + epoch * 0.01
# Log metrics
wandb.log({"epoch": epoch, "accuracy": accuracy})
wandb.finish() output
Learning rate: 0.001
Common variations
- Update hyperparameters mid-run with
wandb.config.update(). - Log hyperparameters separately using
wandb.configattributes. - Use
wandb.init()withoutconfigand set hyperparameters later.
import wandb
wandb.init(project="my-project")
# Update hyperparameters after init
wandb.config.update({"learning_rate": 0.002, "batch_size": 64})
print(f"Updated batch size: {wandb.config.batch_size}")
wandb.finish() output
Updated batch size: 64
Troubleshooting
- If metrics or hyperparameters don't appear in the dashboard, ensure you called
wandb.finish()to flush logs. - Check your internet connection and
wandb loginstatus. - Use
wandbCLI commands likewandb statusto verify run status.
Key Takeaways
- Pass hyperparameters as a dictionary to wandb.init(config=...) for automatic logging.
- Use wandb.config.update() to modify hyperparameters during a run.
- Always call wandb.finish() to ensure logs are uploaded and visible in the dashboard.