I am trying use an embeddings module from tensorflow hub as servable. I am new to tensorflow. Currently, I am using Universal Sentence Encoder embeddings as a lookup to convert sentences to embeddings and then using those embeddings to find a similarity to another sentence.
My current code to convert sentences into embeddings is:
with tf.Session() as session:
session.run([tf.global_variables_initializer(), tf.tables_initializer()])
sen_embeddings = session.run(self.embed(prepared_text))
Prepared_text is a list of sentences. How do I take this model and make it a servable?
Currently, the hub modules cannot be consumed by Tensorflow Serving directly. You will have to load the module into an empty graph and then export it using the
SavedModelBuilder
. For example:This will export your model (to the folder
/tmp/serving_saved_model
) in the desired format for serving. After this, you can follow the instructions given in the documentation here: https://www.tensorflow.org/serving/serving_basicRight now you probably need to do this by hand. Here is my solution, similar to previous answer but more general - show how to use any other module without guessing input parameters, as well as extended with verification and usage:
Thanks to
module.get_input_info_dict()
you know what tensor names you need to pass to the model - you use this name as a key forinputs={}
insimple_save
method.Remember that to serve the model it needs to be in directory path ending with version, that's why
'00000001'
is the last path in whichsaved_model.pb
resides.After exporting your module, quickest way to see if your model is exported properly for serving is to use saved_model_cli API:
To serve the model from docker: