Fix wandb run not logging
Quick answer
To fix wandb runs not logging, ensure you call wandb.init() before logging metrics and wandb.finish() after your run completes. Also, verify your API key is set in the environment variable WANDB_API_KEY and that you call wandb.log() properly within your training loop.
PREREQUISITES
Python 3.8+pip install wandbWANDB_API_KEY environment variable set
Setup
Install the wandb Python package and set your API key as an environment variable to authenticate your runs.
pip install wandb Step by step
Initialize a wandb run, log metrics inside your training loop, and finish the run properly to ensure logs are uploaded.
import os
import wandb
# Ensure your WANDB_API_KEY is set in your environment
# os.environ['WANDB_API_KEY'] = 'your_api_key_here' # Do NOT hardcode in production
# Initialize a new run
wandb.init(project="my-project", entity="my-entity")
for epoch in range(3):
# Simulate training metric
loss = 0.1 * (3 - epoch)
accuracy = 0.8 + 0.05 * epoch
# Log metrics
wandb.log({"epoch": epoch, "loss": loss, "accuracy": accuracy})
# Finish the run to flush logs
wandb.finish() Common variations
You can use wandb.init() with different configurations like config for hyperparameters or run names. For asynchronous logging, wandb.log() is thread-safe. Streaming logs to the dashboard happens automatically.
import wandb
run = wandb.init(project="my-project", config={"lr": 0.001, "batch_size": 32}, name="experiment-1")
for step in range(5):
wandb.log({"step": step, "metric": step * 2})
wandb.finish() Troubleshooting
- If logs do not appear, verify WANDB_API_KEY is set correctly in your environment.
- Ensure wandb.finish() is called to flush logs before your script exits.
- Check your internet connection and firewall settings blocking wandb uploads.
- Run wandb login in your terminal to authenticate if environment variable is missing.
Key Takeaways
- Always call wandb.init() before logging metrics to start a run.
- Use wandb.log() inside your training loop to record metrics properly.
- Call wandb.finish() at the end to ensure logs are uploaded.
- Set your API key in WANDB_API_KEY environment variable or run wandb login.
- Check network connectivity if logs fail to upload.