Example #1
0
    def test_optuna_callback(self) -> None:
        params = {"verbose": -1}  # type: Dict[str, Any]
        dataset = lgb.Dataset(np.zeros((10, 10)))

        callback_mock = mock.MagicMock()

        study = optuna.create_study()
        tuner = LightGBMTunerCV(params, dataset, study=study, optuna_callbacks=[callback_mock],)

        with mock.patch.object(OptunaObjectiveCV, "_get_cv_scores", return_value=[1.0]):
            tuner.tune_params(["num_leaves"], 10, optuna.samplers.TPESampler(), "num_leaves")

        assert callback_mock.call_count == 10
Example #2
0
    def test_resume_run(self) -> None:
        params = {"verbose": -1}  # type: Dict
        dataset = lgb.Dataset(np.zeros((10, 10)))

        study = optuna.create_study()
        tuner = LightGBMTunerCV(params, dataset, study=study)

        with mock.patch.object(OptunaObjectiveCV, "_get_cv_scores", return_value=[1.0]):
            tuner.tune_regularization_factors()

        n_trials = len(study.trials)
        assert n_trials == len(study.trials)

        tuner2 = LightGBMTuner(params, dataset, valid_sets=dataset, study=study)
        with mock.patch.object(OptunaObjectiveCV, "_get_cv_scores", return_value=[1.0]):
            tuner2.tune_regularization_factors()
        assert n_trials == len(study.trials)
Example #3
0
    def test_inconsistent_study_direction(self, metric: str, study_direction: str) -> None:

        params = {}  # type: Dict[str, Any]
        if metric is not None:
            params["metric"] = metric
        train_set = lgb.Dataset(None)
        study = optuna.create_study(direction=study_direction)
        with pytest.raises(ValueError) as excinfo:
            LightGBMTunerCV(
                params, train_set, num_boost_round=5, early_stopping_rounds=2, study=study,
            )

        assert excinfo.type == ValueError
        assert str(excinfo.value).startswith("Study direction is inconsistent with the metric")
Example #4
0
    def _get_tunercv_object(
        self,
        params: Dict[str, Any] = {},
        train_set: lgb.Dataset = None,
        kwargs_options: Dict[str, Any] = {},
        study: Optional[optuna.study.Study] = None,
    ) -> LightGBMTunerCV:

        # Required keyword arguments.
        kwargs = dict(num_boost_round=5, early_stopping_rounds=2,
                      study=study)  # type: Dict[str, Any]
        kwargs.update(kwargs_options)

        runner = LightGBMTunerCV(params, train_set, **kwargs)
        return runner