I want to implement a deep learning model in Keras, but I want to use my own loss function, i.e. custom loss. If I implement some loss function and use Keras Functional API for the model, do I need to change the way the optimizer works, because the optimizer will minimize my loss function? If I need to do that, what is the way to do that?


2 Answers 2


You don't need to change the way optimizer works. You just need to define your loss function in some standard way.

Checkout the answer in this post to have an idea on an example of customizing loss function in Keras.



Write your custom loss function in Tensorflow or Keras backend, keeping in mind that the function takes two inputs of y_pred and y_true and then feed the function into the model.compile command in the loss section. If your function was:

def my_loss (y_true,y_pred):

then in the model.compile you would have loss=[my_loss].


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.