首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于tensorflow的句子相似度

基于tensorflow的句子相似度
EN

Stack Overflow用户
提问于 2020-09-09 21:58:04
回答 1查看 339关注 0票数 0

我正在尝试确定一个句子和其他句子之间的语义相似性,如下所示:

代码语言:javascript
运行
复制
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import os, sys
from sklearn.metrics.pairwise import cosine_similarity

# get cosine similairty matrix
def cos_sim(input_vectors):
    similarity = cosine_similarity(input_vectors)
    return similarity

# get topN similar sentences
def get_top_similar(sentence, sentence_list, similarity_matrix, topN):
    # find the index of sentence in list
    index = sentence_list.index(sentence)
    # get the corresponding row in similarity matrix
    similarity_row = np.array(similarity_matrix[index, :])
    # get the indices of top similar
    indices = similarity_row.argsort()[-topN:][::-1]
    return [sentence_list[i] for i in indices]


module_url = "https://tfhub.dev/google/universal-sentence-encoder/2" #@param ["https://tfhub.dev/google/universal-sentence-encoder/2", "https://tfhub.dev/google/universal-sentence-encoder-large/3"]

# Import the Universal Sentence Encoder's TF Hub module
embed = hub.Module(module_url)

# Reduce logging output.
tf.logging.set_verbosity(tf.logging.ERROR)

sentences_list = [
    # phone related
    'My phone is slow',
    'My phone is not good',
    'I need to change my phone. It does not work well',
    'How is your phone?',

    # age related
    'What is your age?',
    'How old are you?',
    'I am 10 years old',

    # weather related
    'It is raining today',
    'Would it be sunny tomorrow?',
    'The summers are here.'
]

with tf.Session() as session:

  session.run([tf.global_variables_initializer(), tf.tables_initializer()])
  sentences_embeddings = session.run(embed(sentences_list))

similarity_matrix = cos_sim(np.array(sentences_embeddings))

sentence = "It is raining today"
top_similar = get_top_similar(sentence, sentences_list, similarity_matrix, 3)

# printing the list using loop 
for x in range(len(top_similar)): 
    print(top_similar[x])
#view raw

然而,当我尝试运行这段代码时,我得到了这个错误:

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-61-ea8c65e564c2> in <module>
     24 
     25 # Import the Universal Sentence Encoder's TF Hub module
---> 26 embed = hub.Module(module_url)
     27 
     28 # Reduce logging output.

/anaconda3/lib/python3.7/site-packages/tensorflow_hub/module.py in __init__(self, spec, trainable, name, tags)
    179           name=self._name,
    180           trainable=self._trainable,
--> 181           tags=self._tags)
    182       # pylint: enable=protected-access
    183 

/anaconda3/lib/python3.7/site-packages/tensorflow_hub/native_module.py in _create_impl(self, name, trainable, tags)
    383         trainable=trainable,
    384         checkpoint_path=self._checkpoint_variables_path,
--> 385         name=name)
    386 
    387   def _export(self, path, variables_saver):

/anaconda3/lib/python3.7/site-packages/tensorflow_hub/native_module.py in __init__(self, spec, meta_graph, trainable, checkpoint_path, name)
    442     # TPU training code.
    443     with scope_func():
--> 444       self._init_state(name)
    445 
    446   def _init_state(self, name):

/anaconda3/lib/python3.7/site-packages/tensorflow_hub/native_module.py in _init_state(self, name)
    445 
    446   def _init_state(self, name):
--> 447     variable_tensor_map, self._state_map = self._create_state_graph(name)
    448     self._variable_map = recover_partitioned_variable_map(
    449         get_node_map_from_tensor_map(variable_tensor_map))

/anaconda3/lib/python3.7/site-packages/tensorflow_hub/native_module.py in _create_state_graph(self, name)
    502         meta_graph,
    503         input_map={},
--> 504         import_scope=relative_scope_name)
    505 
    506     # Build a list from the variable name in the module definition to the actual

/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/saver.py in import_meta_graph(meta_graph_or_file, clear_devices, import_scope, **kwargs)
   1460   return _import_meta_graph_with_return_elements(meta_graph_or_file,
   1461                                                  clear_devices, import_scope,
-> 1462                                                  **kwargs)[0]
   1463 
   1464 

/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/saver.py in _import_meta_graph_with_return_elements(meta_graph_or_file, clear_devices, import_scope, return_elements, **kwargs)
   1470   """Import MetaGraph, and return both a saver and returned elements."""
   1471   if context.executing_eagerly():
-> 1472     raise RuntimeError("Exporting/importing meta graphs is not supported when "
   1473                        "eager execution is enabled. No graph exists when eager "
   1474                        "execution is enabled.")

RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. No graph exists when eager execution is enabled.

你知道我怎么才能修好吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-09 22:12:04

问题的原因似乎是TF2不支持集线器模型。

这很简单,但是您有没有尝试禁用tensorflow版本2的行为?

代码语言:javascript
运行
复制
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

此命令将禁用tensorflow 2行为,但仍可能发生一些错误,与导入模块和图形有关。

然后尝试下面的命令。

代码语言:javascript
运行
复制
!pip install --upgrade tensorflow==1.15

import tensorflow as tf
print(tf.__version__)

这会将tensorflow升级到版本1.15,并打印结果。搜索"how to upgrade python modules with pip“获取更多帮助。

无论如何,请检查下面的链接。他们描述了类似的问题。

https://github.com/tensorflow/hub/issues/350

https://github.com/tensorflow/hub/issues/124

Tensorflow Eager Guide

Can a TensorFlow Hub module be used in TensorFlow 2.0?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63813264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档