Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LightGBM: loading from json

I am trying to load a LightGBM.Booster from a JSON file pointer, and can't find an example online.

    import json ,lightgbm
    import numpy as np
    X_train = np.arange(0, 200).reshape((100, 2))
    y_train = np.tile([0, 1], 50)
    tr_dataset = lightgbm.Dataset(X_train, label=y_train)
    booster = lightgbm.train({}, train_set=tr_dataset)
    model_json = booster.dump_model()
    with open('model.json', 'w+') as f:
        json.dump(model_json, f, indent=4)
    with open('model.json') as f2:
        model_json = json.load(f2)

How can I create a lightGBM booster from f2 or model_json? This snippet only shows dumping to JSON. model_from_string might help but seems to require an instance of the booster, which I won't have before loading.

like image 661
Sam Shleifer Avatar asked Sep 03 '25 04:09

Sam Shleifer


1 Answers

There's no such method for creation of Booster directly from json. No such method in the source code or documentation, also there's no github issue.

Because of it, I just load models from a text file via

gbm.save_model('model.txt')  # gbm is trained Booster instance
#  ...
bst = lgb.Booster(model_file='model.txt')

or use pickle to dump and load models:

import pickle
pickle.dump(gbm, open('model.pkl', 'wb'))
# ...
gbm = pickle.load(open('model.pkl', 'rb'))

Unforunately, pickle files are unreadable (or, at least, this files are not so clear). But it's better than nothing.

like image 70
Mikhail Stepanov Avatar answered Sep 05 '25 01:09

Mikhail Stepanov