예제 #1
0
    def _run_trial(self, func, catch):
        # type: (ObjectiveFuncType, Union[Tuple[()], Tuple[Type[Exception]]]) -> trial_module.Trial

        trial_id = self._storage.create_new_trial(self.study_id)
        trial = trial_module.Trial(self, trial_id)
        trial_number = trial.number

        try:
            result = func(trial)
        except structs.TrialPruned as e:
            message = 'Setting status of trial#{} as {}. {}'.format(
                trial_number, structs.TrialState.PRUNED, str(e))
            self.logger.info(message)
            self._storage.set_trial_state(trial_id, structs.TrialState.PRUNED)
            return trial
        except catch as e:
            message = 'Setting status of trial#{} as {} because of the following error: {}'\
                .format(trial_number, structs.TrialState.FAIL, repr(e))
            self.logger.warning(message, exc_info=True)
            self._storage.set_trial_system_attr(trial_id, 'fail_reason',
                                                message)
            self._storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            return trial
        finally:
            # The following line mitigates memory problems that can be occurred in some
            # environments (e.g., services that use computing containers such as CircleCI).
            # Please refer to the following PR for further details:
            # https://github.com/pfnet/optuna/pull/325.
            if self.force_garbage_collection:
                gc.collect()

        try:
            result = float(result)
        except (
                ValueError,
                TypeError,
        ):
            message = 'Setting status of trial#{} as {} because the returned value from the ' \
                      'objective function cannot be casted to float. Returned value is: ' \
                      '{}'.format(trial_number, structs.TrialState.FAIL, repr(result))
            self.logger.warning(message)
            self._storage.set_trial_system_attr(trial_id, 'fail_reason',
                                                message)
            self._storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            return trial

        if math.isnan(result):
            message = 'Setting status of trial#{} as {} because the objective function ' \
                      'returned {}.'.format(trial_number, structs.TrialState.FAIL, result)
            self.logger.warning(message)
            self._storage.set_trial_system_attr(trial_id, 'fail_reason',
                                                message)
            self._storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            return trial

        trial.report(result)
        self._storage.set_trial_state(trial_id, structs.TrialState.COMPLETE)
        self._log_completed_trial(trial_number, result)

        return trial
예제 #2
0
    def ask(self) -> trial_module.Trial:
        """Create a new trial from which hyperparameters can be suggested.

        This method is part of an alternative to :func:`~optuna.study.Study.optimize` that allows
        controlling the lifetime of a trial outside the scope of ``func``. Each call to this
        method should be followed by a call to :func:`~optuna.study.Study.tell` to finish the
        created trial.

        Example:

            .. testcode::

                import optuna


                study = optuna.create_study()

                trial = study.ask()

                x = trial.suggest_float("x", -1, 1)

                study.tell(trial, x ** 2)

        Returns:
            A :class:`~optuna.trial.Trial`.
        """

        # Sync storage once every trial.
        self._storage.read_trials_from_remote_storage(self._study_id)

        trial_id = self._pop_waiting_trial_id()
        if trial_id is None:
            trial_id = self._storage.create_new_trial(self._study_id)
        return trial_module.Trial(self, trial_id)
예제 #3
0
    def _ask(self) -> trial_module.Trial:
        # Sync storage once at the beginning of the objective evaluation.
        self._storage.read_trials_from_remote_storage(self._study_id)

        trial_id = self._pop_waiting_trial_id()
        if trial_id is None:
            trial_id = self._storage.create_new_trial(self._study_id)
        return trial_module.Trial(self, trial_id)
