Type CosineSimilarity
Namespace tensorflow.keras.losses
Parent LossFunctionWrapper
Interfaces ICosineSimilarity
Computes the cosine similarity between `y_true` and `y_pred`. Usage:
Usage with the `compile` API:
Show Example
cosine_loss = tf.keras.losses.CosineSimilarity(axis=1)
loss = cosine_loss([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]])
# l2_norm(y_true) = [[0., 1.], [1./1.414], 1./1.414]]]
# l2_norm(y_pred) = [[1., 0.], [1./1.414], 1./1.414]]]
# l2_norm(y_true). l2_norm(y_pred) = [[0., 0.], [0.5, 0.5]]
# loss = mean(sum(l2_norm(y_true). l2_norm(y_pred), axis=1))
= ((0. + 0.) + (0.5 + 0.5)) / 2 print('Loss: ', loss.numpy()) # Loss: 0.5