__init__.pyi 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. """Use wandb to track machine learning work.
  2. Train and fine-tune models, manage models from experimentation to production.
  3. For guides and examples, see https://docs.wandb.ai.
  4. For scripts and interactive notebooks, see https://github.com/wandb/examples.
  5. For reference documentation, see https://docs.wandb.ai/models/ref/python.
  6. """
  7. from __future__ import annotations
  8. from wandb.sdk.lib.deprecation import UNSET, DoNotSet
  9. __all__ = (
  10. "__version__", # doc:exclude
  11. "init",
  12. "finish",
  13. "setup",
  14. "login",
  15. "save", # doc:exclude
  16. "sweep",
  17. "controller",
  18. "agent",
  19. "config", # doc:exclude
  20. "log", # doc:exclude
  21. "summary", # doc:exclude
  22. "Api",
  23. "Graph", # doc:exclude
  24. "Image",
  25. "Plotly",
  26. "Video",
  27. "Audio",
  28. "Table",
  29. "Html",
  30. "box3d",
  31. "Object3D",
  32. "Molecule",
  33. "Histogram",
  34. "ArtifactTTL", # doc:exclude
  35. "log_artifact", # doc:exclude
  36. "use_artifact", # doc:exclude
  37. "log_model", # doc:exclude
  38. "use_model", # doc:exclude
  39. "link_model", # doc:exclude
  40. "define_metric", # doc:exclude
  41. "Error", # doc:exclude
  42. "termsetup", # doc:exclude
  43. "termlog", # doc:exclude
  44. "termerror", # doc:exclude
  45. "termwarn", # doc:exclude
  46. "Artifact",
  47. "Settings",
  48. "teardown",
  49. "watch", # doc:exclude
  50. "unwatch", # doc:exclude
  51. "plot", # doc:exclude
  52. "plot_table",
  53. "restore",
  54. "Run",
  55. )
  56. import os
  57. from collections.abc import Iterable, Sequence
  58. from typing import TYPE_CHECKING, Any, Callable, Literal, TextIO
  59. import wandb.plot as plot
  60. from wandb.apis import InternalApi
  61. from wandb.apis import PublicApi as Api
  62. from wandb.data_types import (
  63. Audio,
  64. Graph,
  65. Histogram,
  66. Html,
  67. Image,
  68. Molecule,
  69. Object3D,
  70. Plotly,
  71. Table,
  72. Video,
  73. box3d,
  74. )
  75. from wandb.errors import Error
  76. from wandb.errors.term import termerror, termlog, termsetup, termwarn
  77. from wandb.sdk import Artifact, Settings, wandb_config, wandb_metric, wandb_summary
  78. from wandb.sdk.artifacts.artifact_ttl import ArtifactTTL
  79. from wandb.sdk.lib.filesystem import PolicyName
  80. from wandb.sdk.lib.paths import FilePathStr, StrPath
  81. from wandb.sdk.wandb_run import Run
  82. from wandb.sdk.wandb_setup import _WandbSetup
  83. from wandb.wandb_controller import _WandbController
  84. if TYPE_CHECKING:
  85. import torch # type: ignore [import-not-found]
  86. import wandb
  87. from wandb.plot import CustomChart
  88. __version__: str = "0.26.0"
  89. run: Run | None
  90. config: wandb_config.Config
  91. summary: wandb_summary.Summary
  92. # private attributes
  93. api: InternalApi
  94. patched: dict[str, list[Callable]]
  95. def require(
  96. requirement: str | Iterable[str] | None = None,
  97. experiment: str | Iterable[str] | None = None,
  98. ) -> None:
  99. """Indicate which experimental features are used by the script.
  100. This should be called before any other `wandb` functions, ideally right
  101. after importing `wandb`.
  102. Args:
  103. requirement: The name of a feature to require or an iterable of
  104. feature names.
  105. experiment: An alias for `requirement`.
  106. Raises:
  107. wandb.errors.UnsupportedError: If a feature name is unknown.
  108. """
  109. ...
  110. def setup(settings: Settings | None = None) -> _WandbSetup:
  111. """Prepares W&B for use in the current process and its children.
  112. You can usually ignore this as it is implicitly called by `wandb.init()`.
  113. When using wandb in multiple processes, calling `wandb.setup()`
  114. in the parent process before starting child processes may improve
  115. performance and resource utilization.
  116. Note that `wandb.setup()` modifies `os.environ`, and it is important
  117. that child processes inherit the modified environment variables.
  118. See also `wandb.teardown()`.
  119. Args:
  120. settings: Configuration settings to apply globally. These can be
  121. overridden by subsequent `wandb.init()` calls.
  122. Example:
  123. ```python
  124. import multiprocessing
  125. import wandb
  126. def run_experiment(params):
  127. with wandb.init(config=params):
  128. # Run experiment
  129. pass
  130. if __name__ == "__main__":
  131. # Start backend and set global config
  132. wandb.setup(settings={"project": "my_project"})
  133. # Define experiment parameters
  134. experiment_params = [
  135. {"learning_rate": 0.01, "epochs": 10},
  136. {"learning_rate": 0.001, "epochs": 20},
  137. ]
  138. # Start multiple processes, each running a separate experiment
  139. processes = []
  140. for params in experiment_params:
  141. p = multiprocessing.Process(target=run_experiment, args=(params,))
  142. p.start()
  143. processes.append(p)
  144. # Wait for all processes to complete
  145. for p in processes:
  146. p.join()
  147. # Optional: Explicitly shut down the backend
  148. wandb.teardown()
  149. ```
  150. """
  151. ...
  152. def teardown(exit_code: int | None = None) -> None:
  153. """Waits for W&B to finish and frees resources.
  154. Completes any runs that were not explicitly finished
  155. using `run.finish()` and waits for all data to be uploaded.
  156. It is recommended to call this at the end of a session
  157. that used `wandb.setup()`. It is invoked automatically
  158. in an `atexit` hook, but this is not reliable in certain setups
  159. such as when using Python's `multiprocessing` module.
  160. """
  161. ...
  162. def init(
  163. entity: str | None = None,
  164. project: str | None = None,
  165. dir: StrPath | None = None,
  166. id: str | None = None,
  167. name: str | None = None,
  168. notes: str | None = None,
  169. tags: Sequence[str] | None = None,
  170. config: dict[str, Any] | str | None = None,
  171. config_exclude_keys: list[str] | None = None,
  172. config_include_keys: list[str] | None = None,
  173. allow_val_change: bool | None = None,
  174. group: str | None = None,
  175. job_type: str | None = None,
  176. mode: Literal["online", "offline", "disabled", "shared"] | None = None,
  177. force: bool | None = None,
  178. reinit: (
  179. bool
  180. | Literal[
  181. None,
  182. "default",
  183. "return_previous",
  184. "finish_previous",
  185. "create_new",
  186. ]
  187. ) = None,
  188. resume: bool | Literal["allow", "never", "must", "auto"] | None = None,
  189. resume_from: str | None = None,
  190. fork_from: str | None = None,
  191. save_code: bool | None = None,
  192. tensorboard: bool | None = None,
  193. sync_tensorboard: bool | None = None,
  194. monitor_gym: bool | None = None,
  195. settings: Settings | dict[str, Any] | None = None,
  196. anonymous: DoNotSet = UNSET,
  197. ) -> Run:
  198. r"""Start a new run to track and log to W&B.
  199. In an ML training pipeline, you could add `wandb.init()` to the beginning of
  200. your training script as well as your evaluation script, and each piece would
  201. be tracked as a run in W&B.
  202. `wandb.init()` spawns a new background process to log data to a run, and it
  203. also syncs data to https://wandb.ai by default, so you can see your results
  204. in real-time. When you're done logging data, call `wandb.Run.finish()` to end the run.
  205. If you don't call `run.finish()`, the run will end when your script exits.
  206. Run IDs must not contain any of the following special characters `/ \ # ? % :`
  207. Args:
  208. entity: The username or team name the runs are logged to.
  209. The entity must already exist, so ensure you create your account
  210. or team in the UI before starting to log runs. If not specified, the
  211. run will default your default entity. To change the default entity,
  212. go to your settings and update the
  213. "Default location to create new projects" under "Default team".
  214. project: The name of the project under which this run will be logged.
  215. If not specified, we use a heuristic to infer the project name based
  216. on the system, such as checking the git root or the current program
  217. file. If we can't infer the project name, the project will default to
  218. `"uncategorized"`.
  219. dir: The absolute path to the directory where experiment logs and
  220. metadata files are stored. If not specified, this defaults
  221. to the `./wandb` directory. Note that this does not affect the
  222. location where artifacts are stored when calling `download()`.
  223. id: A unique identifier for this run, used for resuming. It must be unique
  224. within the project and cannot be reused once a run is deleted. For
  225. a short descriptive name, use the `name` field,
  226. or for saving hyperparameters to compare across runs, use `config`.
  227. name: A short display name for this run, which appears in the UI to help
  228. you identify it. By default, we generate a random two-word name
  229. allowing easy cross-reference runs from table to charts. Keeping these
  230. run names brief enhances readability in chart legends and tables. For
  231. saving hyperparameters, we recommend using the `config` field.
  232. notes: A detailed description of the run, similar to a commit message in
  233. Git. Use this argument to capture any context or details that may
  234. help you recall the purpose or setup of this run in the future.
  235. tags: A list of tags to label this run in the UI. Tags are helpful for
  236. organizing runs or adding temporary identifiers like "baseline" or
  237. "production." You can easily add, remove tags, or filter by tags in
  238. the UI.
  239. If resuming a run, the tags provided here will replace any existing
  240. tags. To add tags to a resumed run without overwriting the current
  241. tags, use `run.tags += ("new_tag",)` after calling `run = wandb.init()`.
  242. config: Sets `wandb.config`, a dictionary-like object for storing input
  243. parameters to your run, such as model hyperparameters or data
  244. preprocessing settings.
  245. The config appears in the UI in an overview page, allowing you to
  246. group, filter, and sort runs based on these parameters.
  247. Keys should not contain periods (`.`), and values should be
  248. smaller than 10 MB.
  249. If a dictionary, `argparse.Namespace`, or `absl.flags.FLAGS` is
  250. provided, the key-value pairs will be loaded directly into
  251. `wandb.config`.
  252. If a string is provided, it is interpreted as a path to a YAML file,
  253. from which configuration values will be loaded into `wandb.config`.
  254. config_exclude_keys: A list of specific keys to exclude from `wandb.config`.
  255. config_include_keys: A list of specific keys to include in `wandb.config`.
  256. allow_val_change: Controls whether config values can be modified after their
  257. initial set. By default, an exception is raised if a config value is
  258. overwritten. For tracking variables that change during training, such as
  259. a learning rate, consider using `wandb.log()` instead. By default, this
  260. is `False` in scripts and `True` in Notebook environments.
  261. group: Specify a group name to organize individual runs as part of a larger
  262. experiment. This is useful for cases like cross-validation or running
  263. multiple jobs that train and evaluate a model on different test sets.
  264. Grouping allows you to manage related runs collectively in the UI,
  265. making it easy to toggle and review results as a unified experiment.
  266. job_type: Specify the type of run, especially helpful when organizing runs
  267. within a group as part of a larger experiment. For example, in a group,
  268. you might label runs with job types such as "train" and "eval".
  269. Defining job types enables you to easily filter and group similar runs
  270. in the UI, facilitating direct comparisons.
  271. mode: Specifies how run data is managed, with the following options:
  272. - `"online"` (default): Enables live syncing with W&B when a network
  273. connection is available, with real-time updates to visualizations.
  274. - `"offline"`: Suitable for air-gapped or offline environments; data
  275. is saved locally and can be synced later. Ensure the run folder
  276. is preserved to enable future syncing.
  277. - `"disabled"`: Disables all W&B functionality, making the run’s methods
  278. no-ops. Typically used in testing to bypass W&B operations.
  279. - `"shared"`: (This is an experimental feature). Allows multiple processes,
  280. possibly on different machines, to simultaneously log to the same run.
  281. In this approach you use a primary node and one or more worker nodes
  282. to log data to the same run. Within the primary node you
  283. initialize a run. For each worker node, initialize a run
  284. using the run ID used by the primary node.
  285. force: Determines if a W&B login is required to run the script. If `True`,
  286. the user must be logged in to W&B; otherwise, the script will not
  287. proceed. If `False` (default), the script can proceed without a login,
  288. switching to offline mode if the user is not logged in.
  289. reinit: Shorthand for the "reinit" setting. Determines the behavior of
  290. `wandb.init()` when a run is active.
  291. resume: Controls the behavior when resuming a run with the specified `id`.
  292. Available options are:
  293. - `"allow"`: If a run with the specified `id` exists, it will resume
  294. from the last step; otherwise, a new run will be created.
  295. - `"never"`: If a run with the specified `id` exists, an error will
  296. be raised. If no such run is found, a new run will be created.
  297. - `"must"`: If a run with the specified `id` exists, it will resume
  298. from the last step. If no run is found, an error will be raised.
  299. - `"auto"`: Automatically resumes the previous run if it crashed on
  300. this machine; otherwise, starts a new run.
  301. - `True`: Deprecated. Use `"auto"` instead.
  302. - `False`: Deprecated. Use the default behavior (leaving `resume`
  303. unset) to always start a new run.
  304. If `resume` is set, `fork_from` and `resume_from` cannot be
  305. used. When `resume` is unset, the system will always start a new run.
  306. resume_from: Specifies a moment in a previous run to resume a run from,
  307. using the format `{run_id}?_step={step}`. This allows users to truncate
  308. the history logged to a run at an intermediate step and resume logging
  309. from that step. The target run must be in the same project.
  310. If an `id` argument is also provided, the `resume_from` argument will
  311. take precedence.
  312. `resume`, `resume_from` and `fork_from` cannot be used together, only
  313. one of them can be used at a time.
  314. Note that this feature is in beta and may change in the future.
  315. fork_from: Specifies a point in a previous run from which to fork a new
  316. run, using the format `{id}?_step={step}`. This creates a new run that
  317. resumes logging from the specified step in the target run’s history.
  318. The target run must be part of the current project.
  319. If an `id` argument is also provided, it must be different from the
  320. `fork_from` argument, an error will be raised if they are the same.
  321. `resume`, `resume_from` and `fork_from` cannot be used together, only
  322. one of them can be used at a time.
  323. Note that this feature is in beta and may change in the future.
  324. save_code: Enables saving the main script or notebook to W&B, aiding in
  325. experiment reproducibility and allowing code comparisons across runs in
  326. the UI. By default, this is disabled, but you can change the default to
  327. enable on your settings page.
  328. tensorboard: Deprecated. Use `sync_tensorboard` instead.
  329. sync_tensorboard: Enables automatic syncing of W&B logs from TensorBoard
  330. or TensorBoardX, saving relevant event files for viewing in
  331. the W&B UI.
  332. monitor_gym: Enables automatic logging of videos of the environment when
  333. using OpenAI Gym.
  334. settings: Specifies a dictionary or `wandb.Settings` object with advanced
  335. settings for the run.
  336. Returns:
  337. A `Run` object.
  338. Raises:
  339. Error: If some unknown or internal error happened during the run
  340. initialization.
  341. AuthenticationError: If the user failed to provide valid credentials.
  342. CommError: If there was a problem communicating with the WandB server.
  343. UsageError: If the user provided invalid arguments.
  344. KeyboardInterrupt: If user interrupts the run.
  345. Examples:
  346. `wandb.init()` returns a `Run` object. Use the run object to log data,
  347. save artifacts, and manage the run lifecycle.
  348. ```python
  349. import wandb
  350. config = {"lr": 0.01, "batch_size": 32}
  351. with wandb.init(config=config) as run:
  352. # Log accuracy and loss to the run
  353. acc = 0.95 # Example accuracy
  354. loss = 0.05 # Example loss
  355. run.log({"accuracy": acc, "loss": loss})
  356. ```
  357. """
  358. ...
  359. def finish(
  360. exit_code: int | None = None,
  361. quiet: bool | None = None,
  362. ) -> None:
  363. """Finish a run and upload any remaining data.
  364. Marks the completion of a W&B run and ensures all data is synced to the server.
  365. The run's final state is determined by its exit conditions and sync status.
  366. Run States:
  367. - Running: Active run that is logging data and/or sending heartbeats.
  368. - Crashed: Run that stopped sending heartbeats unexpectedly.
  369. - Finished: Run completed successfully (`exit_code=0`) with all data synced.
  370. - Failed: Run completed with errors (`exit_code!=0`).
  371. Args:
  372. exit_code: Integer indicating the run's exit status. Use 0 for success,
  373. any other value marks the run as failed.
  374. quiet: Deprecated. Configure logging verbosity using `wandb.Settings(quiet=...)`.
  375. """
  376. ...
  377. def login(
  378. key: str | None = None,
  379. relogin: bool | None = None,
  380. host: str | None = None,
  381. force: bool | None = None,
  382. timeout: int | None = None,
  383. verify: bool = False,
  384. referrer: str | None = None,
  385. anonymous: DoNotSet = UNSET,
  386. ) -> bool:
  387. """Log into W&B.
  388. You generally don't have to use this because most W&B methods that need
  389. authentication can log in implicitly. This is the programmatic counterpart
  390. to the `wandb login` CLI.
  391. This updates global credentials for the session (affecting all wandb usage
  392. in the current Python process after this call) and possibly the .netrc file.
  393. If the identity_token_file setting is set, like through the
  394. WANDB_IDENTITY_TOKEN_FILE environment variable, then this is a no-op.
  395. Otherwise, if an explicit API key is provided, it is used and written to
  396. the system .netrc file. If no key is provided, but the session is already
  397. authenticated, then the session key is used for verification (if verify
  398. is True) and the .netrc file is not updated.
  399. If none of the above is true, then this gets the API key from the first of:
  400. - The WANDB_API_KEY environment variable
  401. - The api_key setting in a system or workspace settings file
  402. - The .netrc file (either ~/.netrc, ~/_netrc or the path specified by the
  403. NETRC environment variable)
  404. - An interactive prompt (if available)
  405. Args:
  406. key: The API key to use.
  407. relogin: If true, get the API key from an interactive prompt, skipping
  408. reading .netrc, environment variables, etc.
  409. host: The W&B server URL to connect to.
  410. force: If true, disallows selecting offline mode in the interactive
  411. prompt.
  412. timeout: Number of seconds to wait for user input in the interactive
  413. prompt. This can be used as a failsafe if an interactive prompt
  414. is incorrectly shown in a non-interactive environment.
  415. verify: Verify the credentials with the W&B server and raise an
  416. AuthenticationError on failure.
  417. referrer: The referrer to use in the URL login request for analytics.
  418. Returns:
  419. bool: If `key` is configured.
  420. Raises:
  421. AuthenticationError: If `api_key` fails verification with the server.
  422. UsageError: If `api_key` cannot be configured and no tty.
  423. """
  424. ...
  425. def log(
  426. data: dict[str, Any],
  427. step: int | None = None,
  428. commit: bool | None = None,
  429. ) -> None:
  430. """Upload run data.
  431. Use `log` to log data from runs, such as scalars, images, video,
  432. histograms, plots, and tables. See [Log objects and media](https://docs.wandb.ai/models/track/log) for
  433. code snippets, best practices, and more.
  434. Basic usage:
  435. ```python
  436. import wandb
  437. with wandb.init() as run:
  438. run.log({"train-loss": 0.5, "accuracy": 0.9})
  439. ```
  440. The previous code snippet saves the loss and accuracy to the run's
  441. history and updates the summary values for these metrics.
  442. Visualize logged data in a workspace at [wandb.ai](https://wandb.ai),
  443. or locally on a [self-hosted instance](https://docs.wandb.ai/platform/hosting)
  444. of the W&B app, or export data to visualize and explore locally, such as in a
  445. Jupyter notebook, with the [Public API](https://docs.wandb.ai/models/track/public-api-guide).
  446. Logged values don't have to be scalars. You can log any
  447. [W&B supported Data Type](https://docs.wandb.ai/models/ref/python/data-types)
  448. such as images, audio, video, and more. For example, you can use
  449. `wandb.Table` to log structured data. See
  450. [Log tables, visualize and query data](https://docs.wandb.ai/models/tables/tables-walkthrough)
  451. tutorial for more details.
  452. W&B organizes metrics with a forward slash (`/`) in their name
  453. into sections named using the text before the final slash. For example,
  454. the following results in two sections named "train" and "validate":
  455. ```python
  456. with wandb.init() as run:
  457. # Log metrics in the "train" section.
  458. run.log(
  459. {
  460. "train/accuracy": 0.9,
  461. "train/loss": 30,
  462. "validate/accuracy": 0.8,
  463. "validate/loss": 20,
  464. }
  465. )
  466. ```
  467. Only one level of nesting is supported; `run.log({"a/b/c": 1})`
  468. produces a section named "a".
  469. `run.log()` is not intended to be called more than a few times per second.
  470. For optimal performance, limit your logging to once every N iterations,
  471. or collect data over multiple iterations and log it in a single step.
  472. By default, each call to `log` creates a new "step".
  473. The step must always increase, and it is not possible to log
  474. to a previous step. You can use any metric as the X axis in charts.
  475. See [Custom log axes](https://docs.wandb.ai/models/track/log/customize-logging-axes)
  476. for more details.
  477. In many cases, it is better to treat the W&B step like
  478. you'd treat a timestamp rather than a training step.
  479. ```python
  480. with wandb.init() as run:
  481. # Example: log an "epoch" metric for use as an X axis.
  482. run.log({"epoch": 40, "train-loss": 0.5})
  483. ```
  484. It is possible to use multiple `wandb.Run.log()` invocations to log to
  485. the same step with the `step` and `commit` parameters.
  486. The following are all equivalent:
  487. ```python
  488. with wandb.init() as run:
  489. # Normal usage:
  490. run.log({"train-loss": 0.5, "accuracy": 0.8})
  491. run.log({"train-loss": 0.4, "accuracy": 0.9})
  492. # Implicit step without auto-incrementing:
  493. run.log({"train-loss": 0.5}, commit=False)
  494. run.log({"accuracy": 0.8})
  495. run.log({"train-loss": 0.4}, commit=False)
  496. run.log({"accuracy": 0.9})
  497. # Explicit step:
  498. run.log({"train-loss": 0.5}, step=current_step)
  499. run.log({"accuracy": 0.8}, step=current_step)
  500. current_step += 1
  501. run.log({"train-loss": 0.4}, step=current_step)
  502. run.log({"accuracy": 0.9}, step=current_step, commit=True)
  503. ```
  504. Args:
  505. data: A `dict` with `str` keys and values that are serializable
  506. Python objects including: `int`, `float` and `string`;
  507. any of the `wandb.data_types`; lists, tuples and NumPy arrays
  508. of serializable Python objects; other `dict`s of this
  509. structure.
  510. step: The step number to log. If `None`, then an implicit
  511. auto-incrementing step is used. See the notes in
  512. the description.
  513. commit: If true, finalize and upload the step. If false, then
  514. accumulate data for the step. See the notes in the description.
  515. If `step` is `None`, then the default is `commit=True`;
  516. otherwise, the default is `commit=False`.
  517. Examples:
  518. For more and more detailed examples, see
  519. [our guides to logging](https://docs.wandb.ai/models/track/log).
  520. Basic usage
  521. ```python
  522. import wandb
  523. with wandb.init() as run:
  524. run.log({"train-loss": 0.5, "accuracy": 0.9
  525. ```
  526. Incremental logging
  527. ```python
  528. import wandb
  529. with wandb.init() as run:
  530. run.log({"loss": 0.2}, commit=False)
  531. # Somewhere else when I'm ready to report this step:
  532. run.log({"accuracy": 0.8})
  533. ```
  534. Histogram
  535. ```python
  536. import numpy as np
  537. import wandb
  538. # sample gradients at random from normal distribution
  539. gradients = np.random.randn(100, 100)
  540. with wandb.init() as run:
  541. run.log({"gradients": wandb.Histogram(gradients)})
  542. ```
  543. Image from NumPy
  544. ```python
  545. import numpy as np
  546. import wandb
  547. with wandb.init() as run:
  548. examples = []
  549. for i in range(3):
  550. pixels = np.random.randint(low=0, high=256, size=(100, 100, 3))
  551. image = wandb.Image(pixels, caption=f"random field {i}")
  552. examples.append(image)
  553. run.log({"examples": examples})
  554. ```
  555. Image from PIL
  556. ```python
  557. import numpy as np
  558. from PIL import Image as PILImage
  559. import wandb
  560. with wandb.init() as run:
  561. examples = []
  562. for i in range(3):
  563. pixels = np.random.randint(
  564. low=0,
  565. high=256,
  566. size=(100, 100, 3),
  567. dtype=np.uint8,
  568. )
  569. pil_image = PILImage.fromarray(pixels, mode="RGB")
  570. image = wandb.Image(pil_image, caption=f"random field {i}")
  571. examples.append(image)
  572. run.log({"examples": examples})
  573. ```
  574. Video from NumPy
  575. ```python
  576. import numpy as np
  577. import wandb
  578. with wandb.init() as run:
  579. # axes are (time, channel, height, width)
  580. frames = np.random.randint(
  581. low=0,
  582. high=256,
  583. size=(10, 3, 100, 100),
  584. dtype=np.uint8,
  585. )
  586. run.log({"video": wandb.Video(frames, fps=4)})
  587. ```
  588. Matplotlib plot
  589. ```python
  590. from matplotlib import pyplot as plt
  591. import numpy as np
  592. import wandb
  593. with wandb.init() as run:
  594. fig, ax = plt.subplots()
  595. x = np.linspace(0, 10)
  596. y = x * x
  597. ax.plot(x, y) # plot y = x^2
  598. run.log({"chart": fig})
  599. ```
  600. PR Curve
  601. ```python
  602. import wandb
  603. with wandb.init() as run:
  604. run.log({"pr": wandb.plot.pr_curve(y_test, y_probas, labels)})
  605. ```
  606. 3D Object
  607. ```python
  608. import wandb
  609. with wandb.init() as run:
  610. run.log(
  611. {
  612. "generated_samples": [
  613. wandb.Object3D(open("sample.obj")),
  614. wandb.Object3D(open("sample.gltf")),
  615. wandb.Object3D(open("sample.glb")),
  616. ]
  617. }
  618. )
  619. ```
  620. Raises:
  621. wandb.Error: If called before `wandb.init()`.
  622. ValueError: If invalid data is passed.
  623. """
  624. ...
  625. def save(
  626. glob_str: str | os.PathLike,
  627. base_path: str | os.PathLike | None = None,
  628. policy: PolicyName = "live",
  629. ) -> bool | list[str]:
  630. """Sync one or more files to W&B.
  631. Relative paths are relative to the current working directory.
  632. A Unix glob, such as "myfiles/*", is expanded at the time `save` is
  633. called regardless of the `policy`. In particular, new files are not
  634. picked up automatically.
  635. A `base_path` may be provided to control the directory structure of
  636. uploaded files. It should be a prefix of `glob_str`, and the directory
  637. structure beneath it is preserved.
  638. When given an absolute path or glob and no `base_path`, one
  639. directory level is preserved as in the example above.
  640. Files are automatically deduplicated: calling `save()` multiple times
  641. on the same file without modifications will not re-upload it.
  642. Args:
  643. glob_str: A relative or absolute path or Unix glob.
  644. base_path: A path to use to infer a directory structure; see examples.
  645. policy: One of `live`, `now`, or `end`.
  646. - live: upload the file as it changes, overwriting the previous version
  647. - now: upload the file once now
  648. - end: upload file when the run ends
  649. Returns:
  650. Paths to the symlinks created for the matched files.
  651. For historical reasons, this may return a boolean in legacy code.
  652. ```python
  653. import wandb
  654. run = wandb.init()
  655. run.save("these/are/myfiles/*")
  656. # => Saves files in a "these/are/myfiles/" folder in the run.
  657. run.save("these/are/myfiles/*", base_path="these")
  658. # => Saves files in an "are/myfiles/" folder in the run.
  659. run.save("/Users/username/Documents/run123/*.txt")
  660. # => Saves files in a "run123/" folder in the run. See note below.
  661. run.save("/Users/username/Documents/run123/*.txt", base_path="/Users")
  662. # => Saves files in a "username/Documents/run123/" folder in the run.
  663. run.save("files/*/saveme.txt")
  664. # => Saves each "saveme.txt" file in an appropriate subdirectory
  665. # of "files/".
  666. # Explicitly finish the run since a context manager is not used.
  667. run.finish()
  668. ```
  669. """
  670. ...
  671. def sweep(
  672. sweep: dict | Callable,
  673. entity: str | None = None,
  674. project: str | None = None,
  675. prior_runs: list[str] | None = None,
  676. ) -> str:
  677. """Initialize a hyperparameter sweep.
  678. Search for hyperparameters that optimizes a cost function
  679. of a machine learning model by testing various combinations.
  680. Make note the unique identifier, `sweep_id`, that is returned.
  681. At a later step provide the `sweep_id` to a sweep agent.
  682. See [Sweep configuration structure](https://docs.wandb.ai/models/sweeps/define-sweep-configuration)
  683. for information on how to define your sweep.
  684. Args:
  685. sweep: The configuration of a hyperparameter search.
  686. (or configuration generator).
  687. If you provide a callable, ensure that the callable does
  688. not take arguments and that it returns a dictionary that
  689. conforms to the W&B sweep config spec.
  690. entity: The username or team name where you want to send W&B
  691. runs created by the sweep to. Ensure that the entity you
  692. specify already exists. If you don't specify an entity,
  693. the run will be sent to your default entity,
  694. which is usually your username.
  695. project: The name of the project where W&B runs created from
  696. the sweep are sent to. If the project is not specified, the
  697. run is sent to a project labeled 'Uncategorized'.
  698. prior_runs: The run IDs of existing runs to add to this sweep.
  699. Returns:
  700. str: A unique identifier for the sweep.
  701. """
  702. ...
  703. def controller(
  704. sweep_id_or_config: str | dict | None = None,
  705. entity: str | None = None,
  706. project: str | None = None,
  707. ) -> _WandbController:
  708. """Public sweep controller constructor.
  709. Examples:
  710. ```python
  711. import wandb
  712. tuner = wandb.controller(...)
  713. print(tuner.sweep_config)
  714. print(tuner.sweep_id)
  715. tuner.configure_search(...)
  716. tuner.configure_stopping(...)
  717. ```
  718. """
  719. ...
  720. def agent(
  721. sweep_id: str,
  722. function: Callable | None = None,
  723. entity: str | None = None,
  724. project: str | None = None,
  725. count: int | None = None,
  726. forward_signals: bool = False,
  727. ) -> None:
  728. """Start one or more sweep agents.
  729. The sweep agent uses the `sweep_id` to know which sweep it
  730. is a part of, what function to execute, and (optionally) how
  731. many agents to run.
  732. Args:
  733. sweep_id: The unique identifier for a sweep. A sweep ID
  734. is generated by W&B CLI or Python SDK.
  735. function: A function to call instead of the "program"
  736. specified in the sweep config.
  737. entity: The username or team name where you want to send W&B
  738. runs created by the sweep to. Ensure that the entity you
  739. specify already exists. If you don't specify an entity,
  740. the run will be sent to your default entity,
  741. which is usually your username.
  742. project: The name of the project where W&B runs created from
  743. the sweep are sent to. If the project is not specified, the
  744. run is sent to a project labeled "Uncategorized".
  745. count: The number of sweep config trials to try.
  746. forward_signals: Whether to forward signals the agent receives
  747. to the child processes. Only supported by CLI agent.
  748. """
  749. ...
  750. def define_metric(
  751. name: str,
  752. step_metric: str | wandb_metric.Metric | None = None,
  753. step_sync: bool | None = None,
  754. hidden: bool | None = None,
  755. summary: str | None = None,
  756. goal: str | None = None,
  757. overwrite: bool | None = None,
  758. ) -> wandb_metric.Metric:
  759. """Customize metrics logged with `wandb.Run.log()`.
  760. Args:
  761. name: The name of the metric to customize.
  762. step_metric: The name of another metric to serve as the X-axis
  763. for this metric in automatically generated charts.
  764. step_sync: Automatically insert the last value of step_metric into
  765. `wandb.Run.log()` if it is not provided explicitly. Defaults to True
  766. if step_metric is specified.
  767. hidden: Hide this metric from automatic plots.
  768. summary: Specify aggregate metrics added to summary.
  769. Supported aggregations include "min", "max", "mean", "last",
  770. "first", "best", "copy" and "none". "none" prevents a summary
  771. from being generated. "best" is used together with the goal
  772. parameter, "best" is deprecated and should not be used, use
  773. "min" or "max" instead. "copy" is deprecated and should not be
  774. used.
  775. goal: Specify how to interpret the "best" summary type.
  776. Supported options are "minimize" and "maximize". "goal" is
  777. deprecated and should not be used, use "min" or "max" instead.
  778. overwrite: If false, then this call is merged with previous
  779. `define_metric` calls for the same metric by using their
  780. values for any unspecified parameters. If true, then
  781. unspecified parameters overwrite values specified by
  782. previous calls.
  783. Returns:
  784. An object that represents this call but can otherwise be discarded.
  785. """
  786. ...
  787. def log_artifact(
  788. artifact_or_path: Artifact | StrPath,
  789. name: str | None = None,
  790. type: str | None = None,
  791. aliases: list[str] | None = None,
  792. tags: list[str] | None = None,
  793. ) -> Artifact:
  794. """Declare an artifact as an output of a run.
  795. Args:
  796. artifact_or_path: (str or Artifact) A path to the contents of this artifact,
  797. can be in the following forms:
  798. - `/local/directory`
  799. - `/local/directory/file.txt`
  800. - `s3://bucket/path`
  801. You can also pass an Artifact object created by calling
  802. `wandb.Artifact`.
  803. name: (str, optional) An artifact name. Valid names can be in the following forms:
  804. - name:version
  805. - name:alias
  806. - digest
  807. This will default to the basename of the path prepended with the current
  808. run id if not specified.
  809. type: (str) The type of artifact to log, examples include `dataset`, `model`
  810. aliases: (list, optional) Aliases to apply to this artifact,
  811. defaults to `["latest"]`
  812. tags: (list, optional) Tags to apply to this artifact, if any.
  813. Returns:
  814. An `Artifact` object.
  815. """
  816. ...
  817. def use_artifact(
  818. artifact_or_name: str | Artifact,
  819. type: str | None = None,
  820. aliases: list[str] | None = None,
  821. use_as: str | None = None,
  822. ) -> Artifact:
  823. """Declare an artifact as an input to a run.
  824. Call `download` or `file` on the returned object to get the contents locally.
  825. Args:
  826. artifact_or_name: The name of the artifact to use. May be prefixed
  827. with the name of the project the artifact was logged to
  828. ("entity" or "entity/project"). If no
  829. entity is specified in the name, the Run or API setting's entity is used.
  830. Valid names can be in the following forms
  831. - name:version
  832. - name:alias
  833. type: The type of artifact to use.
  834. aliases: Aliases to apply to this artifact
  835. use_as: This argument is deprecated and does nothing.
  836. Returns:
  837. An `Artifact` object.
  838. Examples:
  839. ```python
  840. import wandb
  841. run = wandb.init(project="<example>")
  842. # Use an artifact by name and alias
  843. artifact_a = run.use_artifact(artifact_or_name="<name>:<alias>")
  844. # Use an artifact by name and version
  845. artifact_b = run.use_artifact(artifact_or_name="<name>:v<version>")
  846. # Use an artifact by entity/project/name:alias
  847. artifact_c = run.use_artifact(artifact_or_name="<entity>/<project>/<name>:<alias>")
  848. # Use an artifact by entity/project/name:version
  849. artifact_d = run.use_artifact(
  850. artifact_or_name="<entity>/<project>/<name>:v<version>"
  851. )
  852. # Explicitly finish the run since a context manager is not used.
  853. run.finish()
  854. ```
  855. """
  856. ...
  857. def log_model(
  858. path: StrPath,
  859. name: str | None = None,
  860. aliases: list[str] | None = None,
  861. ) -> None:
  862. """Logs a model artifact containing the contents inside the 'path' to a run and marks it as an output to this run.
  863. The name of model artifact can only contain alphanumeric characters,
  864. underscores, and hyphens.
  865. Args:
  866. path: (str) A path to the contents of this model,
  867. can be in the following forms:
  868. - `/local/directory`
  869. - `/local/directory/file.txt`
  870. - `s3://bucket/path`
  871. name: A name to assign to the model artifact that
  872. the file contents will be added to. This will default to the
  873. basename of the path prepended with the current run id if
  874. not specified.
  875. aliases: Aliases to apply to the created model artifact,
  876. defaults to `["latest"]`
  877. Raises:
  878. ValueError: If name has invalid special characters.
  879. Returns:
  880. None
  881. """
  882. ...
  883. def use_model(name: str) -> FilePathStr:
  884. """Download the files logged in a model artifact 'name'.
  885. Args:
  886. name: A model artifact name. 'name' must match the name of an existing logged
  887. model artifact. May be prefixed with `entity/project/`. Valid names
  888. can be in the following forms
  889. - model_artifact_name:version
  890. - model_artifact_name:alias
  891. Returns:
  892. path (str): Path to downloaded model artifact file(s).
  893. Raises:
  894. AssertionError: If model artifact 'name' is of a type that does
  895. not contain the substring 'model'.
  896. """
  897. ...
  898. def link_model(
  899. path: StrPath,
  900. registered_model_name: str,
  901. name: str | None = None,
  902. aliases: list[str] | None = None,
  903. ) -> Artifact | None:
  904. """Log a model artifact version and link it to a registered model in the model registry.
  905. Linked model versions are visible in the UI for the specified registered model.
  906. This method will:
  907. - Check if 'name' model artifact has been logged. If so, use the artifact version that matches the files
  908. located at 'path' or log a new version. Otherwise log files under 'path' as a new model artifact, 'name'
  909. of type 'model'.
  910. - Check if registered model with name 'registered_model_name' exists in the 'model-registry' project.
  911. If not, create a new registered model with name 'registered_model_name'.
  912. - Link version of model artifact 'name' to registered model, 'registered_model_name'.
  913. - Attach aliases from 'aliases' list to the newly linked model artifact version.
  914. Args:
  915. path: (str) A path to the contents of this model, can be in the
  916. following forms:
  917. - `/local/directory`
  918. - `/local/directory/file.txt`
  919. - `s3://bucket/path`
  920. registered_model_name: The name of the registered model that the
  921. model is to be linked to. A registered model is a collection of
  922. model versions linked to the model registry, typically
  923. representing a team's specific ML Task. The entity that this
  924. registered model belongs to will be derived from the run.
  925. name: The name of the model artifact that files in 'path' will be
  926. logged to. This will default to the basename of the path
  927. prepended with the current run id if not specified.
  928. aliases: Aliases that will only be applied on this linked artifact
  929. inside the registered model. The alias "latest" will always be
  930. applied to the latest version of an artifact that is linked.
  931. Raises:
  932. AssertionError: If registered_model_name is a path or
  933. if model artifact 'name' is of a type that does not contain
  934. the substring 'model'.
  935. ValueError: If name has invalid special characters.
  936. Returns:
  937. The linked artifact if linking was successful, otherwise `None`.
  938. """
  939. ...
  940. def plot_table(
  941. vega_spec_name: str,
  942. data_table: wandb.Table,
  943. fields: dict[str, Any],
  944. string_fields: dict[str, Any] | None = None,
  945. split_table: bool = False,
  946. ) -> CustomChart:
  947. """Creates a custom charts using a Vega-Lite specification and a `wandb.Table`.
  948. This function creates a custom chart based on a Vega-Lite specification and
  949. a data table represented by a `wandb.Table` object. The specification needs
  950. to be predefined and stored in the W&B backend. The function returns a custom
  951. chart object that can be logged to W&B using `wandb.Run.log()`.
  952. Args:
  953. vega_spec_name: The name or identifier of the Vega-Lite spec
  954. that defines the visualization structure.
  955. data_table: A `wandb.Table` object containing the data to be
  956. visualized.
  957. fields: A mapping between the fields in the Vega-Lite spec and the
  958. corresponding columns in the data table to be visualized.
  959. string_fields: A dictionary for providing values for any string constants
  960. required by the custom visualization.
  961. split_table: Whether the table should be split into a separate section
  962. in the W&B UI. If `True`, the table will be displayed in a section named
  963. "Custom Chart Tables". Default is `False`.
  964. Returns:
  965. CustomChart: A custom chart object that can be logged to W&B. To log the
  966. chart, pass the chart object as argument to `wandb.Run.log()`.
  967. Raises:
  968. wandb.Error: If `data_table` is not a `wandb.Table` object.
  969. Example:
  970. ```python
  971. # Create a custom chart using a Vega-Lite spec and the data table.
  972. import wandb
  973. data = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]
  974. table = wandb.Table(data=data, columns=["x", "y"])
  975. fields = {"x": "x", "y": "y", "title": "MY TITLE"}
  976. with wandb.init() as run:
  977. # Training code goes here
  978. # Create a custom title with `string_fields`.
  979. my_custom_chart = wandb.plot_table(
  980. vega_spec_name="wandb/line/v0",
  981. data_table=table,
  982. fields=fields,
  983. string_fields={"title": "Title"},
  984. )
  985. run.log({"custom_chart": my_custom_chart})
  986. ```
  987. """
  988. ...
  989. def watch(
  990. models: torch.nn.Module | Sequence[torch.nn.Module],
  991. criterion: torch.F | None = None,
  992. log: Literal["gradients", "parameters", "all"] | None = "gradients",
  993. log_freq: int = 1000,
  994. idx: int | None = None,
  995. log_graph: bool = False,
  996. ) -> None:
  997. """Hook into given PyTorch model to monitor gradients and the model's computational graph.
  998. This function can track parameters, gradients, or both during training.
  999. Args:
  1000. models: A single model or a sequence of models to be monitored.
  1001. criterion: The loss function being optimized (optional).
  1002. log: Specifies whether to log "gradients", "parameters", or "all".
  1003. Set to None to disable logging. (default="gradients").
  1004. log_freq: Frequency (in batches) to log gradients and parameters. (default=1000)
  1005. idx: Index used when tracking multiple models with `wandb.watch`. (default=None)
  1006. log_graph: Whether to log the model's computational graph. (default=False)
  1007. Raises:
  1008. ValueError:
  1009. If `wandb.init()` has not been called or if any of the models are not instances
  1010. of `torch.nn.Module`.
  1011. """
  1012. ...
  1013. def unwatch(
  1014. models: torch.nn.Module | Sequence[torch.nn.Module] | None = None,
  1015. ) -> None:
  1016. """Remove pytorch model topology, gradient and parameter hooks.
  1017. Args:
  1018. models: Optional list of pytorch models that have had watch called on them.
  1019. """
  1020. ...
  1021. def restore(
  1022. name: str,
  1023. run_path: str | None = None,
  1024. replace: bool = False,
  1025. root: str | None = None,
  1026. ) -> None | TextIO:
  1027. """Download the specified file from cloud storage.
  1028. File is placed into the current directory or run directory.
  1029. By default, will only download the file if it doesn't already exist.
  1030. Args:
  1031. name: The name of the file.
  1032. run_path: Optional path to a run to pull files from, i.e. `username/project_name/run_id`
  1033. if wandb.init has not been called, this is required.
  1034. replace: Whether to download the file even if it already exists locally
  1035. root: The directory to download the file to. Defaults to the current
  1036. directory or the run directory if wandb.init was called.
  1037. Returns:
  1038. None if it can't find the file, otherwise a file object open for reading.
  1039. Raises:
  1040. CommError: If W&B can't connect to the W&B backend.
  1041. ValueError: If the file is not found or can't find run_path.
  1042. """
  1043. ...