예제 #4
0
파일: study.py 프로젝트: wakamori/optuna
    def _run_trial(self, func, catch):
        # type: (ObjectiveFuncType, Union[Tuple[()], Tuple[Type[Exception]]]) -> trial_module.Trial

        trial_id = self.storage.create_new_trial_id(self.study_id)
        trial = trial_module.Trial(self, trial_id)

        try:
            result = func(trial)
        except structs.TrialPruned as e:
            message = 'Setting trial status as {}. {}'.format(
                structs.TrialState.PRUNED, str(e))
            self.logger.info(message)
            self.storage.set_trial_state(trial_id, structs.TrialState.PRUNED)
            return trial
        except catch as e:
            message = 'Setting trial status as {} because of the following error: {}'.format(
                structs.TrialState.FAIL, repr(e))
            self.logger.warning(message, exc_info=True)
            self.storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            self.storage.set_trial_system_attr(trial_id, 'fail_reason',
                                               message)
            return trial

        try:
            result = float(result)
        except (
                ValueError,
                TypeError,
        ):
            message = 'Setting trial status as {} because the returned value from the ' \
                      'objective function cannot be casted to float. Returned value is: ' \
                      '{}'.format(structs.TrialState.FAIL, repr(result))
            self.logger.warning(message)
            self.storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            self.storage.set_trial_system_attr(trial_id, 'fail_reason',
                                               message)
            return trial

        if math.isnan(result):
            message = 'Setting trial status as {} because the objective function returned ' \
                      '{}.'.format(structs.TrialState.FAIL, result)
            self.logger.warning(message)
            self.storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            self.storage.set_trial_system_attr(trial_id, 'fail_reason',
                                               message)
            return trial

        trial.report(result)
        self.storage.set_trial_state(trial_id, structs.TrialState.COMPLETE)
        self._log_completed_trial(result)

        return trial
예제 #5
0
    def get_candidates(self, trial=None):
        """
        This function converts the searchspace to a candidate_list that can then be used to distribute via MPI.

        :param searchspace: converted hyperparameter space
        """

        candidates_list = list()
        N = self.max_iterations
        for n in range(N):
            print(n)
            # Todo: Ugly hack that does not even work...
            from optuna import trial as trial_module
            # temp_study = optuna.create_study()
            trial_id = self.study._storage.create_new_trial_id(0)
            trial = trial_module.Trial(self.study, trial_id)
            ## trial.report(result)
            ## self._storage.set_trial_state(trial_id, structs.TrialState.COMPLETE)
            ## self._log_completed_trial(trial_number, result)

            params = {}
            for name, param in self._searchspace.items():
                if param["domain"] == "categorical":
                    params[name] = trial.suggest_categorical(
                        name, param["data"])
                else:
                    params[name] = trial.suggest_uniform(
                        name, param["data"][0], param["data"][1])
            candidates_list.append(CandidateDescriptor(**params))

        return candidates_list

        N = self.max_iterations
        for n in range(N):
            params = {}
            for name, param in self._searchspace.items():
                if param["domain"] == "categorical":
                    params[name] = trial.suggest_categorical(
                        name, param["data"])
                else:
                    params[name] = trial.suggest_uniform(
                        name, param["data"][0], param["data"][1])
            candidates_list.append(CandidateDescriptor(**params))

        return candidates_list
예제 #6
0
파일: study.py 프로젝트: nzw0301/optuna
    def ask(
        self,
        fixed_distributions: Optional[Dict[str, BaseDistribution]] = None
    ) -> trial_module.Trial:
        """Create a new trial from which hyperparameters can be suggested.

        This method is part of an alternative to :func:`~optuna.study.Study.optimize` that allows
        controlling the lifetime of a trial outside the scope of ``func``. Each call to this
        method should be followed by a call to :func:`~optuna.study.Study.tell` to finish the
        created trial.

        .. seealso::

            The :ref:`ask_and_tell` tutorial provides use-cases with examples.

        Example:

            Getting the trial object with the :func:`~optuna.study.Study.ask` method.

            .. testcode::

                import optuna


                study = optuna.create_study()

                trial = study.ask()

                x = trial.suggest_float("x", -1, 1)

                study.tell(trial, x ** 2)

        Example:

            Passing previously defined distributions to the :func:`~optuna.study.Study.ask`
            method.

            .. testcode::

                import optuna


                study = optuna.create_study()

                distributions = {
                    "optimizer": optuna.distributions.CategoricalDistribution(["adam", "sgd"]),
                    "lr": optuna.distributions.LogUniformDistribution(0.0001, 0.1),
                }

                # You can pass the distributions previously defined.
                trial = study.ask(fixed_distributions=distributions)

                # `optimizer` and `lr` are already suggested and accessible with `trial.params`.
                assert "optimizer" in trial.params
                assert "lr" in trial.params

        Args:
            fixed_distributions:
                A dictionary containing the parameter names and parameter's distributions. Each
                parameter in this dictionary is automatically suggested for the returned trial,
                even when the suggest method is not explicitly invoked by the user. If this
                argument is set to :obj:`None`, no parameter is automatically suggested.

        Returns:
            A :class:`~optuna.trial.Trial`.
        """

        fixed_distributions = fixed_distributions or {}

        # Sync storage once every trial.
        self._storage.read_trials_from_remote_storage(self._study_id)

        trial_id = self._pop_waiting_trial_id()
        if trial_id is None:
            trial_id = self._storage.create_new_trial(self._study_id)
        trial = trial_module.Trial(self, trial_id)

        for name, param in fixed_distributions.items():
            trial._suggest(name, param)

        return trial
