简体   繁体   English

如何使用tensorflow在Python中调用通用语句编码器model

[英]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.

相关问题 如何使用 TensorFlow 的通用句子编码器将向量转换回句子? - How to convert vector back to Sentence using TensorFlow's Universal Sentence Encoder? 使用 Tensorflow_hub 和 Universal-sentence-encoder 时的困难 - Difficulties while using Tensorflow_hub and universal-sentence-encoder 使用 tensorflow 的多语言通用句子编码器的问题 - Problem using tensorflow's multilingual universal-sentence-encoder 如何为 tensorflow 模块提供服务,特别是通用句子编码器? - How to serve a tensorflow-module, specifically Universal Sentence Encoder? 通用句子编码器中的 Tensorflow session 错误 - Tensorflow session error in universal sentence encoder tensorflow 1 Session.run 使用通用句子编码器嵌入句子的时间太长 - tensorflow 1 Session.run is taking too much time to embed sentence using universal sentence encoder 服务通用句子编码器 Model 使用 tensorflow 服务和 docker - Serving Univeral Sentence Encoder Model using tensorflow serving and docker 在 Keras 中使用通用句子编码器嵌入层 - Using the a Universal Sentence Encoder Embedding Layer in Keras 将 Universal Sentence Encoder 保存到 Tflite 或将其提供给 tensorflow api - Save the Universal Sentence Encoder to Tflite or serve it to tensorflow api 测试tensorflow-hub安装:通用语句编码器 - Testing tensorflow-hub installation: universal sentence encoder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM