3

In tensorflow 2 fetches and assign is not any more supported. Accessing batch results in tf 1.x in a custom keras callback is possible following the answer provided in https://stackoverflow.com/a/47081613/9949099 In tf.keras and tf 2.0 under eager execution fetches are not supported, therefore the solution provided for tf 1.x is not working. Is there a way to get the y_true and y_pred inside the on_batch_end callback of a tf.keras custom callback?

I have tried to modify the answer working in tf.1 like below

from tf.keras.callbacks import Callback

class CollectOutputAndTarget(Callback):
    def __init__(self):
        super(CollectOutputAndTarget, self).__init__()
        self.targets = []  # collect y_true batches
        self.outputs = []  # collect y_pred batches

    def on_batch_end(self, batch, logs=None):
        # evaluate the variables and save them into lists
        # How to change the following 2 lines so that in tf.2 eager execution collect the batch results
        self.targets.append(K.eval(self.model._targets[0]))
        self.outputs.append(K.eval(self.model.outputs[0]))

When I run the code above the code fails, accessing data in self.model._targets[0] or self.model.outputs[0] apparently is not possible

Dimitris
  • 31
  • 2
  • Your code above does not run by itself - you can greatly increase the chances of getting help by posting a complete example that should run, but does not. – bers Jan 08 '20 at 12:27
  • When you say "the code fails", what is the error message that you are getting? For me, accessing `self.model.outputs[0]` works fine, but converting them to `numpy` does not. – bers Jan 08 '20 at 12:28
  • I got my own version of this working in by disabling eager execution: `tf.compat.v1.disable_eager_execution()`. So it's not TF2 per se that is failing, but it may simply be a side-effect of eager execution. – bers Jan 08 '20 at 12:29
  • This might answer your question: https://stackoverflow.com/a/59697739/880783 – bers Jan 11 '20 at 19:25
  • Does this answer your question? [Create keras callback to save model predictions and targets for each batch during training](https://stackoverflow.com/questions/47079111/create-keras-callback-to-save-model-predictions-and-targets-for-each-batch-durin) – bers Jan 11 '20 at 19:25

0 Answers0