Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom objective does not have the same loss curves. #5350

Closed
nd7141 opened this issue Jul 4, 2022 · 5 comments
Closed

Custom objective does not have the same loss curves. #5350

nd7141 opened this issue Jul 4, 2022 · 5 comments
Labels

Comments

@nd7141
Copy link

nd7141 commented Jul 4, 2022

I see that if I use custom L2 implementation instead of default one, I get much slower convergence. Why is that? I would expect two lines will be exactly the same.

Plot:

image

Code:

X, y = sklearn.datasets.fetch_california_housing(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, train_size=0.05, random_state=0)
print(f"Train Data: {X_train.shape[0]} examples, {X_train.shape[1]} features")

params = {
    'boosting_type': 'gbdt',
    'metric': {'l2'},
}

evals_result = {}
model = lightgbm.train(params, train_data,
                       num_boost_round=200,
                       valid_sets=[train_data, test_data],
                       evals_result=evals_result,
                      )

train_rmse_default = evals_result['training']['l2']

def l2_loss(y, data):
    t = data.get_label()
    grad = y - t 
    hess = np.ones_like(y)
    return grad, hess

evals_result = {}
model = lightgbm.train(params, train_data,
                       fobj = l2_loss,
                       num_boost_round=200,
                       valid_sets=[train_data, test_data],
                       evals_result=evals_result,
                      )

train_rmse_custom = evals_result['training']['l2']

@jmoralez
Copy link
Collaborator

jmoralez commented Jul 4, 2022

Hi @nd7141, I believe this is due to the different init scores, as explained in #5114 (comment). If you set boost_from_average to false with the built-in objective or set the init_score to the mean of the labels when using the custom one you should see identical results.

@nd7141
Copy link
Author

nd7141 commented Jul 5, 2022

Thank you @jmoralez Is there a way to set up init_scores for python API or it's only possible for CLI version?

@jmoralez
Copy link
Collaborator

jmoralez commented Jul 5, 2022

Yes, there's an example in #5114 (comment) passing it to Dataset but you can also pass it to the fit method in the scikit-learn API.

Please let us know if you have further doubts.

@github-actions
Copy link

github-actions bot commented Aug 5, 2022

This issue has been automatically closed because it has been awaiting a response for too long. When you have time to to work with the maintainers to resolve this issue, please post a new comment and it will be re-opened. If the issue has been locked for editing by the time you return to it, please open a new issue and reference this one. Thank you for taking the time to improve LightGBM!

@github-actions github-actions bot closed this as completed Aug 5, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants