After tuning my BERT model I want to save it to use it later (once the task is learned). I use these 2 lines to do it.
model_to_save.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
The problem is that it creates a +400Mb file that I cant upload to github because it is too big.
Is there a way to save the model creating a smaller file and loading it back later?
Regards
Related
I already have a built predictive model written in Python, however currently it is executed by hand and functions on a single data file. I am hoping to generalize the model so that it can read in different datasets from my backend, each time effectively producing a different model since we are using different data for training as well. How would I be able to add the model onto my backend then?
Store the model as a pickle and read it from your backend when you need analog to your training data.
But you might want to checkout MLFlow for an integrated model handling solution. It is possible to run it on prem. With MLFlow you can easily implement a proper ML lifecycle. You can store your training stats and keep the history of your trained models.
I am teaching myself to code Convolutional Neural Networks. In particular I am looking at the "Dogs vs. Cats" challenge (https://medium.com/#mrgarg.rajat/kaggle-dogs-vs-cats-challenge-complete-step-by-step-guide-part-2-e9ee4967b9). I am using PyCharm.
In PyCharm, is there a way of using the trained model to make a prediction on the test data without having to run the entire file each time (and thus retrain the model each time)? Additionally, is there a way to skip the part of the script that prepares the data for input into the CNN? In a similar manner, does PyCharm store variables- can I print individual variables after the script has been run.
Would it be better if I used a different IDLE?
You can use sklearn joblib to save the trained model as a pickle and use it later for predictions.
from sklearn.externals import joblib
# Save the model as a pickle in a file
joblib.dump(knn, 'filename.pkl')
# Load the model from the file
knn_from_joblib = joblib.load('filename.pkl')
# Use the loaded model to make predictions
knn_from_joblib.predict(X_test)
I have trained a model in keras with the help of transfer learning on the top of the vgg16 model as mentioned in the blog Building powerful image classification using model using very little data.
When I saved the model using model.save() method in keras the ouput file size(in .h5) format was about 200MB.
I need to push this code in github but we can't upload a file whose size is more than 100MB.
So, is there any way so that I can save the model in a file of lesser size.
Note :: I need the model only for prediction purpose
The docs have your solution. save is optimal, but is there to easily save and load the entire state for continuous work. You need much less if I understand correctly:
Saving/loading only a model's architecture
json_string = model.to_json()
yaml_string = model.to_yaml()
Saving/loading only a model's weights
model.save_weights('my_model_weights.h5')
Both I guess are much leaner then the entire working area. You can use these to recreate the model later on, as stated in the docs (load_weights, load_from_json, etc.).
I am not aware of a way to save such a model using significantly less space.
However, if your problem is not the file size itself but the upload file size limit, you can try to split the file into several chunks that are smaller than that limit and upload those, e.g. using 7-Zip. See this link for a tutorial on how to split the file.
I'm new to python and working on machine learning. I have trained LinearSVC from sklearn.svm, and training takes quite a long time, mostly because of stemming (7-8 minutes), I want to know if it is possible to save model results as some extension that can be fed as it is back to python when running the application, just to save the time of the training happening in every run of the application..
My Answer:-
Pickle or Joblib is used to save a trained model
For your reference, check it out the link given below.
Reference Link
I am using Tensorflow and Inception v3 CNN.
I want to save the trained network using the freeze_graph.py
What I have done till now is saving the model in input_graph.pb during training, by using tf.write_graph, and my thoughts are that this should be the “input graph” in the FLAGS.
When it comes to the remaining fields, such as input saver, output nodes and initializer nodes, I`m not sure how to create the proper file formats?
Another question is how to use the created .meta file from the training?
I would really appreciate if you could take some time to help me.