Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle configspace as dictionary in mlp example #1057

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
## Bugfixes
- Fix bug in the incumbent selection in the case that multi-fidelity is combined with multi-objective (#1019).
- Fix callback order (#1040).
- Handle configspace as dictionary in mlp and parego example.
- Adapt sgd loss to newest scikit-learn version.

# 2.0.1

Expand Down
12 changes: 6 additions & 6 deletions examples/2_multi_fidelity/1_mlp_epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ def train(self, config: Configuration, seed: int = 0, budget: int = 25) -> float
# For deactivated parameters (by virtue of the conditions),
# the configuration stores None-values.
# This is not accepted by the MLP, so we replace them with placeholder values.
lr = config["learning_rate"] if config["learning_rate"] else "constant"
lr_init = config["learning_rate_init"] if config["learning_rate_init"] else 0.001
batch_size = config["batch_size"] if config["batch_size"] else 200
lr = config.get("learning_rate") if config.get("learning_rate") else "constant"
lr_init = config.get("learning_rate_init") if config.get("learning_rate_init") else 0.001
batch_size = config.get("batch_size") if config.get("batch_size") else 200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an FYI, you can do

lr = config.get("learning_rate", "constant")
lr_init = config.get("learning_rate_init", 0.001)
batch_size = config.get("batch_size", 200)


with warnings.catch_warnings():
warnings.filterwarnings("ignore")

classifier = MLPClassifier(
hidden_layer_sizes=[config["n_neurons"]] * config["n_layer"],
solver=config["solver"],
hidden_layer_sizes=[config.get("n_neurons")] * config.get("n_layer"),
Copy link
Contributor

@eddiebergman eddiebergman Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If n_neurons is going to be in there, you can still do config["n_neurons"]. In fact it would error if either of these were to not be present as [None] * None won't work. Likewise for the two lines below this one.

You use get(...) when there's a sensible default to use, otherwise you should do config[key] when there is no sensible default and it should error if it's not present.

solver=config.get("solver"),
batch_size=batch_size,
activation=config["activation"],
activation=config.get("activation"),
learning_rate=lr,
learning_rate_init=lr_init,
max_iter=int(np.ceil(budget)),
Expand Down
2 changes: 1 addition & 1 deletion examples/2_multi_fidelity/2_sgd_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def train(self, config: Configuration, instance: str, seed: int = 0) -> float:

# SGD classifier using given configuration
clf = SGDClassifier(
loss="log",
loss="log_loss",
penalty="elasticnet",
alpha=config["alpha"],
l1_ratio=config["l1_ratio"],
Expand Down
12 changes: 6 additions & 6 deletions examples/3_multi_objective/2_parego.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ def configspace(self) -> ConfigurationSpace:
return cs

def train(self, config: Configuration, seed: int = 0, budget: int = 10) -> dict[str, float]:
lr = config["learning_rate"] if config["learning_rate"] else "constant"
lr_init = config["learning_rate_init"] if config["learning_rate_init"] else 0.001
batch_size = config["batch_size"] if config["batch_size"] else 200
lr = config.get("learning_rate") if config.get("learning_rate") else "constant"
lr_init = config.get("learning_rate_init") if config.get("learning_rate_init") else 0.001
batch_size = config.get("batch_size") if config.get("batch_size") else 200

start_time = time.time()

with warnings.catch_warnings():
warnings.filterwarnings("ignore")

classifier = MLPClassifier(
hidden_layer_sizes=[config["n_neurons"]] * config["n_layer"],
solver=config["solver"],
hidden_layer_sizes=[config.get("n_neurons")] * config.get("n_layer"),
solver=config.get("solver"),
batch_size=batch_size,
activation=config["activation"],
activation=config.get("activation"),
learning_rate=lr,
learning_rate_init=lr_init,
max_iter=int(np.ceil(budget)),
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def train(self, config: Configuration, instance: str = "0-1", budget: float = 1,

# SGD classifier using given configuration
clf = SGDClassifier(
loss="log",
loss="log_loss",
penalty="elasticnet",
alpha=config["alpha"],
l1_ratio=config["l1_ratio"],
Expand Down
Loading