Intermediate
Experiment Tracking
Log metrics, hyperparameters, images, tables, and custom media. Build interactive dashboards and compare experiments side-by-side.
Logging Metrics
Python — wandb.log() basics
import wandb
wandb.init(project="tracking-demo")
for step in range(1000):
# Log scalar metrics
wandb.log({
"train/loss": compute_loss(train_data),
"train/accuracy": compute_accuracy(train_data),
"val/loss": compute_loss(val_data),
"val/accuracy": compute_accuracy(val_data),
"learning_rate": scheduler.get_last_lr()[0],
})
# Log summary metrics (final values)
wandb.summary["best_accuracy"] = best_accuracy
wandb.summary["total_params"] = count_params(model)
Logging Images and Media
Python — Logging images, plots, and media
import wandb
import matplotlib.pyplot as plt
import numpy as np
# Log images
images = [wandb.Image(img, caption=f"Sample {i}")
for i, img in enumerate(sample_images[:8])]
wandb.log({"predictions": images})
# Log matplotlib plots
fig, ax = plt.subplots()
ax.plot(history['loss'])
ax.set_title("Training Loss")
wandb.log({"loss_plot": wandb.Image(fig)})
plt.close()
# Log confusion matrix
wandb.log({"confusion_matrix": wandb.plot.confusion_matrix(
y_true=y_test, preds=y_pred,
class_names=class_names
)})
# Log audio
wandb.log({"audio_sample": wandb.Audio(
audio_array, sample_rate=16000, caption="Prediction"
)})
# Log 3D point clouds
wandb.log({"point_cloud": wandb.Object3D(points)})
W&B Tables
Python — Interactive data tables
# Create a table for detailed analysis
table = wandb.Table(columns=["image", "prediction", "actual", "confidence"])
for img, pred, actual, conf in zip(images, preds, actuals, confidences):
table.add_data(
wandb.Image(img),
class_names[pred],
class_names[actual],
conf
)
wandb.log({"predictions_table": table})
# Log a pandas DataFrame as a table
import pandas as pd
df = pd.DataFrame(results)
wandb.log({"results": wandb.Table(dataframe=df)})
Framework Integrations
Python — PyTorch integration
import wandb
wandb.init(project="pytorch-demo")
# Watch model gradients and parameters
wandb.watch(model, log="all", log_freq=100)
# Training loop
for epoch in range(epochs):
for batch in dataloader:
loss = train_step(model, batch)
wandb.log({"loss": loss})
# Log the final model
torch.save(model.state_dict(), "model.pt")
wandb.save("model.pt")
Python — scikit-learn integration
from sklearn.ensemble import RandomForestClassifier
import wandb
wandb.init(project="sklearn-demo")
# Log sklearn model params automatically
model = RandomForestClassifier(n_estimators=100, max_depth=10)
wandb.config.update(model.get_params())
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
wandb.log({"accuracy": accuracy})
# Log ROC curve
wandb.log({"roc": wandb.plot.roc_curve(y_test, model.predict_proba(X_test))})
Custom Dashboards
W&B dashboards update in real time and support:
- Line charts: Track metrics over time with smoothing and grouping.
- Scatter plots: Compare hyperparameters vs. metrics across runs.
- Parallel coordinates: Visualize the relationship between many hyperparameters and performance.
- Bar charts: Compare final metrics across runs.
- Tables: Interactive filtering and sorting of run data.
Organize with metric prefixes: Use prefixes like
train/, val/, test/ in metric names. W&B automatically groups them in the dashboard, making it easy to compare training vs. validation performance.