微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用张量流的句子相似度

如何解决使用张量流的句子相似度

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

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,3)

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

但是,当我尝试运行此代码时,出现此错误

---------------------------------------------------------------------------
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,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,Meta_graph,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,-> 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,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.

您知道我该如何解决吗?

解决方法

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

这很简单,但是您是否尝试过禁用tensorflow版本2 behaivour?

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

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

然后尝试以下命令。

!pip install --upgrade tensorflow==1.15

import tensorflow as tf
print(tf.__version__)

这会将您的tensorflow升级到1.15版,并打印结果。 搜索“如何使用pip升级python模块”以获取更多帮助。

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

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?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。