Python catboost custom loss function

128 views Asked by At

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/catboost/blob/24aceedd3abf0ddb2f4791d4b8e054deb6de916f/catboost/tutorials/custom_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[i]
        weight_sum += w
        error_sum += w * (np.exp(approx[i]) - target[i] * approx[i])

    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())

0

There are 0 answers