import wandb
for x in range(2):
= wandb.init(reinit=True)
run for y in range (25):
"metric": x+y})
wandb.log({ run.finish()
Launch Experiments with wandb.init
Call wandb.init() at the top of your script to start a new run
Call wandb.init() once at the beginning of your script to initialize a new job. This creates a new run in W&B and launches a background process to sync data.
- On-Prem: If you need a private cloud or local instance of W&B, see our Self Hosted offerings.
- Automated Environments: Most of these settings can also be controlled via Environment Variables. This is often useful when you’re running jobs on a cluster.
Reference Documentation
View the reference docs for this function, generated from the wandb Python library.
Common Questions
How do I launch multiple runs from one script?
If you’re trying to start multiple runs from one script, add two things to your code:
- run = wandb.init(reinit=True): Use this setting to allow reinitializing runs
- run.finish(): Use this at the end of your run to finish logging for that run
Alternatively you can use a python context manager which will automatically finish logging:
import wandb
for x in range(2):
= wandb.init(reinit=True)
run with run:
for y in range(25):
"metric": x+y}) run.log({
InitStartError: Error communicating with wandb process
This error indicates that the library is having difficulty launching the process which synchronizes data to the server. The following workarounds can help resolve the issue in certain environments:
=wandb.Settings(start_method="fork")) wandb.init(settings
For versions prior to 0.13.0 we suggest using:
=wandb.Settings(start_method="thread")) wandb.init(settings
How do I programmatically access the human-readable run name?
It’s available as the .name attribute of a wandb.Run.
import wandb
;
wandb.init()= wandb.run.name run_name
print(run_name)
sweet-sun-46
Can I just set the run name to the run ID?
If you’d like to overwrite the run name (like snowy-owl-10) with the run ID (like qvlp96vk) you can use this snippet:
import wandb
;
wandb.init()= wandb.run.id
wandb.run.name wandb.run.save()
print(wandb.run.name)
16ej8478
Is it possible to save metrics offline and sync them to W&B later?
By default, wandb.init starts a process that syncs metrics in real time to our cloud hosted app. If your machine is offline, you don’t have internet access, or you just want to hold off on the upload, here’s how to run wandb in offline mode and sync later. You’ll need to set two environment variables.
WANDB_API_KEY=$KEY
, where $KEY is the API Key from your settings pageWANDB_MODE="offline"
And here’s a sample of what this would look like in your script:
import wandb import os os.environ["WANDB_MODE"] = "offline" config = { "dataset": "CIFAR10", "machine": "offline cluster", "model": "CNN", "learning_rate": 0.01, "batch_size": 128, } wandb.init(project="offline-demo") for i in range(100): wandb.log({"accuracy": i})
Here’s a sample terminal output:
python offline.py
wandb: Tracking run with wandb version 0.13.1
wandb: W&B syncing is set to `offline` in this directory.
wandb: Run `wandb online` or set WANDB_MODE=online to enable cloud syncing.
wandb: Waiting for W&B process to finish... (success).
wandb:
wandb:
wandb: Run history:
wandb: accuracy ▁▁▁▁▂▂▂▂▂▃▃▃▃▃▃▄▄▄▄▄▅▅▅▅▅▅▆▆▆▆▆▇▇▇▇▇▇███
wandb:
wandb: Run summary:
wandb: accuracy 99
wandb:
wandb: You can sync this run to the cloud by running:
wandb: wandb sync /Users/hamel/wandb-nbdev/docs/wandb/offline-run-20220819_111619-1kj5i84c
wandb: Find logs at: ./wandb/offline-run-20220819_111619-1kj5i84c/logs
And once you’re ready, just run a sync command to send that folder to the cloud with the following command:
wandb sync wandb/dryrun-folder-name
!wandb sync wandb/latest-run
Find logs at: /Users/hamel/wandb-nbdev/docs/wandb/debug-cli.hamel.log
Syncing: https://wandb.ai/github-wandb/offline-demo/runs/1kj5i84c ... done.