'Tensorflow 2.X : Understanding hinge loss

I am learning Tensorflow 2.X. I am following this page to understand hinge loss.

I went through the standalone usage code.

Code is below -

 y_true = [[0., 1.], [0., 0.]]
 y_pred = [[0.6, 0.4], [0.4, 0.6]]
 h = tf.keras.losses.Hinge()
 h(y_true, y_pred).numpy()

the output is 1.3

I tried to manually calculate it & writing code by given formula

loss = maximum(1 - y_true * y_pred, 0)

my code -

y_true = tf.Variable([[0., 1.], [0., 0.]])
y_pred = tf.Variable([[0.6, 0.4], [0.4, 0.6]])
def hinge_loss(y_true, y_pred):
  return tf.reduce_mean(tf.math.maximum(1. - y_true * y_pred, 0.))

print("Hinge Loss :: ", hinge_loss(y_true, y_pred).numpy())

But I am getting 0.9.

Where am i doing wrong ? Am i missing any concept here ?

Kindly guide.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source