Skip to content

Commit

Permalink
Allow to load multi-objective SMAC3v2 and add example
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-segel authored and Sarah Krebs committed Jan 5, 2024
2 parents ea8554c + b806b00 commit 067f63a
Show file tree
Hide file tree
Showing 10 changed files with 11,557 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Version 1.1.4

## Enhancements
- Fix lower bounds of dependency versions.
- Allow to load multi-objective SMAC3v2 and add example (#69)

## Bug-Fixes
- Don't convert BOHB runs with status 'running' (consistent with SMAC).

Expand Down
19 changes: 13 additions & 6 deletions deepcave/runs/converters/smac3v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ def from_path(cls, path):
configspace = cs_json.read(f.read())

# Read objectives
# We have to define it ourselves, because we don't know the type of the objective
with (path / "scenario.json").open() as json_file:
all_data = json.load(json_file)
objectives = all_data["objectives"]

obj_list = list()
if not isinstance(objectives, list):
objectives = [objectives]
# Only lock lower
objective1 = Objective("Cost", lower=0)
objective2 = Objective("Time", lower=0)
for obj in objectives:
obj_list.append(Objective(obj, lower=0))
obj_list.append(Objective("Time", lower=0))

# Read meta
with (path / "scenario.json").open() as json_file:
Expand All @@ -54,7 +61,7 @@ def from_path(cls, path):

# Let's create a new run object
run = SMAC3v2Run(
name=path.stem, configspace=configspace, objectives=[objective1, objective2], meta=meta
name=path.stem, configspace=configspace, objectives=obj_list, meta=meta
)

# We have to set the path manually
Expand Down Expand Up @@ -118,7 +125,7 @@ def from_path(cls, path):

if status != Status.SUCCESS:
# We don't want cost included which are failed
cost = None
cost = [None] * len(cost) if isinstance(cost, list) else None
time = None
else:
time = endtime - starttime
Expand All @@ -134,7 +141,7 @@ def from_path(cls, path):
origin = config_origins[config_id]

run.add(
costs=[cost, time],
costs=cost + [time] if isinstance(cost, list) else [cost, time],
config=config,
budget=budget,
start_time=starttime,
Expand Down
6 changes: 2 additions & 4 deletions examples/record/mnist_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ def __init__(self, activation="relu", learning_rate=1e-4, dropout_rate=0.1, batc
]
)

self.accuracy = Accuracy()
self.accuracy = Accuracy(task="multiclass", num_classes=self.num_classes)

def prepare_data(self):
# download
MNIST(self.data_dir, train=True, download=True)
MNIST(self.data_dir, train=False, download=True)

def setup(self, stage=None):

# Assign train/val datasets for use in dataloaders
if stage == "fit" or stage is None:
mnist_full = MNIST(self.data_dir, train=True, transform=self.transform)
Expand Down Expand Up @@ -257,10 +256,9 @@ def get_configspace(seed):

# The model weights are trained
trainer = pl.Trainer(
accelerator="gpu",
accelerator="cpu",
devices=1,
num_sanity_val_steps=0, # No validation sanity
auto_scale_batch_size="power",
deterministic=True,
min_epochs=epochs,
max_epochs=epochs,
Expand Down
17 changes: 17 additions & 0 deletions logs/SMAC3v2/multi-objective/run_1/configspace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"hyperparameters": [
{
"name": "x",
"type": "uniform_float",
"log": false,
"lower": -2.0,
"upper": 2.0,
"default": 0.0,
"q": null
}
],
"conditions": [],
"forbiddens": [],
"python_module_version": "0.6.1",
"json_format_version": 0.4
}
Loading

0 comments on commit 067f63a

Please sign in to comment.