3

I currently have a multi output model

model=Model(inputs=x1, outputs=[y1,y2])
model.compile((optimizer='sgd', loss=[cutom_loss,'mse'])

What is the y_pred and y_true values here for mse loss function? What is the y_true for mse; is it output of y2 alone or its both y1 and y2?

In my custom_loss I need to pass y_true and y_pred from both the outputs sepeartaly for calculation

 def custom_loss(y1_true, y1_pred,y2_true, y2_pred):

How can I do this?

Eka
  • 14,170
  • 38
  • 128
  • 212

1 Answers1

3

Unfortunately you cannot define a 'global' loss function. A loss function is always computed only on one output (see the pseudo-code in the accepted answer).

In your example the custom loss will be computed on y1_true and y1_pred, while the mse will be computed on y2_true and y2_pred.

If you want a custom loss that includes both y1 and y2 outputs, I can think of two ways:

  1. Collapse multiple outputs in one output: if y1 and y2 are similar vectors, you could concatenate them in order to have only one output. Then in your custom loss you apply some indexing/slicing in order to separate the two outputs.
  2. Make the loss an output of your model: create a custom network graph (using keras functional API and the backend) that computes the loss, by taking y1_true and y2_true as an input to the network; by doing that, your final model will have 3 outputs; y1_pred, y2_pred and the loss. After the training you can discard the part of the model your are not interested anymore (y_true inputs and loss output).

I remember that I had a similar problem in the past and I chose to implement option 2, but it was kind of a pain.

rickyalbert
  • 2,552
  • 4
  • 21
  • 31