I have a (time-expensive) operation that multiple metrics have in common. What would be the best way to share the operation result between the metrics avoiding the overhead of recalculating it each time?
Asked
Active
Viewed 73 times
1 Answers
2
You should create a special class to override tf.keras.callback.Callback()
(thus implement your own callback class) and calculate the metrics that you need by overriding the method on_epoch_end()
.
Then, you can calculate some of your metrics, say, on the validation set, and you thus manually ensure that if you calculate for example TP + FP
, you do use this sum to calculate the precision (TP / (TP + FP)
) rather than recalculating it.
Manually doing so ensures no additional/superfluous computations are made.

Timbus Calin
- 13,809
- 5
- 41
- 59
-
Thanks for your quick reply Timbus. Would I have to pass the validation set to the callback to be able to get the metrics or do the callbacks have access to the provided dataset to .fit()? – Ramon May 29 '20 at 14:57
-
Yes Ramon. Have a look at my response here: https://stackoverflow.com/questions/60616842/how-to-get-other-metrics-in-tensorflow-2-0-not-only-accuracy/60800425#60800425 . If it solves your problem, please accept my answer as solving one (pressing the green tick below my answer). – Timbus Calin Jun 02 '20 at 09:11