Is there any way to implement a loss function that is shared between outputs? I have a 2D image output and scalar classification that are both used by a single loss function.

I have attempted writing a function that returns a function, as in this comment, but I would need the input to the function to be the current training example. I also thought of using a merge layer, but that wouldn't work due to an incompatibility of the layer dimensions.

Does anyone know of a way to write such a loss function in keras?

  • $\begingroup$ Definitely possible in lower-level libraries - e.g. TensorFlow. So it will depend if Keras has a way to do it. For clarification, why do you need the full training example, usually the loss function takes prediction and ground truth (your problem here being how to represent that)? $\endgroup$ Feb 23, 2017 at 7:42
  • $\begingroup$ "incompatibility of the layer dimensions when using merge" - couldn't you use "Flatten" and then "Merge" with mode=concat? $\endgroup$
    – stmax
    Feb 23, 2017 at 11:32
  • $\begingroup$ @NeilSlater I don't actually need the inputs for the training example, but I do need the ground truth and predictions for multiple branches. From what I see in keras, each branch has a different objective function. I was hoping to be able to do what I wanted easily, but it looks like I may have to implement an optimizer or make a lambda layer that captures everything. $\endgroup$ Feb 23, 2017 at 16:25
  • $\begingroup$ @stmax I hadn't thought of that, but it does seem like a possibility. I found an approach using a Lambda layer in one of the examples that looks like it may work. $\endgroup$ Feb 23, 2017 at 16:26
  • $\begingroup$ @danielunderwood could you please post how you solve this problem? Would love to see some code. $\endgroup$ Nov 16, 2018 at 4:15


Your Answer

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

Browse other questions tagged or ask your own question.