__init__.pyi 46 KB

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