Search code examples
pythontensorflowcosine-similaritysentence-similarity

Sentences similarity using tensorflow


I am trying to determine semantic similarity between one sentence and others as follows:

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

However, when I try to run this code, I get this error:

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

Do you know how I can fix it?


Solution

  • The reason of the problem seems to be that TF2 does not support hub Models.

    It's simple, but have you tried to disable tensorflow version 2 behaivour?

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

    This command will disable tensorflow 2 behavior, but still some errors may occur, connected with importing modules and graphs.

    Then try commands below.

    !pip install --upgrade tensorflow==1.15
    
    import tensorflow as tf
    print(tf.__version__)
    
    

    This will upgrade your tensorflow to version 1.15, and print the result. Search for "how to upgrade python modules with pip" for more help.

    Anyways, check following links. They describe similar problems.

    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?