1

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)

Raghu
  • 407
  • 5
  • 12
  • What if you applied .numpy() on your result? – Timbus Calin Mar 06 '20 at 08:02
  • This is not a eager tensor, so .numpy does not work here. ----> 1 score_cosine.numpy() AttributeError: 'Tensor' object has no attribute 'numpy' – Raghu Mar 06 '20 at 08:58
  • 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 Mar 06 '20 at 09:12
  • Please note, I am in TF 2.x already and tensor is not eager but it is ops.tensor. – Raghu Mar 06 '20 at 11:04

1 Answers1

4

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)
  • Thanks @Rohit for your answer. I will try with this to calculate cosine similarity and Euclidean distance but as you see in my question, the tensors are not eager but ops.Tensor so not able to print the values directly. Do you have any solution for this? – Raghu Mar 06 '20 at 11:33
  • @Raghu what version of tensorflow you are using? Is it possible to share the code? It might help in recreating the issue. Also I hope the operations mentioned in above answer might as well work with your version. –  Mar 06 '20 at 12:03
  • @ Rohit - I am using TF version 2.0.0. Please take a look here. This is for the issue of printing the values of ops tensor. I posted the minimal reproducible code. If we can print the values of that tensor, I can apply the same post the above calculations. Thanks ! https://stackoverflow.com/questions/60338842/how-to-print-value-of-tensorflow-python-framework-ops-tensor-in-tensorflow-2-0 – Raghu Mar 06 '20 at 12:13
  • @Raghu The reproducible code is generating a symbolic tensor. symbolic tensor differs from other tensors in that they do not specifically hold values. No values are explicitly being defined in the network. Rather, a framework is created for the input variables to be read by the network, and then generate predictions. –  Mar 06 '20 at 12:33
  • @Raghu If I have answered your question, can you please accept it and up vote it. Also in the other link https://stackoverflow.com/questions/60338842/how-to-print-value-of-tensorflow-python-framework-ops-tensor-in-tensorflow-2-0/60564318#60564318 I have answered your question. If I have answered your question, can you please accept it and up vote it. Thank You. –  Mar 06 '20 at 13:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209203/discussion-between-raghu-and-rohit). – Raghu Mar 07 '20 at 10:18