BayesSearchCV-lightgbm-提早停止-“ ValueError:没有足够的值可解包”

时间:2019-12-28 07:29:11

标签: python scikit-learn lightgbm

您能帮我解决以下问题吗?预先非常感谢。

在没有fit_params=fit_params的情况下,下面的代码可以正常工作,但是我想尝试尽早停止使用lgbm。 我确实尝试搜索线索,但发现资源有限,但是lightgbm和BayesSearchCV有一些github问题。

lg = lgb.LGBMClassifier(random_state=42, n_jobs=-1, objective='multiclass', n_estimators=5000)

fullPipeline = Pipeline(steps=[
    ('scaler', StandardScaler()),
    ('model', lg)
])

param_space =  {'model__max_depth': [2, 63],
    'model__num_leaves': [7, 4095],
}

fit_params = {
    'early_stopping_rounds':30,
    'eval_metric':'accuracy',
    'eval_set':[(xValid, yValid)],
}

BSLGB = BayesSearchCV(fullPipeline, param_space, random_state=42, scoring='accuracy', cv=5, n_iter=50, verbose=3, n_jobs=-1,
                     fit_params=fit_params)
%time BSLGB.fit(xTrain.astype(float), yTrain)

例外:

ValueError                                Traceback (most recent call last)
<timed eval> in <module>

C:\Anaconda3x64\envs\ml\lib\site-packages\skopt\searchcv.py in fit(self, X, y, groups, callback)
    652                 optim_result = self._step(
    653                     X, y, search_space, optimizer,
--> 654                     groups=groups, n_points=n_points_adjusted
    655                 )
    656                 n_iter -= n_points

C:\Anaconda3x64\envs\ml\lib\site-packages\skopt\searchcv.py in _step(self, X, y, search_space, optimizer, groups, n_points)
    548         refit = self.refit
    549         self.refit = False
--> 550         self._fit(X, y, groups, params_dict)
    551         self.refit = refit
    552 

C:\Anaconda3x64\envs\ml\lib\site-packages\skopt\searchcv.py in _fit(self, X, y, groups, parameter_iterable)
    401                 error_score=self.error_score
    402             )
--> 403             for parameters in parameter_iterable
    404             for train, test in cv_iter)
    405 

C:\Anaconda3x64\envs\ml\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
    928 
    929             with self._backend.retrieval_context():
--> 930                 self.retrieve()
    931             # Make sure that we get a last message telling us we are done
    932             elapsed_time = time.time() - self._start_time

C:\Anaconda3x64\envs\ml\lib\site-packages\sklearn\externals\joblib\parallel.py in retrieve(self)
    831             try:
    832                 if getattr(self._backend, 'supports_timeout', False):
--> 833                     self._output.extend(job.get(timeout=self.timeout))
    834                 else:
    835                     self._output.extend(job.get())

C:\Anaconda3x64\envs\ml\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in wrap_future_result(future, timeout)
    519         AsyncResults.get from multiprocessing."""
    520         try:
--> 521             return future.result(timeout=timeout)
    522         except LokyTimeoutError:
    523             raise TimeoutError()

C:\Anaconda3x64\envs\ml\lib\concurrent\futures\_base.py in result(self, timeout)
    430                 raise CancelledError()
    431             elif self._state == FINISHED:
--> 432                 return self.__get_result()
    433             else:
    434                 raise TimeoutError()

C:\Anaconda3x64\envs\ml\lib\concurrent\futures\_base.py in __get_result(self)
    382     def __get_result(self):
    383         if self._exception:
--> 384             raise self._exception
    385         else:
    386             return self._result

ValueError: not enough values to unpack (expected 2, got 1)

1 个答案:

答案 0 :(得分:0)

此问题的根本原因是我将管道而不是模型传递给BayesSearchCV。同时,我的fit_params没有前缀。要解决:

fit_params = {
    'model__early_stopping_rounds':30,
    'model__eval_metric':'multi_logloss',
    'model__eval_set':[(xValid, yValid)],
}