question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recieve list of all outputs as input to a custom loss function.

See original GitHub issue

Important note: the development of Keras is currently taking place at github.com/tensorflow/tensorflow. Please file your bug report by creating a new issue in the TensorFlow repository.

Please note that multi-backend Keras development has been discontinued. Do not report issues about multi-backend Keras (Keras 2.3.1 and lower), only report issues about the TensorFlow implementation of Keras (tf.keras).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
manaspalaparthicommented, Jan 20, 2021

I have the same problem too. my model outputs 3 tensors. model= Model(inputs = inputs,outputs =[predicted_image,depth,x_train])

my loss function should be

` def depth_loss_function(y_true, y_pred):

print(y_pred)

predict_image, depth , input_image = y_pred

# Point-wise depth
depth_loss = K.mean(K.abs(depth - y_true), axis=-1)

# image reconstruction
mse = tf.keras.losses.MeanSquaredError()
image_loss = mse(input_image - predict_image)

# Weights
w1 = 1.0
w2 = 1.0

return  (w2 * depth_loss) + (w2 * image_loss )`

compile method

model.compile(optimizer= optimizer, loss =depth_loss_function)

error 1 : y_pred is only first tensor. error 2 : by using predict_image, depth , input_image = y_pred Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn.

1reaction
vladhondru25commented, Oct 26, 2021

Yes, I created a custom keras layer (as last layer in my function), and inside it I used self.add_loss function (you can pass any function with any number of parameters in add_loss).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access all outputs from a single custom loss function in ...
FINALLY, THE FIX WAS to concatenate the vectors together (along axis=1 this case) and on the loss function, take them apart:
Read more >
Custom loss function with multiple outputs in tensorflow
my network has two outputs and single input. I am trying to write a custom ...
Read more >
Advanced Keras - Custom loss functions - Petamind
A machine learning model may need custom loss function. You can use a different loss on each output by passing a dictionary or...
Read more >
How To Build Custom Loss Functions In Keras For Any Use ...
In deep learning, the loss is computed to get the gradients for the model weights and update those weights accordingly using backpropagation. What...
Read more >
Losses - Keras
All built-in loss functions may also be passed via their string ... pass optimizer by name: default parameters will be used ... Creating...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found