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

WIP: [python-package] preserve params when copying Booster (fixes #5539) #6101

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[python-package] preserve params when copying Booster (fixes #5539)
  • Loading branch information
jameslamb committed Sep 15, 2023
commit 5b917e2912d2a2065d8e8502901eb4dc76a0dc87
5 changes: 4 additions & 1 deletion python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
@@ -3250,6 +3250,9 @@ def __init__(
'to create Booster instance')
self.params = params

# ensure params are updated on the C++ side
self.reset_parameter(params)

def __del__(self) -> None:
try:
if self._network:
@@ -3267,7 +3270,7 @@ def __copy__(self) -> "Booster":

def __deepcopy__(self, _) -> "Booster":
model_str = self.model_to_string(num_iteration=-1)
return Booster(model_str=model_str)
return Booster(model_str=model_str, params=self.params)

def __getstate__(self) -> Dict[str, Any]:
this = self.__dict__.copy()
5 changes: 4 additions & 1 deletion src/boosting/gbdt.cpp
Original file line number Diff line number Diff line change
@@ -795,7 +795,10 @@ void GBDT::ResetConfig(const Config* config) {

boosting_on_gpu_ = objective_function_ != nullptr && objective_function_->IsCUDAObjective() &&
!data_sample_strategy_->IsHessianChange(); // for sample strategy with Hessian change, fall back to boosting on CPU
tree_learner_->ResetBoostingOnGPU(boosting_on_gpu_);

if (tree_learner_ != nullptr) {
tree_learner_->ResetBoostingOnGPU(boosting_on_gpu_);
}

if (train_data_ != nullptr) {
data_sample_strategy_->ResetSampleConfig(new_config.get(), false);
3 changes: 3 additions & 0 deletions tests/c_api_test/test_.py
Original file line number Diff line number Diff line change
@@ -210,6 +210,9 @@ def test_booster():
c_str('model.txt'),
ctypes.byref(num_total_model),
ctypes.byref(booster2))
LIB.LGBM_BoosterResetParameter(
booster2,
c_str("app=binary metric=auc num_leaves=29 verbose=0"))
data = np.loadtxt(str(binary_example_dir / 'binary.test'), dtype=np.float64)
mat = data[:, 1:]
preb = np.empty(mat.shape[0], dtype=np.float64)
26 changes: 26 additions & 0 deletions tests/python_package_test/test_basic.py
Original file line number Diff line number Diff line change
@@ -823,3 +823,29 @@ def test_feature_names_are_set_correctly_when_no_feature_names_passed_into_Datas
data=np.random.randn(100, 3),
)
assert ds.construct().feature_name == ["Column_0", "Column_1", "Column_2"]


def test_booster_deepcopy_preserves_parameters():
bst = lgb.train(
params={'num_leaves': 5, 'verbosity': -1},
num_boost_round=2,
train_set=lgb.Dataset(np.random.rand(100, 2))
)
bst2 = copy.deepcopy(bst)
assert bst2.params == bst.params
assert bst.params["num_leaves"] == 5
assert bst.params["verbosity"] == -1


def test_booster_params_kwarg_overrides_params_from_model_string():
bst = lgb.train(
params={'num_leaves': 5, 'learning_rate': 0.708, 'verbosity': -1},
num_boost_round=2,
train_set=lgb.Dataset(np.random.rand(100, 2))
)
bst2 = lgb.Booster(
params={'num_leaves': 7},
model_str=bst.model_to_string()
)
assert bst2.params["num_leaves"] == 7
assert bst2.params["learning_rate"] == 0.708