value of loss from add_loss

818 views
Skip to first unread message

Kyle McDonald

unread,
Jul 26, 2018, 9:37:39 PM7/26/18
to Keras-users
How can I get the value of a loss function that I added with add_loss after calling train_on_batch?

As a simple example, I have an autoencoder like this:

inputs = Input(input_shape)
encoded
= encoder(inputs)
decoded
= decoder(encoded)
model
= Model(inputs=inputs, outputs=decoded)
loss
= MSE(inputs, decoded)
model
.add_loss(loss)
model
.compile('adam')
for batch in batches:
    model
.train_on_batch(batch)

And after I call train_on_batch I would like to know what the value is for loss.

Some ideas I considered:

- Return the loss as an output: outputs=[decoded, loss]. This doesn't work because loss is not a layer.
- Make another model with inputs=inputs and outputs=loss. Doesn't work for the same reason as above.
- Add metrics=[loss] to compile(). If I try to retrieve model.losses[0] it's just the original Tensor, not a value.

When I run fit() I noticed it's logging something for "loss", but as far as I can tell it's just the mean value of the output.

If there is a way to do this with fit() and callbacks, I'm also open to that.

Daπid

unread,
Jul 27, 2018, 2:54:47 AM7/27/18
to Kyle McDonald, Keras-users
You can easily create a custom callback. For example, TerminateOnNaN checks if the loss is NaN:


/David

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/e24a4c82-4ba6-415e-abdd-54a3e0b31b3b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kyle McDonald

unread,
Jul 28, 2018, 2:52:29 AM7/28/18
to Keras-users
Hi David, it turns out that Keras does not return the loss of add_loss() inside the callback. It only returns the output of the model.

If there is another way to do this with callbacks, I would appreciate hearing about it.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.

kortha...@gmail.com

unread,
Jan 9, 2019, 6:57:44 AM1/9/19
to Keras-users
Maybe my answer on SO helps you: https://stackoverflow.com/questions/54069363/output-multiple-losses-added-by-add-loss-in-keras/54109533#54109533
I've written custom loss layers for the reconstruction- and KL-loss in a VAE and added the calculated loss to the output of these layers.
Furthermore, a custom callback in the fit call does the logging.
Reply all
Reply to author
Forward
0 new messages