diff --git a/src/amltk/optimization/optimizers/neps.py b/src/amltk/optimization/optimizers/neps.py index 4f2a4172..10265993 100644 --- a/src/amltk/optimization/optimizers/neps.py +++ b/src/amltk/optimization/optimizers/neps.py @@ -40,7 +40,7 @@ The below example shows how you can use neps to optimize an sklearn pipeline. -```python exec="False" source="material-block" result="python" +```python from __future__ import annotations import logging @@ -52,7 +52,7 @@ from amltk.optimization.optimizers.neps import NEPSOptimizer from amltk.scheduling import Scheduler -from amltk.optimization import History, Trial +from amltk.optimization import History, Trial, Metric from amltk.pipeline import Component logging.basicConfig(level=logging.INFO) @@ -73,10 +73,10 @@ def target_function(trial: Trial, pipeline: Pipeline) -> Trial.Report: return trial.fail() from amltk._doc import make_picklable; make_picklable(target_function) # markdown-exec: hide - pipeline = Component(RandomForestClassifier, space={"n_estimators": (10, 100)}) -space = pipeline.search_space(parser=NEPSOptimizer.preferred_parser()) -optimizer = NEPSOptimizer.create(space=space) + +metric = Metric("accuracy", minimize=False, bounds=(0, 1)) +optimizer = NEPSOptimizer.create(space=pipeline, metrics=metric, bucket="neps-doc-example") N_WORKERS = 2 scheduler = Scheduler.with_processes(N_WORKERS) @@ -103,6 +103,7 @@ def add_to_history(_, report: Trial.Report): scheduler.run(timeout=3, wait=False) print(history.df()) +optimizer.bucket.rmdir() # markdown-exec: hide ``` !!! todo "Deep Learning" @@ -112,6 +113,7 @@ def add_to_history(_, report: Trial.Report): !!! todo "Graph Search Spaces" Write an example demonstrating NEPS with its graph search spaces + """ # noqa: E501 from __future__ import annotations diff --git a/src/amltk/optimization/optimizers/optuna.py b/src/amltk/optimization/optimizers/optuna.py index 54a50f98..81015f0a 100644 --- a/src/amltk/optimization/optimizers/optuna.py +++ b/src/amltk/optimization/optimizers/optuna.py @@ -60,10 +60,10 @@ def target_function(trial: Trial, pipeline: Pipeline) -> Trial.Report: return trial.fail() from amltk._doc import make_picklable; make_picklable(target_function) # markdown-exec: hide +pipeline = Component(RandomForestClassifier, space={"n_estimators": (10, 100)}) accuracy_metric = Metric("accuracy", minimize=False, bounds=(0, 1)) -pipeline = Component(RandomForestClassifier, space={"n_estimators": (10, 100)}) -optimizer = OptunaOptimizer.create(space=pipeline, metrics=accuracy_metric) +optimizer = OptunaOptimizer.create(space=pipeline, metrics=accuracy_metric, bucket="optuna-doc-example") N_WORKERS = 2 scheduler = Scheduler.with_processes(N_WORKERS) diff --git a/src/amltk/optimization/optimizers/smac.py b/src/amltk/optimization/optimizers/smac.py index 64ca6d90..e28b8043 100644 --- a/src/amltk/optimization/optimizers/smac.py +++ b/src/amltk/optimization/optimizers/smac.py @@ -35,7 +35,7 @@ from amltk.optimization.optimizers.smac import SMACOptimizer from amltk.scheduling import Scheduler -from amltk.optimization import History, Trial +from amltk.optimization import History, Trial, Metric from amltk.pipeline import Component, Node logging.basicConfig(level=logging.INFO) @@ -50,16 +50,15 @@ def target_function(trial: Trial, pipeline: Node) -> Trial.Report: clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) - loss = 1 - accuracy - return trial.success(loss=loss, accuracy=accuracy) + return trial.success(accuracy=accuracy) return trial.fail() from amltk._doc import make_picklable; make_picklable(target_function) # markdown-exec: hide +pipeline = Component(RandomForestClassifier, space={"n_estimators": (10, 100), "max_samples": (0.1, 0.9)}) -pipeline = Component(RandomForestClassifier, space={"n_estimators": (10, 100)}) -space = pipeline.search_space(parser=SMACOptimizer.preferred_parser()) -optimizer = SMACOptimizer.create(space=space) +metric = Metric("accuracy", minimize=False, bounds=(0, 1)) +optimizer = SMACOptimizer.create(space=pipeline, metrics=metric, bucket="smac-doc-example") N_WORKERS = 2 scheduler = Scheduler.with_processes(N_WORKERS) @@ -86,6 +85,8 @@ def add_to_history(_, report: Trial.Report): scheduler.run(timeout=3, wait=False) print(history.df()) +optimizer.bucket.rmdir() # markdown-exec: hide +``` """ # noqa: E501 from __future__ import annotations diff --git a/src/amltk/pipeline/node.py b/src/amltk/pipeline/node.py index e6a72c68..b77d91d5 100644 --- a/src/amltk/pipeline/node.py +++ b/src/amltk/pipeline/node.py @@ -541,7 +541,24 @@ def search_space( *parser_args: P.args, **parser_kwargs: P.kwargs, ) -> ParserOutput | ConfigurationSpace | OptunaSearchSpace: - """Get the search space for this node.""" + """Get the search space for this node. + + Args: + parser: The parser to use. This can be a function that takes in + the node and returns the search space or a string that is one of: + + * `#!python "configspace"`: Build a + [`ConfigSpace.ConfigurationSpace`](https://automl.github.io/ConfigSpace/master/) + out of this node. + * `#!python "optuna"`: Build a dict of hyperparameters that Optuna can + use in its [ask and tell methods](https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/009_ask_and_tell.html#define-and-run) + + parser_args: The positional arguments to pass to the parser + parser_kwargs: The keyword arguments to pass to the parser + + Returns: + The search space + """ match parser: case "configspace": from amltk.pipeline.parsers.configspace import parser as cs_parser