wandb.init() to start tracking an experiment and stream metrics, logs, and artifacts to W&B. This page explains how to initialize a single run and how to manage multiple runs from the same Python process.
By default, W&B supports one active run per Python process. If you call wandb.init() while a run is active, W&B either returns the active run or finishes it before creating a new one. The behavior depends on the environment and the reinit configuration.
To manage multiple active runs in one process, see Multiple runs in one process.
W&B recommends using a context manager (
with block) when calling wandb.init(). This ensures that W&B finishes the run and uploads its data when the block ends.Single run per process
The following example initializes a run:basic.py
exalted-darkness-6 to the awesome-project project under the nico entity. W&B assigns the run the unique ID pgbn9y21.
Multiple runs in one process
Use thereinit parameter in wandb.init() or wandb.Settings to manage multiple runs in one Python process. For example, keep a primary run active while creating short-lived secondary runs for other tasks.
Common use cases include:
- Creating secondary runs for evaluations or subtasks while a primary run remains active.
- Running multiple sub-experiments from one script.
- Logging different tasks or time periods to separate runs from one process.
RequirementsTo manage multiple runs in a single Python process, you must have W&B Python SDK version
v0.19.10 or newer.reinit options
Use the reinit parameter to control what happens when you call wandb.init() while another run is active. The following table compares available options and common use cases. For the complete parameter definition, see the wandb.init() reference documentation.
W&B doesn’t support
create_new mode for W&B Integrations that assume a single global run, such as Hugging Face Trainer, Keras callbacks, and PyTorch Lightning. If you use these integrations, run each sub-experiment in a separate process.Configure reinit
You can set the reinit value in several ways. Choose the option that best fits your workflow:
-
Use
wandb.init()with thereinitargument directly: -
Use
wandb.init()and pass awandb.Settingsobject to thesettingsparameter. Specifyreinitin theSettingsobject: -
Use
wandb.setup()to set thereinitoption globally for all runs in the current process. This is useful if you want to configure the behavior once and have it apply to all subsequentwandb.init()calls in that process. -
Specify the desired value for
reinitin the environment variableWANDB_REINIT. Defining an environment variable applies thereinitoption to allwandb.init()calls.
wandb.init():
Example: Concurrent processes
This example shows how to apply thereinit="create_new" option to a realistic workflow that combines a long-lived primary run with short-lived secondary runs.
Suppose you want to create a primary process that remains open for the entire script, while periodically spawning short-lived secondary processes without finishing the primary process. For example, this pattern can be useful if you want to train a model in the primary run, but compute evaluations or do other work in separate runs.
To achieve this, use reinit="create_new" and initialize multiple runs. For this example, suppose “Run A” is the primary process that remains open throughout the script, while “Run B1” and “Run B2” are short-lived secondary runs for tasks like evaluation.
The high-level workflow might look like this:
- Initialize the primary process Run A with
wandb.init()and log training metrics. - Initialize Run B1 (with
wandb.init()), log data, then finish it. - Log more data to Run A.
- Initialize Run B2, log data, then finish it.
- Continue logging to Run A.
- Finish Run A at the end.
reinit="create_new"creates a new run each time you callwandb.init().- You keep references to each run.
wandb.rundoesn’t automatically point to the new run created withreinit="create_new". Store new runs in variables such asrun_aorrun_b1, and call.log()or.finish()on those objects as needed. - You can finish sub-runs whenever you want while keeping the primary run open.
- Finish your runs with
run.finish()when you’re done logging to them. This ensures that W&B uploads all data and closes the run.