Weights & Biases Cheat Sheet
A guide to Weights & Biases for experiment tracking, hyperparameter sweeps, artifact versioning, and logging metrics and media during model training.
Quickstart Logging
Initialize a run and log metrics over training.
import wandbwandb.init(project='my-project', config={'lr': 0.001, 'epochs': 10})config = wandb.configfor epoch in range(config.epochs): loss = train_one_epoch() wandb.log({'loss': loss, 'epoch': epoch})wandb.finish()
CLI Commands
Authenticate and manage sweeps from the terminal.
wandb login # Authenticate with your API keywandb init # Link the current directory to a projectwandb sweep sweep.yaml # Create a hyperparameter sweep from a configwandb agent <sweep_id> # Launch an agent to run sweep trialswandb artifact get <name>:latest # Download the latest version of an artifact
Logging Media & Plots
Log images and built-in chart types.
wandb.log({ 'predictions': wandb.Image(image, caption='Predicted: cat'), 'confusion_matrix': wandb.plot.confusion_matrix( y_true=y_true, preds=y_pred, class_names=class_names ),})
Core Concepts
Key building blocks of the W&B platform.
- Run- A single execution of your training or evaluation script, logged with a unique ID
- Project- A collection of related runs compared together on shared dashboards
- Artifact- Versioned reference to datasets or model checkpoints, tracked via wandb.Artifact()
- Sweep- Automated hyperparameter search (grid, random, or Bayesian) defined in a YAML config
- Report- Shareable, interactive document combining charts, tables, and markdown notes
Programmatic Sweeps
Define a Bayesian sweep config in code and launch agents without a YAML file.
import wandbsweep_config = { 'method': 'bayes', 'metric': {'name': 'val_loss', 'goal': 'minimize'}, 'parameters': { 'lr': {'min': 1e-5, 'max': 1e-1, 'distribution': 'log_uniform_values'}, 'batch_size': {'values': [16, 32, 64]}, }, 'early_terminate': {'type': 'hyperband', 'min_iter': 3},}def train(): with wandb.init() as run: cfg = run.config val_loss = train_one_config(cfg.lr, cfg.batch_size) wandb.log({'val_loss': val_loss})sweep_id = wandb.sweep(sweep_config, project='my-project')wandb.agent(sweep_id, function=train, count=20)
Watching Gradients & Weights
Auto-log gradient and parameter histograms each training step to spot vanishing/exploding gradients.
import wandbwandb.init(project='my-project')wandb.watch(model, criterion=loss_fn, log='all', log_freq=100)for batch in dataloader: optimizer.zero_grad() loss = loss_fn(model(batch.x), batch.y) loss.backward() optimizer.step() # gradient/parameter histograms are captured automatically at log_freq steps
Artifact Lineage
Chain dataset and model artifacts so the DAG of what produced what is queryable later.
with wandb.init(job_type='preprocess') as run: raw = run.use_artifact('raw-data:latest') raw_dir = raw.download() processed = wandb.Artifact('processed-data', type='dataset') processed.add_dir('./processed') run.log_artifact(processed)with wandb.init(job_type='train') as run: data = run.use_artifact('processed-data:latest').download() model_art = wandb.Artifact('trained-model', type='model') model_art.add_file('model.pt') run.log_artifact(model_art) # lineage graph links raw -> processed -> model
Interactive Tables for Dataset & Prediction Review
Log a wandb.Table to inspect model predictions row-by-row in the UI, filterable like a spreadsheet.
columns = ['image', 'ground_truth', 'prediction', 'confidence']table = wandb.Table(columns=columns)for img, label, pred, conf in zip(images, labels, predictions, confidences): table.add_data(wandb.Image(img), label, pred, conf)wandb.log({'predictions_table': table})
Advanced Platform Concepts
Organizational and reliability features beyond basic run logging.
- group / job_type- init() params that cluster distributed-training processes or pipeline stages into one logical run in the UI
- resume='allow'- Reattaches to an existing run by id so a crashed job can continue logging to the same history
- define_metric- Declares a custom x-axis (e.g. step) or summary aggregation (min/max) for a metric instead of the default
- Report API (wandb.apis.reports)- Programmatically builds shareable Reports combining panels, markdown, and run comparisons
- WANDB_MODE=offline- Buffers all logging locally for air-gapped runs; `wandb sync` uploads the run directory later
- Alerts (run.alert)- Sends a Slack/email notification when a condition (e.g. loss spike) is hit mid-run
Log datasets and model checkpoints as wandb.Artifact() objects instead of plain files — artifacts are versioned and content-hashed, so every run records the exact lineage of which data and weights produced it.