[Python] Python catboost custom loss function

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I know that there is built-in Poisson loss function in catboost. I want to try to reproduce it. I used the tutorial example https://github.com/catboost/catboos...om_loss/custom_loss_and_metric_tutorial.ipynb

    My code for CustomObjective and CustomMetric:

    class CustomPoissonObjective(object):

    def calc_ders_range(self, approxes, targets, weights):

    assert len(approxes) == len(targets)
    if weights is not None:
    assert len(weights) == len(approxes)

    result = []
    for index in range(len(targets)):
    e = np.exp(approxes[index])
    der1 = e - targets[index]
    der2 = e

    if weights is not None:
    der1 *= weights[index]
    der2 *= weights[index]

    result.append((der1, der2))
    return result


    class CustomPoissonMetric(object):

    def is_max_optimal(self):
    return False

    def evaluate(self, approxes, target, weight):
    assert len(approxes) == 1
    assert len(target) == len(approxes[0])

    approx = approxes[0]

    error_sum = 0.0
    weight_sum = 0.0
    for i in range(len(approx)):
    w = 1.0 if weight is None else weight
    weight_sum += w
    error_sum += w * (np.exp(approx) - target * approx)

    return error_sum, weight_sum

    def get_final_error(self, error, weight):
    return error / (weight + 1e-38)


    When trying to reproduce the experiment with built-in loss_fuction='Poisson' and loss_fuction=CustomPoissonObjective() I get different results.

    I don't understand where I'm going wrong. I would really appreciate your help!

    I've tried CatBoostRegressor(loss_function=CustomPoissonObjective())

    Continue reading...

Compartilhe esta Página