Skip to main content
Use the W&B Python SDK to track machine learning experiments. You can then review the results in an interactive dashboard or export your data to Python for programmatic access with the W&B Public API. This guide describes how to use W&B building blocks to create a W&B Experiment.

How to create a W&B Experiment

Create a W&B Experiment in four steps:
  1. Initialize a W&B Run
  2. Capture a dictionary of hyperparameters
  3. Log metrics inside your training loop
  4. Log an artifact to W&B

Initialize a W&B run

Use wandb.init() to create a W&B Run. The following snippet creates a run in a W&B project named “cat-classification” with the description “My first experiment” to help identify this run. Tags “baseline” and “paper1” are included to remind us that this run is a baseline experiment intended for a future paper publication.
wandb.init() returns a Run object.
Note: Runs are added to pre-existing projects if that project already exists when you call wandb.init(). For example, if you already have a project called “cat-classification”, that project will continue to exist and not be deleted. Instead, a new run is added to that project.

Capture a dictionary of hyperparameters

Save a dictionary of hyperparameters such as learning rate or model type. The model settings you capture in config are useful later to organize and query your results.
For more information on how to configure an experiment, see Configure Experiments.

Log metrics inside your training loop

Call run.log() to log metrics about each training step such as accuracy and loss.
For more information on different data types you can log with W&B, see Log objects and media.

Log an artifact to W&B

Optionally log a W&B Artifact. Artifacts make it easy to version datasets and models.
Learn more about Artifacts or about versioning models in Registry.

Putting it all together

The full script with the preceding code snippets is found below:

Next steps: Visualize your experiment

Use your project’s workspace to organize and visualize results from your machine learning models. You can construct interactive charts like parallel coordinates plots, parameter importance analyses, and additional chart types.
Quickstart Sweeps workspace example
For more information on how to view experiments and specific runs, see View experiment results.

Best practices

The following are some suggested guidelines to consider when you create experiments:
  • Manage runs with a context manager: Use wandb.init() in a with statement to automatically finish the run when the code completes or raises an exception.
    • In Jupyter notebooks, you might prefer to manage the run object yourself. In this case, call finish() to mark it complete:
  • Config: Track hyperparameters, model architecture, dataset information, and other values needed to reproduce your model. For more information, see View the config in the Overview section of a run in the W&B App.
  • Project: Use projects to organize experiments in a central location where you can visualize results, compare runs, view and download artifacts, create automations, and more.
  • Notes: Add notes to describe the purpose of a run, such as baseline model or tuned hyperparameters. You can edit notes later from the run overview in the W&B App.
  • Job types: Add job types to your runs to organize and filter runs by task, such as train, test, or inference.
  • Tags: Add tags to runs to label runs with features or attributes that might not be obvious from logged metrics or artifacts.
The following example shows how to initialize a W&B run using these best practices:
For more information about available parameters, see wandb.init() in the Python SDK Reference Guide.