Create tf.keras callback to save model predictions and targets for each batch during training in tf 2.0

笑着哭i 提交于 2020-01-15 06:18:09

问题


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

来源:https://stackoverflow.com/questions/58229537/create-tf-keras-callback-to-save-model-predictions-and-targets-for-each-batch-du

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!