Neps
The NEPSOptimizer
,
is a wrapper around the NePs
optimizer.
Requirements
This requires smac
which can be installed with:
NePs is still in development
NePs is still in development and is not yet stable. There are likely going to be issues. Please report any issues to NePs or in AMLTK.
This uses ConfigSpace
as its search_space()
to
optimize.
Users should report results using
trial.success(loss=...)
where loss=
is a scaler value to minimize. Optionally,
you can also return a cost=
which is used for more budget aware algorithms.
Again, please see NeP's documentation for more.
Conditionals in ConfigSpace
NePs does not support conditionals in its search space. This is account
for when using the
preferred_parser()
.
during search space creation. In this case, it will simply remove all conditionals
from the search space, which may not be ideal for the given problem at hand.
Visit their documentation for what you can pass to
NEPSOptimizer.create()
.
The below example shows how you can use neps to optimize an sklearn pipeline.
from __future__ import annotations
import logging
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from amltk.optimization.optimizers.neps import NEPSOptimizer
from amltk.scheduling import Scheduler
from amltk.optimization import History, Trial, Metric
from amltk.pipeline import Component
logging.basicConfig(level=logging.INFO)
def target_function(trial: Trial, pipeline: Pipeline) -> Trial.Report:
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)
clf = pipeline.configure(trial.config).build("sklearn")
with trial.begin():
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.fail()
from amltk._doc import make_picklable; make_picklable(target_function) # markdown-exec: hide
pipeline = Component(RandomForestClassifier, space={"n_estimators": (10, 100)})
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)
task = scheduler.task(target_function)
history = History()
@scheduler.on_start(repeat=N_WORKERS)
def on_start():
trial = optimizer.ask()
task.submit(trial, pipeline)
@task.on_result
def tell_and_launch_trial(_, report: Trial.Report):
if scheduler.running():
optimizer.tell(report)
trial = optimizer.ask()
task.submit(trial, pipeline)
@task.on_result
def add_to_history(_, report: Trial.Report):
history.add(report)
scheduler.run(timeout=3, wait=False)
print(history.df())
optimizer.bucket.rmdir() # markdown-exec: hide
Deep Learning
Write an example demonstrating NEPS with continuations
Graph Search Spaces
Write an example demonstrating NEPS with its graph search spaces
class NEPSPreferredParser
#
Bases: Protocol
The preferred parser call signature for NEPSOptimizer.
def __call__(node, *, seed=None, flat=False, delim=':')
#
See configspace_parser
.
class NEPSTrialInfo
dataclass
#
The info for a trial.
class NEPSOptimizer(*, space, loss_metric, cost_metric=None, optimizer, working_dir, seed=None, bucket=None)
#
Bases: Optimizer[NEPSTrialInfo]
An optimizer that uses SMAC to optimize a config space.
PARAMETER | DESCRIPTION |
---|---|
space |
The space to use.
TYPE:
|
loss_metric |
The metric to optimize.
TYPE:
|
cost_metric |
The cost metric to use. Only certain NePs optimizers support
TYPE:
|
optimizer |
The optimizer to use.
TYPE:
|
seed |
The seed to use for the trials (and not optimizers).
TYPE:
|
working_dir |
The directory to use for the trials.
TYPE:
|
bucket |
The bucket to give to trials generated from this optimizer.
TYPE:
|
Source code in src/amltk/optimization/optimizers/neps.py
def create(*, space, metrics, cost_metric=None, bucket=None, searcher='default', working_dir='neps', overwrite=True, seed=None, max_cost_total=None, searcher_kwargs=None)
classmethod
#
Create a new NEPS optimizer.
PARAMETER | DESCRIPTION |
---|---|
space |
The space to use.
TYPE:
|
metrics |
The metrics to optimize. Warning NePs does not support multiple metrics. Please only pass a single metric.
TYPE:
|
cost_metric |
The cost metric to use. Only certain NePs optimizers support this.
TYPE:
|
seed |
The seed to use for the trials. Warning NePS optimizers do not support an explicit seeding. If you'd
like to seed their optimizers, they use the global
TYPE:
|
bucket |
The bucket to give to trials generated by this optimizer.
TYPE:
|
searcher |
The searcher to use.
TYPE:
|
working_dir |
The directory to use for the optimization. |
overwrite |
Whether to overwrite the working directory if it exists.
TYPE:
|
max_cost_total |
The maximum cost to use for the optimization. Warning This only effects the optimization if the searcher utilizes the budget for it's actual suggestion of the next config. If the searcher does not use the budget. This parameter has no effect. The user is still expected to stop
TYPE:
|
searcher_kwargs |
Additional kwargs to pass to the searcher. |
Source code in src/amltk/optimization/optimizers/neps.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
def ask()
#
Ask the optimizer for a new config.
RETURNS | DESCRIPTION |
---|---|
Trial[NEPSTrialInfo]
|
The trial info for the new config. |
Source code in src/amltk/optimization/optimizers/neps.py
def tell(report)
#
Tell the optimizer the result of the sampled config.
PARAMETER | DESCRIPTION |
---|---|
report |
The report of the trial.
TYPE:
|
Source code in src/amltk/optimization/optimizers/neps.py
def preferred_parser()
classmethod
#
The preferred parser for this optimizer.