Custom loss function's results does not match with the inbuilt loss function's result
By : user2888981
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have implemented a custom binary cross entropy loss function in tensorflow. To test this I had compared it with the inbuilt binary cross entropy loss function in Tensorflow. But, I got very different results in both cases. I am unable to understand this behaviour. , You have a slight error in your implementation. You have: code :
ans = 1*(w1*y_true*tf.log(y_pred+eps) + w2*(1y_true)*tf.log(y_pred + eps)) ans = 1*(w1*y_true*tf.log(y_pred+eps) + w2*(1y_true)*tf.log(1  y_pred + eps)) def custom_loss(eps,w1,w2):
def loss(y_true, y_pred):
ans = 1*(w1*y_true*tf.log(y_pred+eps) + w2*(1y_true)*tf.log(1y_pred+eps))
return tf.reduce_mean(ans)
return loss
y_true = tf.constant([0.1, 0.2])
y_pred = tf.constant([0.11, 0.19])
custom_loss(y_true, y_pred) # == 0.41316
tf.keras.losses.binary_crossentropy(y_true, y_pred) # == 0.41317

keras sparse_categorical_crossentropy loss function output shape didn't match
By : Thato Sello
Date : March 29 2020, 07:55 AM
hope this fix your issue loss='sparse_categorical_crossentropy' is not meant for onehot encodings but for integer targets. You probably need a "Dense(..." as the output layer and use y_train directly.

keras "unknown loss function" error after defining custom loss function
By : J. Ouellet
Date : March 29 2020, 07:55 AM
Hope that helps In Keras we have to pass the custom functions in the load_model function:

Keras Custom Binary Cross Entropy Loss Function. Get NaN as output for loss
By : Oscar Lastera Sanche
Date : March 29 2020, 07:55 AM
I hope this helps . A naive implementation of Binary Cross Entropy will suffer numerical problem on 0 output or larger than one output, eg log(0) > NaN. The formula you posted is reformulated to ensure stability and avoid underflow. The following deduction is from tf.nn.sigmoid_cross_entropy_with_logits. code :
z * log(sigmoid(x)) + (1  z) * log(1  sigmoid(x))
= z * log(1 / (1 + exp(x))) + (1  z) * log(exp(x) / (1 + exp(x)))
= z * log(1 + exp(x)) + (1  z) * (log(exp(x)) + log(1 + exp(x)))
= z * log(1 + exp(x)) + (1  z) * (x + log(1 + exp(x))
= (1  z) * x + log(1 + exp(x))
= x  x * z + log(1 + exp(x))
x  x * z + log(1 + exp(x))
= log(exp(x))  x * z + log(1 + exp(x))
=  x * z + log(1 + exp(x))
max(x, 0)  x * z + log(1 + exp(abs(x)))

Loading model with custom loss function: ValueError: 'Unknown loss function' in keras
By : Ash
Date : March 29 2020, 07:55 AM
this will help Compiling the model then saving. Then while loading model that time getting error. , Use custom_objects when loading your model: code :
def def triplet_loss(y_true, y_pred, alpha = 0.3):
anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=1)
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=1)
basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0))
return loss
FRmodel = load_model('model.h5',custom_objects={'triplet_loss':triplet_loss})

