Skip to content

Commit 468bc62

Browse files
Fix issue with "list index out of range" when max_iter=1 (#1419)
1 parent 437c239 commit 468bc62

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

flaml/automl/automl.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,21 @@ def _search(self):
25292529
self._selected = state = self._search_states[estimator]
25302530
state.best_config_sample_size = self._state.data_size[0]
25312531
state.best_config = state.init_config[0] if state.init_config else {}
2532+
self._track_iter = 0
2533+
self._config_history[self._track_iter] = (estimator, state.best_config, self._state.time_from_start)
2534+
self._best_iteration = self._track_iter
2535+
state.val_loss = getattr(state, "val_loss", float("inf"))
2536+
state.best_loss = getattr(state, "best_loss", float("inf"))
2537+
state.config = getattr(state, "config", state.best_config.copy())
2538+
state.metric_for_logging = getattr(state, "metric_for_logging", None)
2539+
state.sample_size = getattr(state, "sample_size", self._state.data_size[0])
2540+
state.learner_class = getattr(state, "learner_class", self._state.learner_classes.get(estimator))
2541+
if hasattr(self, "mlflow_integration") and self.mlflow_integration:
2542+
self.mlflow_integration.record_state(
2543+
automl=self,
2544+
search_state=state,
2545+
estimator=estimator,
2546+
)
25322547
elif self._use_ray is False and self._use_spark is False:
25332548
self._search_sequential()
25342549
else:

test/automl/test_max_iter_1.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import mlflow
2+
import numpy as np
3+
import pandas as pd
4+
5+
from flaml import AutoML
6+
7+
8+
def test_max_iter_1():
9+
date_rng = pd.date_range(start="2024-01-01", periods=100, freq="H")
10+
X = pd.DataFrame({"ds": date_rng})
11+
y_train_24h = np.random.rand(len(X)) * 100
12+
13+
# AutoML
14+
settings = {
15+
"max_iter": 1,
16+
"estimator_list": ["xgboost", "lgbm"],
17+
"starting_points": {"xgboost": {}, "lgbm": {}},
18+
"task": "ts_forecast",
19+
"log_file_name": "test_max_iter_1.log",
20+
"seed": 41,
21+
"mlflow_exp_name": "TestExp-max_iter-1",
22+
"use_spark": False,
23+
"n_concurrent_trials": 1,
24+
"verbose": 1,
25+
"featurization": "off",
26+
"metric": "rmse",
27+
"mlflow_logging": True,
28+
}
29+
30+
automl = AutoML(**settings)
31+
32+
with mlflow.start_run(run_name="AutoMLModel-XGBoost-and-LGBM-max_iter_1"):
33+
automl.fit(
34+
X_train=X,
35+
y_train=y_train_24h,
36+
period=24,
37+
X_val=X,
38+
y_val=y_train_24h,
39+
split_ratio=0,
40+
force_cancel=False,
41+
)
42+
43+
assert automl.model is not None, "AutoML failed to return a model"
44+
assert automl.best_run_id is not None, "Best run ID should not be None with mlflow logging"
45+
46+
print("Best model:", automl.model)
47+
print("Best run ID:", automl.best_run_id)
48+
49+
50+
if __name__ == "__main__":
51+
test_max_iter_1()

0 commit comments

Comments
 (0)