Some layers have regularisation losses. I would like to have a "metric" that only keeps track of the regularisation losses, so that I can separately (from the total loss) see, in the progress bar, how the regularisation losses are evolving during training. Furthermore, given that metrics are saved in the history object (returned by fit
), I could easily plot and save (to a file) the regularisation losses at the end of the training.
How can I write a custom metric only for the regularisation losses?
I know I can have a callback that iterates the layers of a model and sums the regularisation losses (see e.g. https://stackoverflow.com/a/48489090/3924118), but I would like to have a metric (rather than a callback) because the metric will be saved in the history object.
Ultimately, I would like the regularisation losses to be present in the history object and displayed in the progress bar (separately from the total loss), even without implementing a custom metric, but I don't know if there's such an option in tf.keras
. Also, it would be nice if I could have two options: 1. see the sum of all regularisation losses across all layers and 2. see the regularisation losses for each layer separately.