Skip to main content
Use the W&B Python SDK to log metrics, media, and custom objects from your training scripts so you can visualize, compare, and analyze them in the W&B app. This page introduces how logging works, what W&B captures automatically, and the common patterns you’ll use to track experiments. Log a dictionary of metrics, media, or custom objects to a step with the W&B Python SDK. W&B collects the key-value pairs during each step and stores them in a single dictionary each time you log data with wandb.Run.log(). W&B saves the data your script logs locally to your machine in a directory called wandb, then syncs it to the W&B cloud or your private server.
W&B stores key-value pairs in a single dictionary only if you pass the same value for each step. W&B writes all of the collected keys and values to memory if you log a different value for step.
Each call to wandb.Run.log() is a new step by default. W&B uses steps as the default x-axis when it creates charts and panels. You can optionally create and use a custom x-axis or capture a custom summary metric. For more information, see Customize log axes.
Use wandb.Run.log() to log consecutive values for each step: 0, 1, 2, and so on. You can’t write to a specific history step. W&B only writes to the “current” and “next” step.

Automatically logged data

W&B automatically logs the following information during a W&B Experiment:
  • System metrics: CPU and GPU utilization, network, and similar metrics. For the GPU, these are fetched with nvidia-smi.
  • Command line: W&B picks up stdout and stderr and shows them in the logs tab on the run page.
Turn on Code Saving in your account’s Settings page to log:
  • Git commit: W&B picks up the latest git commit and shows it on the overview tab of the run page, along with a diff.patch file if you have any uncommitted changes.
  • Dependencies: W&B uploads the requirements.txt file and shows it on the files tab of the run page, along with any files you save to the wandb directory for the run.

Data logged with specific W&B API calls

Beyond the data W&B captures automatically, you decide exactly what to log from your code. The following list describes some commonly logged objects and the API calls used to capture them:
  • Datasets: You must explicitly log images or other dataset samples for them to stream to W&B.
  • Plots: Use wandb.plot() with wandb.Run.log() to track charts. For more information, see Create and track plots from experiments.
  • Tables: Use wandb.Table to log data to visualize and query with W&B. For more information, see Log tables.
  • PyTorch gradients: Add wandb.Run.watch(model) to see gradients of the weights as histograms in the UI.
  • Configuration information: Log hyperparameters, a link to your dataset, or the name of the architecture you’re using as config parameters, passed in like this: wandb.init(config=your_config_dictionary).
  • Metrics: Use wandb.Run.log() to see metrics from your model. If you log metrics like accuracy and loss from inside your training loop, you’ll get live updating graphs in the UI.

Metric naming constraints

Due to GraphQL limitations, metric names in W&B must follow specific naming rules. When you choose names for the metrics you log, follow the rules in this section so that your metrics sort, query, and display correctly in the W&B UI:
  • Allowed characters: Letters (A-Z, a-z), digits (0-9), and underscores (_)
  • Starting character: Names must start with a letter or underscore
  • Pattern: Metric names should match /^[_a-zA-Z][_a-zA-Z0-9]*$/
Avoid naming metrics with invalid characters (such as commas, spaces, or special symbols), which may cause problems with sorting, querying, or display in the W&B UI.
Valid metric names:
with wandb.init() as run:
  run.log({"accuracy": 0.9, "val_loss": 0.1, "epoch_5": 5})
  run.log({"modelAccuracy": 0.95, "learning_rate": 0.001})
Invalid metric names (avoid these):
with wandb.init() as run:
  run.log({"acc,val": 0.9})  # Contains comma
  run.log({"loss-train": 0.1})  # Contains hyphen
  run.log({"test acc": 0.95})  # Contains space
  run.log({"5_fold_cv": 0.8})  # Starts with number

Common workflows

The following workflows show how to combine wandb.Run.log() with related APIs to address frequent experiment tracking tasks:
  • Compare the best accuracy: To compare the best value of a metric across runs, set the summary value for that metric. By default, W&B sets the summary to the last value you logged for each key. This is useful in the table in the UI, where you can sort and filter runs based on their summary metrics to compare runs in a table or bar chart based on their best accuracy instead of final accuracy. For example: wandb.run.summary["best_accuracy"] = best_accuracy.
  • View multiple metrics on one chart: Log multiple metrics in the same call. For example:
    with wandb.init() as run:
      run.log({"acc": 0.9, "loss": 0.1})
    
    You can then plot both metrics in the UI.
  • Customize the x-axis: Add a custom x-axis to the same log call to visualize your metrics against a different axis in the W&B dashboard. For example:
    with wandb.init() as run:
      run.log({'acc': 0.9, 'epoch': 3, 'batch': 117})
    
    To set the default x-axis for a given metric, use Run.define_metric().
  • Log rich media and charts: wandb.Run.log() supports many data types, from media like images and videos to tables and charts.

Best practices and tips

For best practices and tips for Experiments and logging, see Best Practices: Experiments and Logging.