Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
72 changes: 31 additions & 41 deletions autosklearn/automl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,65 +1554,55 @@ def _load_models(self):
self.ensemble_ = None

# If no ensemble is loaded, try to get the best performing model
if not self.ensemble_:
self.ensemble_ = self._load_best_individual_model()

if self.ensemble_:
identifiers = self.ensemble_.get_selected_model_identifiers()
self.models_ = self._backend.load_models_by_identifiers(identifiers)

if self._resampling_strategy in ("cv", "cv-iterative-fit"):
self.cv_models_ = self._backend.load_cv_models_by_identifiers(
identifiers
if (
not self.ensemble_
and not (
self._disable_evaluator_output is True
or (
isinstance(self._disable_evaluator_output, list)
and "model" in self._disable_evaluator_output
)
else:
self.cv_models_ = None

if len(self.models_) == 0 and self._resampling_strategy not in [
)
and self._resampling_strategy
not in (
"partial-cv",
"partial-cv-iterative-fit",
]:
raise ValueError("No models fitted!")

if (
self._resampling_strategy in ["cv", "cv-iterative-fit"]
and len(self.cv_models_) == 0
):
raise ValueError("No models fitted!")

elif self._disable_evaluator_output is False or (
isinstance(self._disable_evaluator_output, list)
and "model" not in self._disable_evaluator_output
)
):
model_names = self._backend.list_all_models(self._seed)

if len(model_names) == 0 and self._resampling_strategy not in [
"partial-cv",
"partial-cv-iterative-fit",
]:
raise ValueError("No models fitted!")
self.ensemble_ = self._load_best_individual_model()

self.ensemble_ = None
self.models_ = []
self.cv_models_ = None
identifiers = self.ensemble_.get_selected_model_identifiers()
self.models_ = self._backend.load_models_by_identifiers(identifiers)

if self._resampling_strategy in ("cv", "cv-iterative-fit"):
self.cv_models_ = self._backend.load_cv_models_by_identifiers(identifiers)
else:
self.ensemble_ = None
self.models_ = []
self.cv_models_ = None

if len(self.models_) == 0 and self._resampling_strategy not in [
"partial-cv",
"partial-cv-iterative-fit",
]:
raise ValueError("No models fitted!")

elif (
self._resampling_strategy in ["cv", "cv-iterative-fit"]
and len(self.cv_models_) == 0
):
raise ValueError("No models fitted!")

def _load_best_individual_model(self):
"""
In case of failure during ensemble building,
this method returns the single best model found
by AutoML.
This is a robust mechanism to be able to predict,
even though no ensemble was found by ensemble builder.
It is also used to load the single best model in case
the user does not want to build an ensemble.
"""
# We also require that the model is fit and a task is defined
# The ensemble size must also be greater than 1, else it means
# that the user intentionally does not want an ensemble
if not self._task or self._ensemble_size < 1:
if not self._task:
return None

# SingleBest contains the best model found by AutoML
Expand Down
6 changes: 3 additions & 3 deletions autosklearn/estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def __init__(
ensemble_size : int, optional (default=50)
Number of models added to the ensemble built by *Ensemble
selection from libraries of models*. Models are drawn with
replacement. If set to ``0`` no ensemble is fit.
replacement. If set to ``0`` no ensemble is fit and the single
best model is loaded.

ensemble_nbest : int, optional (default=50)
Only consider the ``ensemble_nbest`` models when building an
Expand Down Expand Up @@ -526,8 +527,7 @@ def fit_ensemble(
All parameters are ``None`` by default. If no other value is given,
the default values which were set in a call to ``fit()`` are used.

Calling this function is only necessary if ``ensemble_size==0``, for
example when executing *auto-sklearn* in parallel.
Calling this function is only necessary if ``ensemble_size==0``.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this true?


Parameters
----------
Expand Down
9 changes: 5 additions & 4 deletions test/test_automl/test_post_fit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from autosklearn.automl import AutoML
from autosklearn.ensembles.singlebest_ensemble import SingleBest

from pytest_cases import parametrize_with_cases

Expand Down Expand Up @@ -59,10 +60,10 @@ def test_no_ensemble(automl: AutoML) -> None:

Expects
-------
* The ensemble should remain None
* The models_ should be empty
* Auto-sklearn loads a single best model
* The models_ should be of size 1
* The cv_models_ should remain None
"""
assert automl.ensemble_ is None
assert automl.models_ == []
assert isinstance(automl.ensemble_, SingleBest)
assert len(automl.models_) == 1
assert automl.cv_models_ is None