Keras CosineSimilarity - Positive or Negative

Viewed 2894

I'm training a model, my loss function is cosine similarity:

model.compile(optimizer='adam', 
              loss=tf.keras.losses.cosine_similarity, 
              metrics=[tf.keras.metrics.CosineSimilarity(axis=1)])

In Keras, "cosine_similarity" loss should converge to -1?

On the other hand, "CosineSimilarity" metric should go towards 1 for better accuracy, right?

Thanks in advance!

1 Answers

As this is implemented as a loss and we minimize the loss during training the sign is inverted, so that when you minimize the loss (towards -1), the actual similarity is maximized towards 1. I also think that the documentation in the Stable release is a bit confusing, see the Nightly documentation with this example:

y_true = [[0., 1.], [1., 1.], [1., 1.]]
y_pred = [[1., 0.], [1., 1.], [-1., -1.]]
loss = tf.keras.losses.cosine_similarity(y_true, y_pred, axis=1)
loss.numpy()
array([-0., -0.999, 0.999], dtype=float32)

For some reason the metric is also implemented with a - (minus).

Related