marwan
marwan

Reputation: 31

How to call the universal sentence encoder model in Python using tensorflow

I am trying to find sentence similarities using the universal sentence encoding model. I have universal sentence encoder model form saved on the local drive. But I don't know how to call it through the code directly from the local drive below instead of calling it through the link in the code. Note that my OS is windows.

import tensorflow as tf
import tensorflow_hub as hub
embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
def get_features(texts):
    if type(texts) is str:
        texts = [texts]
    with tf.Session() as sess:
        sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
        return sess.run(embed(texts))
get_features("The quick brown fox jumps over the lazy dog.I am a sentence for which I would like to get its embedding")

Upvotes: 3

Views: 3392

Answers (1)

user11530462
user11530462

Reputation:

You can use hub.load to load the Universal Sentence Encoder Model which is Saved to Drive.

For example, the USE-5 Model is Saved in the Folder named 5 and its Folder structure is shown in the screenshot below, we can load the Model using the code mentioned below:

enter image description here

import tensorflow_hub as hub

embed = hub.load('5')

embeddings = embed([
    "The quick brown fox jumps over the lazy dog.",
    "I am a sentence for which I would like to get its embedding"])

print(embeddings)

Output of the above code is :

tf.Tensor([[ 0.01305105 0.02235123 -0.03263275 ... -0.00565093 -0.04793027 -0.11492756] [ 0.05833394 -0.08185011 0.06890941 ... -0.00923879 -0.08695354 -0.0141574 ]], shape=(2, 512), dtype=float32)

The above code is equivalent to the code shown below which uses the URL to Load the USE-5.

import tensorflow_hub as hub

embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/5")

embeddings = embed([ 
"The quick brown fox jumps over the lazy dog.", 
"I am a sentence for which I would like to get its embedding"])

print(embeddings)

Upvotes: 4

Related Questions