-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[python] Lightgbm.sklearn LGBMRegressor Subclassing Support #3758
Comments
Having the same issue with LGBMClassifier, subclassing using the sklearn interface seems quite burdensome. |
This issue was originally opened in January 2021, prior to the There have been many changes in
I don't think it's accurate to say "subclassing does not work". For example, consider the sample code below. On latest git clone --recursive https://github.com/microsoft/LightGBM.git
cd LightGBM/python-package
python setup.py install from lightgbm import LGBMRegressor
from copy import deepcopy
class CustomRegressor(LGBMRegressor):
"""
Like ``lightgbm.sklearn.LGBMRegressor``, but always
sets ``learning_rate`` to 0.123 regardless of what you pass to the constructor,
just to show it can be done.
"""
def set_params(self, **params):
new_params = deepcopy(super().get_params())
new_params['learning_rate'] = 0.123
super().set_params(**new_params)
# instantiate a model
reg = CustomRegressor(learning_rate = 0.3)
# notice: the learning_rate value passed to the constructor was ignored and replaced with 0.123
reg.get_params()["learniing_rate"]
# 0.123
# confirm that you can train model with this sub-class
from sklearn.datasets import make_regression
X, y = make_regression()
reg.fit(X, y)
# CustomRegressor(learning_rate=0.123) I think it would be more accurate to say that "it is not obvious how to create a sub-class of one of This is definitely challenging! I was confused by this too until I got some help from @StrikerRUS in #3883 (for example, #3883 (review)). An approach like the one in the original post will not work because it is incompatible with From https://scikit-learn.org/stable/developers/develop.html#instantiation
And in https://scikit-learn.org/stable/developers/develop.html#parameters-and-init
This is why LightGBM/python-package/lightgbm/sklearn.py Line 353 in a77260f
And it's why LightGBM/python-package/lightgbm/dask.py Lines 1118 to 1140 in a77260f
If you want to achieve this behavior of "set some parameters based on the value of others" , you might have an easier time and run into less surprises by overriding I think that the sample code below, for example, accomplishes the same thing as the intent of the post at the top of this issue ("set other GPU parameters to specific values based on whether or not I'm using the GPU"). from lightgbm import LGBMRegressor
class LRegressor(LGBMRegressor):
def set_params(self, **params):
new_params = deepcopy(params)
if new_params.get("device", None) == "gpu":
print("using GPU")
self.gpu_device_id = 0
new_params['gpu_device_id'] = 0
new_params['gpu_platform_id'] = 0
new_params['gpu_use_db'] = True
new_params['max_bin'] = 256
else:
print("not using GPU")
super().set_params(**new_params)
mod = LRegressor(device="gpu")
# notice that all those params like `gpu_device_id`, `gpu_use_db` are set
mod.get_params() |
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! |
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. |
I am trying to plug the LGBMRegressor into my ML pipeline and so need to subclass it.
That does not seem to work, well at least parameters are not set and I have not gotten any further than that.
Here is some code to reproduce the problem:
Now when trying to use this:
There is no effect. For some reason subclassing does not work?!
On the other hand getting params works for LGBMRegressor directly, as it should:
Any insight as to why subclassing is not working? Shouldn't it work?
The text was updated successfully, but these errors were encountered: