1
votes

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)

1
What if you applied .numpy() on your result? - Timbus Calin
This is not a eager tensor, so .numpy does not work here. ----> 1 score_cosine.numpy() AttributeError: 'Tensor' object has no attribute 'numpy' - Raghu
You need to run this in a session if you're using TF 1.x. Or upgrade to TF 2.x where eager mode is the default so you will get the results straight away. - xdurch0
Please note, I am in TF 2.x already and tensor is not eager but it is ops.tensor. - Raghu

1 Answers

4
votes

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)