TensorFlow | cosine_similarity for vectors

Cosine similarity measures the similarity between vectors by calculating the cosine angle between the two vectors.

TensorFlow provides tf.keras.losses.cosine_similarity function to compute cosine similarity between labels and predictions.

Cosine similarity is a number number between -1 and 1. Cosine values closer to -1 indicate greater similarity and values closer to 1 indicate greater dissimilarity.

Let's understand tf.keras.losses.cosine_similarity with below examples

Calculate Cosine similarity between two vectors

 
import tensorflow as tf

y_true = [[1., 1.], [1., 1.]]
y_pred = [[1., 1.], [-1., -1.]]

consine_sim_tensor = tf.keras.losses.cosine_similarity(y_true, y_pred, axis=1)
print(consine_sim_tensor.numpy())
    
     

Output

   
[-0.99999994  0.99999994]
 

From the above output we can see as the first element in both y_true and y_pred is same hence cosine similarity is approaching to -1, while second element is opposite in y_true and y_pred hence cosine similarity value is approaching to 1.

Calculate Cosine similarity of one vector with list of vectors

   
import tensorflow as tf

tensor_1 = [.34, .45, .67, .65] 
tensor_list = [[.34, .45, .67, .65], [.14, .35, .67, .65],
            [.54, .95, .07, .5], [.64, .75, .81, .05], [.34, .45, .67, .65]]

cosine_sim_values = tf.keras.losses.cosine_similarity(
    tensor_1,
    tensor_list,
    axis=-1)

print(cosine_sim_values.numpy())
 

Output

   
[-1.         -0.9804376  -0.74875045 -0.8115672  -1.        ]
 

In the above output first and fifth values are -1 because tensor_1 and first and fifth element of tensor_list are identical.


Follow US on Twitter:

Similar Articles