liveBook · Manning

Compute_loss

As explained in Deep Learning with R, Third Edition

compute_loss is a Keras model method that computes the configured loss together with applicable regularization losses.

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

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains the method signature, scalar return, metric updates, and backend-neutral customization, but does not derive optimization mathematics.
Examples
Uses supervised classification, a custom train_step(), and an MNIST variational autoencoder with reconstruction and KL losses.
What it assumes
Assumes familiarity with model predictions and targets, while demonstrating custom loss design from the compute_loss() method signature.
What you can do after
Apply it via Keras to define custom objectives and train models with built-in or customized training loops.
How it connects
Connects custom train_step(), regularization losses, metrics, autoencoders, variational autoencoders, and self-supervised learning.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.