Example #1
0
def compute_objective(objective, history):
    # set a multiplier to turn objective to its negative
    import socket
    print('hyliu history -------------------- ', history, ' ',
          socket.gethostname())
    # print(objective, type(objective))

    if type(objective) is str:
        if objective[0] == "-":
            multiplier = -1
            objective = objective[1:]
        else:
            multiplier = 1

    if type(objective) is str and ("__" in objective or objective in history):
        split_objective = objective.split("__")
        kind = split_objective[1] if len(split_objective) > 1 else "last"
        mname = split_objective[0]

        if kind == "min":
            res = min(history[mname])
        elif kind == "max":
            res = max(history[mname])
        else:  # 'last' or else, by default it will be the last one
            res = history[mname][-1]
            # print(f' hyliu res last {res} ')

        return multiplier * res
    elif callable(objective):
        func = objective
        return func(history)
    else:
        raise WrongProblemObjective(objective)
Example #2
0
    def check_objective(self, objective):
        """
        :meta private:
        """

        if not type(objective) is str and not callable(objective):
            raise WrongProblemObjective(objective)
Example #3
0
def compute_objective(objective, history: dict) -> float:
    """Compute an objective based on the history

    Args:
        objective (str|callable): the definition of the objective. If ``str`` has to be one of the metrics' name (e.g. ``"acc"``). It can have a prefix ``"-"`` to ask for the negative scalar. It can have a suffix ``__max``, ``__min`` or ``__last`` depending if the maximum, minimum of last epoch objective should be used. If it is a callable, it will be passed the ``history`` and has to return a scalar value.
        history (dict): The training history of the model.

    Raises:
        WrongProblemObjective: raised when the value of ``objective`` is not correct.

    Returns:
        float: the deducted objective from ``history``.
    """
    # set a multiplier to turn objective to its negative
    if type(objective) is str:
        if objective[0] == "-":
            multiplier = -1
            objective = objective[1:]
        else:
            multiplier = 1

    if type(objective) is str and ("__" in objective or objective in history):
        split_objective = objective.split("__")
        kind = split_objective[1] if len(split_objective) > 1 else "last"
        mname = split_objective[0]
        if kind == "min":
            res = min(history[mname])
        elif kind == "max":
            res = max(history[mname])
        else:  # 'last' or else, by default it will be the last one
            res = history[mname][-1]
        return multiplier * res
    elif callable(objective):
        func = objective
        return func(history)
    else:
        raise WrongProblemObjective(objective)
    def check_objective(self, objective):

        if not type(objective) is str and not callable(objective):
            raise WrongProblemObjective(objective)