I am trying to understand how to build a custom loss function and the first thing I've tried is to reimplement the binary_crossentropy function in keras. In my code if I do:
model.compile(Adam(lr=learning_rate), loss=losses.binary_crossentropy, metrics=['accuracy'])
the model compiles ok and trains quickly reaching an accuracy of over 95% in the first epoch and a loss of 0.2
When I create a custom loss function that basically replicates losses.binary_crossentropy:
def custom_loss(y_true,y_pred):
return K.mean(K.binary_crossentropy(y_pred, y_true), axis=-1)
and then:
model.compile(Adam(lr=learning_rate), loss=custom_loss, metrics=['accuracy'])
when I fit the loss is quite high (0.65) and accuracy low (0.47). The fitting procedure and data are the same on both cases so it seems that I am not correctly declaring my loss function. I am using latest versions of keras with tensorflow backend and my model is a simple vgg16 full convolutional model (fcn 32).