From d1676e8496a1656cefb43d002506ac977102ed9c Mon Sep 17 00:00:00 2001 From: Samarjeet Kaur Date: Thu, 28 May 2020 22:50:14 +0530 Subject: [PATCH 1/2] Wrapper for tensorflow 2.2.0 --- tensorflow_wrapper.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tensorflow_wrapper.py diff --git a/tensorflow_wrapper.py b/tensorflow_wrapper.py new file mode 100644 index 0000000..87185f4 --- /dev/null +++ b/tensorflow_wrapper.py @@ -0,0 +1,55 @@ +import pandas +import tensorflow + +import tensorflow_hub + + +class TensorFlowWrapper: + """ + Wrapper object for TensorFlow graph and helps use it. + """ + + def __init__(self, embedding_layer_hub_name: str) -> None: + g = tensorflow.Graph() + with g.as_default(): + # Import the Universal Sentence Encoder's TF Hub module + embedding_layer = tensorflow_hub.Module(embedding_layer_hub_name) + + self._sts_input1 = tensorflow.compat.v1.placeholder(tensorflow.string, shape=None) + self._sts_input2 = tensorflow.compat.v1.placeholder(tensorflow.string, shape=None) + + # For evaluation we use exactly normalized rather than approximately normalized. + sts_encode1 = tensorflow.math.l2_normalize(embedding_layer(self._sts_input1), axis=1) + sts_encode2 = tensorflow.math.l2_normalize(embedding_layer(self._sts_input2), axis=1) + cosine_similarities = tensorflow.math.reduce_sum(tensorflow.multiply(sts_encode1, sts_encode2), + axis=1) + clip_cosine_similarities = tensorflow.clip_by_value(cosine_similarities, -1.0, 1.0) + self._sim_scores = 1.0 - tensorflow.math.acos(clip_cosine_similarities) + init_op = tensorflow.group([tensorflow.compat.v1.global_variables_initializer(), tensorflow.compat.v1.tables_initializer()]) + g.finalize() + + self._session = tensorflow.compat.v1.Session(graph=g) + self._session.run(init_op) + + def append_scores(self, sentence_pairs: pandas.DataFrame) -> None: + """ + Appending scoring of cosine similarity based on the given embedding layer. + + :param sentence_pairs: DataFrame matrix of paired sentences with the columns ["sent_1", "sent_2"] where each row + is a paired sentences. + :return: None; it append to given DataFrame new column "score" with the cosine similarity score for each pair in + each row. + """ + + text_a = sentence_pairs["sent_1"].fillna("").tolist() + text_b = sentence_pairs["sent_2"].fillna("").tolist() + + scores = self._session.run(self._sim_scores, feed_dict={self._sts_input1: text_a, self._sts_input2: text_b}) + + sentence_pairs["score"] = scores + + def close(self): + """ + closes the TensorFlow session. + """ + self._session.close() \ No newline at end of file From b6113b4c84c64f364295a867a44b0f4be0c5af10 Mon Sep 17 00:00:00 2001 From: Samarjeet Kaur Date: Thu, 28 May 2020 22:51:00 +0530 Subject: [PATCH 2/2] Rename tensorflow_wrapper.py to wrappers/tensorflow2.2.0_wrapper.py --- tensorflow_wrapper.py => wrappers/tensorflow2.2.0_wrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tensorflow_wrapper.py => wrappers/tensorflow2.2.0_wrapper.py (98%) diff --git a/tensorflow_wrapper.py b/wrappers/tensorflow2.2.0_wrapper.py similarity index 98% rename from tensorflow_wrapper.py rename to wrappers/tensorflow2.2.0_wrapper.py index 87185f4..78afb01 100644 --- a/tensorflow_wrapper.py +++ b/wrappers/tensorflow2.2.0_wrapper.py @@ -52,4 +52,4 @@ def close(self): """ closes the TensorFlow session. """ - self._session.close() \ No newline at end of file + self._session.close()