1
$\begingroup$

I am using universal sentence encoder from tensorflow hub to encode sentence into embedding.

import tensorflow_hub as hub
import tensorflow as tf

module_url = "https://tfhub.dev/google/universal-sentence-encoder/1?tf-hub-format=compressed"

def get_emb(text):
    embed = hub.Module(module_url)
    similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
    similarity_message_encodings = embed(similarity_input_placeholder)
    with tf.Session() as session:
            session.run(tf.global_variables_initializer())
            session.run(tf.tables_initializer())
            message_embeddings_ = session.run(similarity_message_encodings, feed_dict={similarity_input_placeholder: text})
    tf.reset_default_graph()
    return message_embeddings_

Now the problem is each time I call the function the time is taking is about 10 seconds which is very high. Is there a way to save the model or something and do it instantly. This will be used in restful service where the response should be near instant

$\endgroup$
0
$\begingroup$

I would recommend using the model like this:

import tensorflow_hub as hub
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
model = hub.load(module_url)
print ("module %s loaded" % module_url)
def embed(input):
    return model(input)

And then:

embed(["This is a nice string!"])

Which would return:

<tf.Tensor: shape=(1, 512), dtype=float32, numpy= array([[ 8.84598400e-03, ... , 1.89793427e-02]], dtype=float32)>

That is the 512 dimension vector, but you can choose the one you need between all the encoders here: https://tfhub.dev/

Hope it helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.