我正在尝试使用通用句子编码模型来查找句子相似性。我将通用句子编码器模型表格保存在本地驱动器上。但是我不知道如何直接从下面的本地驱动器通过代码调用它,而不是通过代码中的链接调用它。请注意,我的操作系统是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")
答案 0 :(得分:1)
您可以使用hub.load
加载保存到驱动器的通用句子编码器模型。
例如,USE-5
模型保存在名为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)
以上代码的输出为:
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)
上面的代码与下面显示的代码等效,该代码使用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)