예제 #7
0
    def _run_trial(
            self,
            func,  # type: ObjectiveFuncType
            catch,  # type: Union[Tuple[()], Tuple[Type[Exception]]]
            gc_after_trial,  # type: bool
    ):
        # type: (...) -> trial_module.Trial

        trial_id = self._pop_waiting_trial_id()
        if trial_id is None:
            trial_id = self._storage.create_new_trial(self._study_id)
        trial = trial_module.Trial(self, trial_id)
        trial_number = trial.number

        try:
            result = func(trial)
        except exceptions.TrialPruned as e:
            message = "Setting status of trial#{} as {}. {}".format(
                trial_number, structs.TrialState.PRUNED, str(e))
            _logger.info(message)

            # Register the last intermediate value if present as the value of the trial.
            # TODO(hvy): Whether a pruned trials should have an actual value can be discussed.
            frozen_trial = self._storage.get_trial(trial_id)
            last_step = frozen_trial.last_step
            if last_step is not None:
                self._storage.set_trial_value(
                    trial_id, frozen_trial.intermediate_values[last_step])
            self._storage.set_trial_state(trial_id, structs.TrialState.PRUNED)
            return trial
        except Exception as e:
            message = "Setting status of trial#{} as {} because of the following error: {}".format(
                trial_number, structs.TrialState.FAIL, repr(e))
            _logger.warning(message, exc_info=True)
            self._storage.set_trial_system_attr(trial_id, "fail_reason",
                                                message)
            self._storage.set_trial_state(trial_id, structs.TrialState.FAIL)

            if isinstance(e, catch):
                return trial
            raise
        finally:
            # The following line mitigates memory problems that can be occurred in some
            # environments (e.g., services that use computing containers such as CircleCI).
            # Please refer to the following PR for further details:
            # https://github.com/optuna/optuna/pull/325.
            if gc_after_trial:
                gc.collect()

        try:
            result = float(result)
        except (
                ValueError,
                TypeError,
        ):
            message = (
                "Setting status of trial#{} as {} because the returned value from the "
                "objective function cannot be casted to float. Returned value is: "
                "{}".format(trial_number, structs.TrialState.FAIL,
                            repr(result)))
            _logger.warning(message)
            self._storage.set_trial_system_attr(trial_id, "fail_reason",
                                                message)
            self._storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            return trial

        if math.isnan(result):
            message = (
                "Setting status of trial#{} as {} because the objective function "
                "returned {}.".format(trial_number, structs.TrialState.FAIL,
                                      result))
            _logger.warning(message)
            self._storage.set_trial_system_attr(trial_id, "fail_reason",
                                                message)
            self._storage.set_trial_state(trial_id, structs.TrialState.FAIL)
            return trial

        self._storage.set_trial_value(trial_id, result)
        self._storage.set_trial_state(trial_id, structs.TrialState.COMPLETE)
        self._log_completed_trial(trial_number, result)

        return trial