Raghu
Raghu

Reputation: 477

How to calculate Cosine similarity and Euclidean distance between two tensors in TF2.0?

I have two tensors (OQ, OA) with shapes as below at the end of last layers in my model.

OQ shape: (1, 600)

OA shape: (1, 600)

These tensors are of type 'tensorflow.python.framework.ops.Tensor'

  1. How can we calculate cosine similarity and Euclidean distance for these tensors in Tensorflow 2.0?
  2. Do we get a tensor again or a single score value between [0,1]? Please help. enter image description here

I tried this but not able to view the score.

score_cosine = tf.losses.cosine_similarity(tf.nn.l2_normalize(OQ, 0), tf.nn.l2_normalize(OA, 0))
print (score_cosine)

Output: Tensor("Neg_1:0", shape=(1,), dtype=float32)

Upvotes: 1

Views: 3440

Answers (1)

user8879803
user8879803

Reputation:

You can calculate Euclidean distance and cosine similarity in tensorflow 2.X as below. The returned output will also be a tensor.

import tensorflow as tf

# It should be tf 2.0 or greater
print("Tensorflow Version:",tf.__version__)

#Create Tensors
x1 = tf.constant([1.0, 112332.0, 89889.0], shape=(1,3))
print("x1 tensor shape:",x1.shape)
y1 = tf.constant([1.0, -2.0, -8.0], shape=(1,3))
print("y1 tensor shape:",y1.shape)

#Cosine Similarity
s = tf.keras.losses.cosine_similarity(x1,y1)
print("Cosine Similarity:",s)

#Normalized Euclidean Distance
s = tf.norm(tf.nn.l2_normalize(x1, 0)-tf.nn.l2_normalize(y1, 0),ord='euclidean')
print("Normalized Euclidean Distance:",s)

#Euclidean Distance
s = tf.norm(x1-y1,ord='euclidean')
print("Euclidean Distance:",s)

The Output of the above code is -

Tensorflow Version: 2.1.0
x1 tensor shape: (1, 3)
y1 tensor shape: (1, 3)
Cosine Similarity: tf.Tensor([0.7897223], shape=(1,), dtype=float32)
Normalized Euclidean Distance: tf.Tensor(2.828427, shape=(), dtype=float32)
Euclidean Distance: tf.Tensor(143876.33, shape=(), dtype=float32)

Upvotes: 4

Related Questions