Skip to content

A programming framework for agentic AI

update

skzhang1e5a422c41e62
diff --git a/flaml/automl.py b/flaml/automl.py
index c14772c06..d6c335dd2 100644
--- a/flaml/automl.py
+++ b/flaml/automl.py
@@ -364,6 +364,7 @@ class AutoMLState:
state.best_loss,
state.n_jobs,
state.learner_classes.get(estimator),
+ state.cv_strategy,
state.log_training_metric,
this_estimator_kwargs,
)
@@ -2070,6 +2071,7 @@ class AutoML(BaseEstimator):
use_ray=None,
metric_constraints=None,
custom_hp=None,
+ cv_strategy=None,
fit_kwargs_by_estimator=None,
**fit_kwargs,
):
@@ -2287,6 +2289,17 @@ class AutoML(BaseEstimator):
}
```
+ cv_strategy: function, the strategy of conducting cross-validation. Default to average the optimization metric across folds.
+ We give an example here:
+
+ ```python
+ def cv_strategy(val_loss_folds):
+ return sum(val_loss_folds)/len(val_loss_folds)
+ ```
+
+ where val_loss_folds is the list that stores the metrics values of all folds. In this example, we return the average of the optimization
+ metric across all folds (default strategy).
+
fit_kwargs_by_estimator: dict, default=None | The user specified keywords arguments, grouped by estimator name.
For TransformersEstimator, available fit_kwargs can be found from
[TrainingArgumentsForAuto](nlp/huggingface/training_args).
@@ -2307,7 +2320,7 @@ class AutoML(BaseEstimator):
gpu_per_trial: float, default = 0 | A float of the number of gpus per trial,
only used by TransformersEstimator and XGBoostSklearnEstimator.
"""
-
+
self._state._start_time_flag = self._start_time_flag = time.time()
task = task or self._settings.get("task")
self._estimator_type = "classifier" if task in CLASSIFICATION else "regressor"
@@ -2447,6 +2460,7 @@ class AutoML(BaseEstimator):
eval_method = self._decide_eval_method(eval_method, time_budget)
self._state.eval_method = eval_method
logger.info("Evaluation method: {}".format(eval_method))
+ self._state.cv_strategy = cv_strategy
self._retrain_in_budget = retrain_full == "budget" and (
eval_method == "holdout" and self._state.X_val is None
diff --git a/flaml/ml.py b/flaml/ml.py
index cc7a0e4b8..02ae93954 100644
--- a/flaml/ml.py
+++ b/flaml/ml.py
@@ -438,11 +438,14 @@ def evaluate_model_CV(
task,
eval_metric,
best_val_loss,
+ cv_strategy,
log_training_metric=False,
fit_kwargs={},
):
+ if cv_strategy is None:
+ cv_strategy = lambda val_loss_folds: sum(val_loss_folds)/len(val_loss_folds)
start_time = time.time()
- total_val_loss = 0
+ val_loss_folds = []
total_metric = None
metric = None
train_time = pred_time = 0
@@ -515,7 +518,7 @@ def evaluate_model_CV(
fit_kwargs["sample_weight"] = weight
valid_fold_num += 1
total_fold_num += 1
- total_val_loss += val_loss_i
+ val_loss_folds.append(val_loss_i)
if log_training_metric or not isinstance(eval_metric, str):
if isinstance(total_metric, dict):
total_metric = {k: total_metric[k] + v for k, v in metric_i.items()}
@@ -526,10 +529,11 @@ def evaluate_model_CV(
train_time += train_time_i
pred_time += pred_time_i
if valid_fold_num == n:
- val_loss_list.append(total_val_loss / valid_fold_num)
- total_val_loss = valid_fold_num = 0
+ val_loss_list.append(cv_strategy(val_loss_folds))
+ val_loss_folds = []
+ valid_fold_num = 0
elif time.time() - start_time >= budget:
- val_loss_list.append(total_val_loss / valid_fold_num)
+ val_loss_list.append(cv_strategy(val_loss_folds))
break
val_loss = np.max(val_loss_list)
n = total_fold_num
@@ -559,6 +563,7 @@ def compute_estimator(
best_val_loss=np.Inf,
n_jobs=1,
estimator_class=None,
+ cv_strategy=None,
log_training_metric=False,
fit_kwargs={},
):
@@ -605,6 +610,7 @@ def compute_estimator(
task,
eval_metric,
best_val_loss,
+ cv_strategy,
log_training_metric=log_training_metric,
fit_kwargs=fit_kwargs,
)