0

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).

Angel Lopez
  • 29
  • 1
  • 4
  • Do you relearn everything? Show how you get the benchmark and how you get that low accuracy please (enough code to reproduce your result) – Nassim Ben May 31 '17 at 22:11
  • Possible duplicate of [How to write a custom loss function in Tensorflow?](https://stackoverflow.com/questions/34875944/how-to-write-a-custom-loss-function-in-tensorflow) – Salvador Dali Jun 01 '17 at 04:14
  • it seems to be a problem when predictions are a matrix and not a vector, I tried to make an example with a simple classification model where y_pred y a vector and it works ok. I am trying now to make a reproducible example with the matrix predictions. Yes, @NassimBen I relearn everything – Angel Lopez Jun 01 '17 at 09:01

0 Answers0