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.
You have to change the 0
values of the y_true
to -1
. In the link you shared it is mentioned that that if your y_true
is originally {0,1}
that you have to change it to {-1,1}
for the Hinge Loss calculation. Then you will get the same value for the example which is 1.3
.
From the link shared: https://www.tensorflow.org/api_docs/python/tf/keras/losses/Hinge
y_true values are expected to be -1 or 1. If binary (0 or 1) labels are provided we will convert them to -1 or 1.
import tensorflow as tf
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.))
# convert y_true from {0,1} to {-1,1} before passing them to hinge_loss
y_true = y_true * 2 - 1
print(hinge_loss(y_true, y_pred))
Output:
tf.Tensor(1.3, shape=(), dtype=float32)