-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carolin Benjamins
committed
Jun 12, 2023
1 parent
ba5a5c1
commit 48a3366
Showing
3 changed files
with
69 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from smac.callback.callback import Callback | ||
from smac.callback.metadata_callback import MetadataCallback | ||
from smac.callback.wandb_logging import WandBCallback | ||
|
||
__all__ = [ | ||
"Callback", | ||
"MetadataCallback", | ||
"WandBCallback", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from dataclasses import asdict | ||
|
||
import smac | ||
from smac import Callback | ||
from smac.runhistory import TrialInfo, TrialValue | ||
|
||
|
||
class WandBCallback(Callback): | ||
def __init__( | ||
self, | ||
project: str, | ||
entity: str, | ||
id: str | None = None, | ||
outdir: str | None = None, | ||
mode: str | None = None, | ||
resume: str = "allow", | ||
job_type: str | None = None, | ||
group: str | None = None, | ||
config: dict | str | None = None, | ||
save_code: bool = True, | ||
**kwargs, | ||
) -> None: | ||
import wandb | ||
|
||
self.run = wandb.init( | ||
id=id, | ||
resume=resume, | ||
mode=mode, | ||
project=project, | ||
job_type=job_type, | ||
entity=entity, | ||
group=group, | ||
dir=outdir, | ||
config=config, | ||
save_code=save_code, | ||
**kwargs, | ||
) | ||
super().__init__() | ||
|
||
def on_tell_end(self, smbo: smac.main.smbo.SMBO, info: TrialInfo, value: TrialValue) -> bool | None: | ||
info_dict = asdict(info) | ||
info_dict["config"] = info_dict["config"].get_dictionary() | ||
value_dict = asdict(value) | ||
log_dict = info_dict | value_dict | ||
log_dict["step"] = smbo.runhistory.finished | ||
self.run.log(data=log_dict) | ||
return super().on_tell_end(smbo, info, value) | ||
|
||
def on_end(self, smbo: smac.main.smbo.SMBO) -> None: | ||
intensifier_data = smbo.intensifier.get_data() | ||
trajectory = intensifier_data["trajectory"] | ||
import pandas as pd | ||
|
||
df = pd.DataFrame(data=trajectory) | ||
print(df) | ||
# trajectory = Table(dataframe=df, allow_mixed_types=True) | ||
df["costs"] = df["costs"].apply(lambda x: x[0]) # TODO properly log multi costs | ||
for index, row in df.iterrows(): | ||
print(dict(row)) | ||
self.run.log(dict(row)) | ||
self.run.finish() | ||
return super().on_end(smbo) |