如何在on_epoch_end中获取真实的y_true和y_pred?

时间:2019-11-20 20:17:17

标签: python python-3.x tensorflow machine-learning keras

我正在尝试在每个时期的末尾获取y_true和y_pred。我使用fit_generator训练我的模型。我试图在每个时期后找到特异性。

class LossHistory(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
      y_true = ?
      y_pred = ?

loss_history = LossHistory

fit_generator(train_data, .. callbacks=[loss_history])

1 个答案:

答案 0 :(得分:1)

默认情况下,您无权访问此信息,但可以通过将其传递给构造函数来为Callback提供所需的任何属性。例如,如果您使用的是生成器(数据存储在generator.y属性中):

class LossHistory(keras.callbacks.Callback):

    def __init__(self, data_generator, **kwargs):
        self.generator = data_generator
        Super(LossHistory, self).__init__(**kwargs)

    def on_epoch_end(self, epoch, logs={}):
        y_true = self.generator.y
        y_pred = self.model.predict_generator(self.generator)


loss_history = LossHistory(train_data_generator)