diff --git a/examples/6_advanced_features/1_wandb_logging.py b/examples/6_advanced_features/1_wandb_logging.py index 9ce045851..994d8f5f0 100644 --- a/examples/6_advanced_features/1_wandb_logging.py +++ b/examples/6_advanced_features/1_wandb_logging.py @@ -2,7 +2,16 @@ Use Weights and Biases for logging ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -This example shows how to use Weights and Biases for logging. +This example shows how to use Weights and Biases (WandB) for logging. + +To use WandB, you need to install the package via pip: + +.. code-block:: bash + + pip install wandb + +Then you can use the WandBCallback to log the results of the optimization as well as intermediate information to WandB. +This is done by creating a WandBCallback object and passing it to the used Facade. """ from __future__ import annotations diff --git a/smac/callback/wandb_logging.py b/smac/callback/wandb_logging.py index ecb65c09c..54e5c35a8 100644 --- a/smac/callback/wandb_logging.py +++ b/smac/callback/wandb_logging.py @@ -1,11 +1,46 @@ +from typing import Any + from dataclasses import asdict import smac -from smac import Callback +from smac.callback import Callback from smac.runhistory import TrialInfo, TrialValue class WandBCallback(Callback): + """ + + Callback to log the results of the optimization as well as intermediate information to WandB. + + Logs TrialInfo, TrialValue and the number of successfully executed trials (as step) to WandB `on_tell_end`. + Upon the end of the run, logs the trajectory of the intensifier to WandB. + + Parameters + ---------- + project : str + The project name of the WandB project. + entity : str + The entity name of the WandB project. + id : str, optional + The id of the run. + outdir : str, optional + The output directory of the WandB run. + mode : str, optional + The mode of the WandB run. + resume : str, optional + The resume mode of the WandB run. + job_type : str, optional + The job type of the WandB run. + group : str, optional + The group of the WandB run. + config : dict or str, optional + The configuration of the WandB run. + save_code : bool, optional + Whether to save the code of the WandB run. + **kwargs : dict + Additional arguments to pass to the WandB run. + """ + def __init__( self, project: str, @@ -18,7 +53,7 @@ def __init__( group: str | None = None, config: dict | str | None = None, save_code: bool = True, - **kwargs, + **kwargs: dict[str, Any], ) -> None: import wandb @@ -37,7 +72,7 @@ def __init__( ) super().__init__() - def on_tell_end(self, smbo: smac.main.smbo.SMBO, info: TrialInfo, value: TrialValue) -> bool | None: + def on_tell_end(self, smbo: smac.main.smbo.SMBO, info: TrialInfo, value: TrialValue) -> bool | None: # noqa: D102 info_dict = asdict(info) info_dict["config"] = info_dict["config"].get_dictionary() value_dict = asdict(value) @@ -46,7 +81,7 @@ def on_tell_end(self, smbo: smac.main.smbo.SMBO, info: TrialInfo, value: TrialVa self.run.log(data=log_dict) return super().on_tell_end(smbo, info, value) - def on_end(self, smbo: smac.main.smbo.SMBO) -> None: + def on_end(self, smbo: smac.main.smbo.SMBO) -> None: # noqa: D102 intensifier_data = smbo.intensifier.get_data() trajectory = intensifier_data["trajectory"] import pandas as pd