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.