Home > Net >  Save and reuse ML model
Save and reuse ML model

Time:06-22

First of all, let me introduce myself. I am a young researcher and I am interested in machine learning. I created a model, trained, tested and validated. Now I would like to know if there is a way to save my trained model.

I am also interested in knowing if the model is saved trained.

Finally, is there a way to use the saved (and trained) model with new data without having to train the model again?

I work with python!

CodePudding user response:

There may be a better way to do this but this is how I have done it before, with python.

So you have a ML model that you have trained. That model is basically just a set of parameters. Depending on what module you are using, you can save those parameters in a file, and import them to regenerate your model later.

Perhaps a simpler way is to save the model object entirely in a file using pickling.

https://docs.python.org/3/library/pickle.html

You can dump the object into a file, and load it back when you want to run it again.

CodePudding user response:

Welcome to the community.

Yes, you may save the trained model and reuse it later. There are several ways to do so and I will introduce you to a couple of them here. However, please note which library you used to build your model and use a method for that library.

  1. Pickel: Pickle is the standard way of serializing objects in Python.

    import pickle pickle.dump(model, open(filename, 'wb')) loaded_model = pickle.load(open(filename, 'rb'))

  2. Joblib: Joblib is part of the SciPy ecosystem and provides utilities for pipelining Python jobs.

    import joblib joblib.dump(model, filename) loaded_model = joblib.load(filename)

  3. Finally, as suggested by others, if you used libraries such as Tensorflow to build and train your models, please note that they have extensive ways to work with the built model and save/load it. Please check the following information:

Tensorflow Save and Load model

  • Related