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

fix: replace json with pickle for storing lgbm params #190

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
9 changes: 6 additions & 3 deletions optuna_integration/_lightgbm_tuner/optimize.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

import abc
import base64
from collections.abc import Callable
from collections.abc import Container
from collections.abc import Generator
from collections.abc import Iterable
from collections.abc import Iterator
from collections.abc import Sequence
import copy
import json
import os
import pickle
import time
Expand Down Expand Up @@ -267,8 +267,10 @@ def _postprocess(
trial._trial_id, _AVERAGE_ITERATION_TIME_KEY, average_iteration_time
)
trial.storage.set_trial_system_attr(trial._trial_id, _STEP_NAME_KEY, self.step_name)

serialized_params = base64.b64encode(pickle.dumps(self.lgbm_params)).decode('utf-8')
trial.storage.set_trial_system_attr(
trial._trial_id, _LGBM_PARAMS_KEY, json.dumps(self.lgbm_params)
trial._trial_id, _LGBM_PARAMS_KEY, serialized_params
)

self.trial_count += 1
Expand Down Expand Up @@ -439,7 +441,8 @@ def best_score(self) -> float:
def best_params(self) -> dict[str, Any]:
"""Return parameters of the best booster."""
try:
return json.loads(self.study.best_trial.system_attrs[_LGBM_PARAMS_KEY])
serialized_params = self.study.best_trial.system_attrs[_LGBM_PARAMS_KEY]
return pickle.loads(base64.b64decode(serialized_params.encode('utf-8')))
except ValueError:
# Return the default score because no trials have completed.
params = copy.deepcopy(_DEFAULT_LIGHTGBM_PARAMETERS)
Expand Down
Loading