4

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Sam Shleifer
  • 1,716
  • 2
  • 18
  • 29

1 Answers1

4

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.

Mikhail Stepanov
  • 3,680
  • 3
  • 23
  • 24
  • The two formats (`save_model` and `save_model`) contain the same information and one format should be translatable to another ... but lightgbm does not have this function for the moment :( – Alex Apr 16 '20 at 13:33