<> preface

I believe many people are doing machine learning , Deep learning is good , The main thing is how to train a good model , Of course it should , I also focus on how to train the model .

But recently, after finishing the training of a deep learning model , When deployed Online , But I found that I didn't think about it .

One aspect is how to apply your model to the Internet quickly ? How to make the code run more efficiently ?
Because usually the training is to store the model directly in the local , Just read it into memory when you need it .

But if in actual production " In the environment , How can I read a file every time I call the model ? This is too inefficient , And at the time of deployment, you need to copy the model to each server , It is more inconvenient to maintain and update the model .

<> One , middleware Redis

I believe many people know Redis This artifact .
This example also uses Redis As middleware , Serialize the model and store it in Redis in .
use Redis The benefits are :
Just store the model in one machine , Read it once when using Redis Medium .
adopt Redis Read model greatly speeds up reading from local disk , Accelerate online business process .
When updating a model, you only need to update the model on one machine .

<> Two , Save serialization of general machine learning model

The example used here is the model trained when standardizing data scaler
The common way to save on a local disk is to :
With the help of joblib package
from sklearn.externals import joblib ## Save to test catalog joblib.dump(scaler,
'/home/test/scaler_test') ## read scaler scaler_test = joblib.load(
To store in Redis in , The model needs to be serialized first , Direct storage can only be stored as a bunch of strings
With the help of cPickle
## Serializing models scaler_cp = cPickle.dumps(scaler) ## Store to redis in self.redis_dumps(
store in Redis The data in is as follows

## Load model scaler_loads = self.redis_loads("scaler") ## Deserialization scaler_cp = cPickle.
So the model is fully loaded .

<> Three ,tensorflow Model preservation after training

1, The easiest and most convenient way is to store it locally , as follows
## preservation model.save('/home/tensorflow/tensor_test.h5') ## load from tensorflow.keras
.models import load_model model = load_model('/home/tensorflow/tensor_test.h5')
2, If you want to store it in Redis in , The model needs to be stored in two parts , Structural part , Weight part .
Storage of structural parts :
model_json = model.to_json() ## Store to redis in self.redis_dumps("model",model_json)

Storage of weight part :
weights_list = model.get_weights() ## Serializing models weights = cPickle.dumps(
weights_list) ## Store to redis in self.redis_dumps("weights",weights)

Model loading :
from tensorflow.keras.models import model_from_json model = model_from_json(
self.redis_loads("model")) weight = cPickle.loads(self.redis_loads("weights"))
Now the model is loaded .
Tested exactly like the model saved on disk .

<> Four , summary

There is a big difference between the local experimental training model and the real online training model , It's not just about the accuracy of the model , Also consider the impact on all aspects of the online .

I am a moving ant , Hope to move forward together .

If it helps you a little , One like is enough , thank !

notes : If there are any mistakes and suggestions in this blog , Welcome to point out , esteem it a favor !!!

©2019-2020 Toolsou All rights reserved,
First knowledge python Skills summary GDOI2019 travels c Linguistic 5 Three common sorting methods Python Basic knowledge and notes " Cephalosporin wine Say go and go "? DANGER ! Don't drink alcohol when taking these drugs Thorough explanation from Zhongtai vue The component page height is adaptive according to the screen size Classical algorithm - recursion ( The case of raw rabbit ) Big data tells you , How tired are Chinese women about keras use fit_generator Encountered in StopIteration