'TPESampler' 对象没有属性“_group”

'TPESampler' object has no attribute '_group'

今天在 Optuna 2.8.0 的 Colab 上加载 后尝试优化它时,我的研究中出现了以下错误。每次试验开始或结束时,我都会用 joblib 保存一项研究,每个试验都保存在一个单独的文件中。我以前从来没有遇到过这个问题,也不确定到底是什么原因造成的。

Colab 显示以下跟踪:

/usr/local/lib/python3.7/dist-packages/optuna/study.py in optimize(self, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)
    408             callbacks=callbacks,
    409             gc_after_trial=gc_after_trial,
--> 410             show_progress_bar=show_progress_bar,
    411         )
    412 

/usr/local/lib/python3.7/dist-packages/optuna/_optimize.py in _optimize(study, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)
     73                 reseed_sampler_rng=False,
     74                 time_start=None,
---> 75                 progress_bar=progress_bar,
     76             )
     77         else:

/usr/local/lib/python3.7/dist-packages/optuna/_optimize.py in _optimize_sequential(study, func, n_trials, timeout, catch, callbacks, gc_after_trial, reseed_sampler_rng, time_start, progress_bar)
    160 
    161         try:
--> 162             trial = _run_trial(study, func, catch)
    163         except Exception:
    164             raise

/usr/local/lib/python3.7/dist-packages/optuna/_optimize.py in _run_trial(study, func, catch)
    195                 failed_trial_callback(study, failed_trial)
    196 
--> 197     trial = study.ask()
    198 
    199     state: Optional[TrialState] = None

/usr/local/lib/python3.7/dist-packages/optuna/study.py in ask(self, fixed_distributions)
    485         if trial_id is None:
    486             trial_id = self._storage.create_new_trial(self._study_id)
--> 487         trial = trial_module.Trial(self, trial_id)
    488 
    489         for name, param in fixed_distributions.items():

/usr/local/lib/python3.7/dist-packages/optuna/trial/_trial.py in __init__(self, study, trial_id)
     55         self.storage = self.study._storage
     56 
---> 57         self._init_relative_params()
     58 
     59     def _init_relative_params(self) -> None:

/usr/local/lib/python3.7/dist-packages/optuna/trial/_trial.py in _init_relative_params(self)
     65         self.relative_search_space = self.study.sampler.infer_relative_search_space(study, trial)
     66         self.relative_params = self.study.sampler.sample_relative(
---> 67             study, trial, self.relative_search_space
     68         )
     69 

/usr/local/lib/python3.7/dist-packages/optuna/samplers/_tpe/sampler.py in sample_relative(self, study, trial, search_space)
    327         self._raise_error_if_multi_objective(study)
    328 
--> 329         if self._group:
    330             assert self._search_space_group is not None
    331             params = {}

AttributeError: 'TPESampler' object has no attribute '_group'

PS。有趣的是,这项研究的问题似乎并不存在于我的本地机器上。我使用的是 2.5.0.

版本

刚从Optuna开发者那里得到答案:

A (private) attribute was introduced to the TPESampler in v2.8 and if you've pickled or serialized the sampler before this introduction, you'll encounter this AttributeError when unpickling it with v2.8.

这个问题的解决方案是重新实例化采样器对象并将其替换为 study.sampler:

study = optuna.load_study(...) 
sampler = optuna.samplers.TPESampler() 
study.sampler = sampler
study.optimize(...)

PS。修枝剪可能会出现同样的问题。如果是这样,也重新实例化修剪器!