Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wrapper for tensorflow 2.2.0 #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions wrappers/tensorflow2.2.0_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pandas
import tensorflow

import tensorflow_hub


class TensorFlowWrapper:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the original tensorflow_wrapper.py

"""
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tf.disable_v2_behavior() if you'd like to use tf1 behavior.
I suggest moving to tensorflow v2 behavior. Replace compat.v1.placeholder with their match in tf2 such as:
placeholder become Variable or keras.layers.Model.

See full guide here: https://www.tensorflow.org/guide/migrate

self._sts_input2 = tensorflow.compat.v1.placeholder(tensorflow.string, shape=None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tf.disable_v2_behavior() if you'd like to use tf1 behavior or migrate to tensorflow v2.

See previous remark.


# 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()])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tf.disable_v2_behavior() if you'd like to use tf1 behavior or migrate to tensorflow v2.

See previous remark.

g.finalize()

self._session = tensorflow.compat.v1.Session(graph=g)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tf.disable_v2_behavior() if you'd like to use tf1 behavior or migrate to tensorflow v2.

See previous remark.

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()