compute_loss is a Keras model method that calculates the scalar loss used for training. In the standard case, it combines the loss passed to compile() with regularization losses added by applicable layers. section 7.4.5 section 17.1.3
Use compute_loss when training logic needs a customized objective without replacing Keras’s default training loop:
- Call self$compute_loss(y = targets, y_pred = predictions) inside a custom train_step(), then use the result for gradients and loss tracking. section 7.4.5
- Override compute_loss() when the objective depends on model inputs, such as reconstruction loss in an autoencoder or variational autoencoder. section 17.1.3
- Return one scalar loss value; compute_loss() can also update custom metric state while preserving backend-agnostic training logic. section 17.1.3
A VAE reconstruction objective
A variational autoencoder receives images without separate targets. Its compute_loss() method treats x as the original image, reads latent parameters from y_pred, samples a latent vector, decodes it, and combines reconstruction and KL losses. The model compiles with only an Adam optimizer and trains by passing images to fit(). section 17.1.3
