You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While running OptunaSearchCV and enable_metadata_routing=True, set_score_request() is expected to share metadata with scoring function only and set_fit_request() is expected to share metadata with fit function only
If error_score='raise', any failed Trial is expected to raise error
Environment
Optuna version: 4.1.0
Optuna Integration version: 4.2.0
Python version: 3.10.8
OS: Windows-10-10.0.19045-SP0
(Optional) Other libraries and their versions:
sklearn 1.4.1.post1
Error messages, stack traces, or logs
1) UserWarning: Failed to report cross validation scores for TerminatorCallback, with error: The length of `scores` is expected to be greater than one.
warnings.warn(warn_msg)
2) TypeError: Ridge.fit() got an unexpected keyword argument 'score_param'
Steps to reproduce
If refit=False and n_jobs=1 then just observe warning that (I believe) should not be there
If refit=True or n_jobs>1 observe incorrect passing of score-only parameters into fit function
If metric returns NaN, error should be raised if error_score='raise". Currently a trial is failed and error message is empty - very hard to debug.
importnumpyasnpimportoptunaimportsklearnfromsklearn.linear_modelimportRidgefromsklearn.metricsimportmake_scorerfromoptuna.integrationimportOptunaSearchCVfromsklearn.model_selectionimportGridSearchCVsklearn.set_config(enable_metadata_routing=True)
defcustom_metric(y: np.ndarray, y_pred: np.ndarray, sample_weight: np.ndarray, score_param: str, *args, **kwargs) ->float:
assertscore_param=='a'assertnp.all(sample_weight==np.ones(4))
return0.01x=np.random.randn(10, 2)
y=np.random.randn(10)
y[-1] =np.nanw=np.ones(len(y))
score_param='a'sklearn_grid_params=dict(
param_grid={'alpha': [0.0, 0.1]},
scoring=make_scorer(
custom_metric,
greater_is_better=True,
).set_score_request(
sample_weight=True,
score_param=True
),
cv=[(np.arange(10)[:6], np.arange(10)[6:])],
return_train_score=False,
refit=False
)
optuna_grid_params=dict(
scoring=make_scorer(
custom_metric,
greater_is_better=True,
).set_score_request(
sample_weight=True,
score_param=True
),
n_jobs=1,
error_score='raise',
n_trials=10,
cv=[(np.arange(10)[:6], np.arange(10)[6:])],
param_distributions={'alpha': optuna.distributions.CategoricalDistribution([0.0, 0.1])},
random_state=77,
return_train_score=False,
refit=False
)
sklearn_grid=GridSearchCV(
estimator=Ridge().set_fit_request(sample_weight=True),
**sklearn_grid_params
)
optuna_grid=OptunaSearchCV(
estimator=Ridge().set_fit_request(sample_weight=True),
**optuna_grid_params
)
# runs without issues as expected. score_param is only fed into scoring functionsklearn_grid.fit(X=x, y=y, sample_weight=w, score_param=score_param)
# Observe warning "UserWarning: Failed to report cross validation scores for TerminatorCallback, with error: The length of `scores` is expected to be greater than one."optuna_grid.fit(X=x, y=y, sample_weight=w, score_param=score_param)
# Observe error when refit=True: TypeError: Ridge.fit() got an unexpected keyword argument 'score_param'optuna_grid_params['refit'] =Trueoptuna_grid=OptunaSearchCV(
estimator=Ridge().set_fit_request(sample_weight=True),
**optuna_grid_params
)
optuna_grid.fit(X=x, y=y, sample_weight=w, score_param=score_param)
# Observe same error when refit is set back to false and number of n_jobs > 1optuna_grid_params['refit'] =Falseoptuna_grid_params['n_jobs'] =2optuna_grid=OptunaSearchCV(
estimator=Ridge().set_fit_request(sample_weight=True),
**optuna_grid_params
)
optuna_grid.fit(X=x, y=y, sample_weight=w, score_param=score_param)
# Fail to observe error when custom metric returns NaN, although error_score='raise'defcustom_metric_with_nan(y: np.ndarray, y_pred: np.ndarray, sample_weight: np.ndarray, score_param: str, *args, **kwargs) ->float:
returnnp.nanoptuna_grid_params['scoring'] =make_scorer(
custom_metric_with_nan,
greater_is_better=True,
).set_score_request(
sample_weight=True,
score_param=True
)
optuna_grid_params['n_jobs'] =1optuna_grid=OptunaSearchCV(
estimator=Ridge().set_fit_request(sample_weight=True),
**optuna_grid_params
)
optuna_grid.fit(X=x, y=y, sample_weight=w, score_param=score_param)
assertoptuna_grid_params['error_score'] =='raise'
Additional context (optional)
No response
The text was updated successfully, but these errors were encountered:
Expected behavior
Environment
sklearn 1.4.1.post1
Error messages, stack traces, or logs
Steps to reproduce
Additional context (optional)
No response
The text was updated successfully, but these errors were encountered: