Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 0 additions & 2 deletions examples/optuna/gp_sampler_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
- Can handle constraints and noisy observations
"""

import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score

from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.optuna import GPOptimizer
Expand Down
2 changes: 0 additions & 2 deletions examples/optuna/grid_sampler_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
- Interpretable and deterministic results
"""

import numpy as np
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score

from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.optuna import GridOptimizer
Expand Down
2 changes: 1 addition & 1 deletion examples/optuna/nsga_ii_sampler_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, X, y):
self.X = X
self.y = y

def __call__(self, **params):
def __call__(self, params):
# Create model with parameters
model = RandomForestClassifier(random_state=42, **params)

Expand Down
2 changes: 1 addition & 1 deletion examples/optuna/nsga_iii_sampler_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, X, y):
self.X = X
self.y = y

def __call__(self, **params):
def __call__(self, params):
# Create model with parameters
model = DecisionTreeClassifier(random_state=42, **params)

Expand Down
2 changes: 0 additions & 2 deletions examples/optuna/qmc_sampler_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
- Baseline optimization comparisons
"""

import numpy as np
from sklearn.datasets import load_wine
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.optuna import QMCOptimizer
Expand Down
2 changes: 0 additions & 2 deletions examples/optuna/random_sampler_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
- Good when objective function is noisy
"""

import numpy as np
from sklearn.datasets import load_digits
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score

from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.optuna import RandomOptimizer
Expand Down
2 changes: 0 additions & 2 deletions examples/optuna/tpe_sampler_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
- Default choice for most hyperparameter optimization tasks
"""

import numpy as np
from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score

from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.optuna import TPEOptimizer
Expand Down
6 changes: 3 additions & 3 deletions src/hyperactive/base/_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class BaseExperiment(BaseObject):
def __init__(self):
super().__init__()

def __call__(self, **kwargs):
"""Score parameters, with kwargs call. Same as score call."""
score, _ = self.score(kwargs)
def __call__(self, params):
"""Score parameters. Same as score call, returns only only a first element."""
score, _ = self.score(params)
return score

@property
Expand Down
4 changes: 2 additions & 2 deletions src/hyperactive/experiment/bench/_ackley.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class Ackley(BaseExperiment):
>>> from hyperactive.experiment.bench import Ackley
>>> ackley = Ackley(a=20)
>>> params = {"x0": 1, "x1": 2}
>>> score, add_info = ackley.score(params)
>>> score, metadata = ackley.score(params)

Quick call without metadata return or dictionary:
>>> score = ackley(x0=1, x1=2)
>>> score = ackley({"x0": 1, "x1": 2})
""" # noqa: E501

_tags = {
Expand Down
4 changes: 2 additions & 2 deletions src/hyperactive/experiment/bench/_parabola.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class Parabola(BaseExperiment):
>>> from hyperactive.experiment.bench import Parabola
>>> parabola = Parabola(a=1.0, b=0.0, c=0.0)
>>> params = {"x": 1, "y": 2}
>>> score, add_info = parabola.score(params)
>>> score, metadata = parabola.score(params)

Quick call without metadata return or dictionary:
>>> score = parabola(x=1, y=2)
>>> score = parabola({"x": 1, "y": 2})
"""

_tags = {
Expand Down
6 changes: 3 additions & 3 deletions src/hyperactive/experiment/bench/_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class Sphere(BaseExperiment):
>>> from hyperactive.experiment.bench import Sphere
>>> sphere = Sphere(const=0, n_dim=3)
>>> params = {"x0": 1, "x1": 2, "x2": 3}
>>> score, add_info = sphere.score(params)
>>> score, metadata = sphere.score(params)

Quick call without metadata return or dictionary:
>>> score = sphere(x0=1, x1=2, x2=3)
>>> score = sphere({"x0": 1, "x1": 2, "x2": 3})

Different number of dimensions changes the parameter names:
>>> sphere4D = Sphere(const=0, n_dim=4)
>>> score4D = sphere4D(x0=1, x1=2, x2=3, x3=4)
>>> score4D = sphere4D({"x0": 1, "x1": 2, "x2": 3, "x3": 4})
"""

_tags = {
Expand Down
10 changes: 5 additions & 5 deletions src/hyperactive/experiment/integrations/sklearn_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SklearnCvExperiment(BaseExperiment):
... y=y,
... )
>>> params = {"C": 1.0, "kernel": "linear"}
>>> score, add_info = sklearn_exp.score(params)
>>> score, metadata = sklearn_exp.score(params)

For default choices of ``scoring`` and ``cv``:
>>> sklearn_exp = SklearnCvExperiment(
Expand All @@ -71,10 +71,10 @@ class SklearnCvExperiment(BaseExperiment):
... y=y,
... )
>>> params = {"C": 1.0, "kernel": "linear"}
>>> score, add_info = sklearn_exp.score(params)
>>> score, metadata = sklearn_exp.score(params)

Quick call without metadata return or dictionary:
>>> score = sklearn_exp(C=1.0, kernel="linear")
>>> score = sklearn_exp({"C": 1.0, "kernel": "linear"})
"""

def __init__(self, estimator, X, y, scoring=None, cv=None):
Expand Down Expand Up @@ -158,13 +158,13 @@ def _evaluate(self, params):
cv=self._cv,
)

add_info_d = {
metadata = {
"score_time": cv_results["score_time"],
"fit_time": cv_results["fit_time"],
"n_test_samples": _num_samples(self.X),
}

return cv_results["test_score"].mean(), add_info_d
return cv_results["test_score"].mean(), metadata

@classmethod
def get_test_params(cls, parameter_set="default"):
Expand Down
6 changes: 3 additions & 3 deletions src/hyperactive/experiment/integrations/sktime_forecasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class SktimeForecastingExperiment(BaseExperiment):
... y=y,
... )
>>> params = {"strategy": "mean"}
>>> score, add_info = sktime_exp.score(params)
>>> score, metadata = sktime_exp.score(params)

For default choices of ``scoring``:
>>> sktime_exp = SktimeForecastingExperiment(
Expand All @@ -130,10 +130,10 @@ class SktimeForecastingExperiment(BaseExperiment):
... y=y,
... )
>>> params = {"strategy": "mean"}
>>> score, add_info = sktime_exp.score(params)
>>> score, metadata = sktime_exp.score(params)

Quick call without metadata return or dictionary:
>>> score = sktime_exp(strategy="mean")
>>> score = sktime_exp({"strategy": "mean"})
"""

_tags = {
Expand Down
4 changes: 2 additions & 2 deletions src/hyperactive/opt/_adapters/_base_optuna_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _objective(self, trial):
The objective value
"""
params = self._suggest_params(trial, self.param_space)
score = self.experiment(**params)
score = self.experiment(params)

# Handle early stopping based on max_score
if self.max_score is not None and score >= self.max_score:
Expand All @@ -133,7 +133,7 @@ def _setup_initial_positions(self, study):
# For warm start, we manually add trials to the study history
# instead of using suggest methods to avoid distribution conflicts
for point in warm_start_points:
self.experiment(**point)
self.experiment(point)
study.enqueue_trial(point)

def _solve(self, experiment, param_space, n_trials, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion src/hyperactive/tests/test_all_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def test_score_function(self, object_class):
msg = f"Score and eval calls do not match: |{e_score}| != |{score}|"
assert abs(e_score) == abs(score), msg

call_sc = inst(**obj)
call_sc = inst(obj)
assert isinstance(call_sc, float), f"Score is not a float: {call_sc}"
if det_tag == "deterministic":
msg = f"Score does not match: {score} != {call_sc}"
Expand Down
Loading