Skip to content

Commit

Permalink
fix(docs): Optimizer inline examples (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiebergman authored Nov 25, 2023
1 parent 1b41aa2 commit 299ab3d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/amltk/optimization/optimizers/neps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/amltk/optimization/optimizers/optuna.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/amltk/optimization/optimizers/smac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down
19 changes: 18 additions & 1 deletion src/amltk/pipeline/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 299ab3d

Please sign in to comment.