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

[python-package] bug : dump_model json loads failed #6431

Open
Fhwang0926 opened this issue Apr 29, 2024 · 1 comment
Open

[python-package] bug : dump_model json loads failed #6431

Fhwang0926 opened this issue Apr 29, 2024 · 1 comment

Comments

@Fhwang0926
Copy link

Description

error when "json.loads()" with "\" special charset using key

for exam

{ "\" : "aa" }

so should be exist escape code

Reproducible example

it happend used "shap.TreeExplainer", it is anothor open source, but referce it so write issue

Environment info

LightGBM version or commit hash:

using lightgbm-4.3.0.dist-info

Command(s) you used to install LightGBM

Traceback (most recent call last):
  File "/home/f1/wmds-deep-scanner/agent.py", line 411, in predict
    explain = getXaiDetector(model_name, value)
  File "/home/f1/wmds-deep-scanner/agent.py", line 214, in getXaiDetector
    explainer = shap.TreeExplainer(MODEL_MAP[model_name])
  File "/home/f1/wmds-deep-scanner/virtual/lib/python3.10/site-packages/shap/explainers/_tree.py", line 195, in __init__
    self.model = TreeEnsemble(model, self.data, self.data_missing, model_output)
  File "/home/f1/wmds-deep-scanner/virtual/lib/python3.10/site-packages/shap/explainers/_tree.py", line 1140, in __init__
    tree_info = self.original_model.dump_model()["tree_info"]
  File "/home/f1/wmds-deep-scanner/virtual/lib/python3.10/site-packages/lightgbm/basic.py", line 4387, in dump_model
    ret = json.loads(string_buffer.value.decode('utf-8'), object_hook=object_hook)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 9 column 468 (char 631)

Additional Comments

it is happend under here, into basic.py

origin code

# basic.py, line 4661

ret = json.loads(string_buffer.value.decode("utf-8"), object_hook=object_hook)
ret["pandas_categorical"] = json.loads(
    json.dumps(
        self.pandas_categorical,
        default=_json_default_with_numpy,
    )
)

fix code

# basic.py, line 4661

ret = json.loads(string_buffer.value.decode("utf-8").replace("\\", "\\\\"), object_hook=object_hook)
ret["pandas_categorical"] = json.loads(
    json.dumps(
        self.pandas_categorical,
        default=_json_default_with_numpy,
    )
)

how about it ?, Can i do pull request ?

@jameslamb
Copy link
Collaborator

Thanks for using LightGBM.

I don't fully understand this report. Can you provide a minimal, reproducible example showing this behavior? For example, modify this code in a way that causes LightGBM to generate this error:

import lightgbm as lgb
from sklearn.datasets import make_regression

X, y = make_regression()
model = lgb.LGBMRegressor().fit(X , y)

@jameslamb jameslamb changed the title bug : dump_model json loads failed [python-package] bug : dump_model json loads failed Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants