[英]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.我正在尝试使用通用句子编码 model 来查找句子相似之处。 I have universal sentence encoder model form saved on the local drive.
我在本地驱动器上保存了通用句子编码器 model 表格。 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.
请注意,我的操作系统是 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")
You can use hub.load
to load the Universal Sentence Encoder Model which is Saved to Drive.您可以使用
hub.load
加载保存到驱动器的通用句子编码器 Model。
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:例如,
USE-5
Model 保存在名为5
的文件夹中,其文件夹结构如下图所示,我们可以使用下面提到的代码加载Model
:
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:上述代码的Output为:
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.上面的代码相当于下面显示的代码,它使用 URL 来加载 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.