Search code examples
tensorflowkerasmetricstensorflow2.0keras-2

How does Keras 2 aggregate the results of custom metrics?


The Keras documentation gives, at the very end, an example of a function that gets y_true and y_pred and returns a value, once per batch, and that value gets shown during training.

If I try to implement the Keras Metric class, and look at the other metrics, they usually use assign_add(), and the result is calculated after each batch, but using the variables from the entire epoch, and they get reset at the end of an epoch.

Is my understanding correct that if I simply write a custom keras metric as a function, I'll get batch-wise results, and the final result I see at the end of an epoch is merely the result on the last batch, while by implementing Metric I'll get epoch-wise results, and the last result I see at the end of an epoch is over the entire epoch?


Solution

  • Found it with a debugger - the values get passed to MeanMetricWrapper, which in turn calculates the mean value of all the results of the custom stateless metric, which makes sense.