简体   繁体   English

Python / Keras - 为每个纪元创建一个带有一个预测的回调

[英]Python/Keras - Creating a callback with one prediction for each epoch

I'm using Keras to predict a time series. 我正在使用Keras来预测时间序列。 As standard I'm using 20 epochs. 作为标准,我使用了20个时代。 I want to know what did my neural network predict for each one of the 20 epochs. 我想知道我的神经网络为20个时期中的每一个预测了什么。

By using model.predict I'm getting only one prediction among all epochs (not sure how Keras select it). 通过使用model.predict我在所有时期中只得到一个预测(不确定Keras如何选择它)。 I want all predictions, or at least the 10 best. 我想要所有的预测,或者至少是10个最好的预测。

According to a previous answer I got, I should compute the predictions after each training epoch by implementing an appropriate callback by subclassing Callback() and calling predict on the model inside the on_epoch_end function. 根据我之前得到的答案,我应该在每个训练时期之后通过子类化Callback()并在on_epoch_end函数内的模型上调用predict来实现适当的回调来计算预测。

Well, the theory seems in shape but I'm in trouble to code that. 嗯,这个理论似乎很有形,但我很难编码。 Would anyone be able to give a code example on that? 有人能够给出一个代码示例吗?

Not sure how to implement the Callback() subclassing and neither how to mix that with the model.predict inside an on_epoch_end function. 不知道如何实现Callback()子类化以及如何将它与on_epoch_end函数中的model.predict混合。

Your help will be highly appreciated :) 非常感谢您的帮助:)


EDIT 编辑

Well, I evolved a little bit. 好吧,我进化了一点点。 Found out how to create the subclass and how to link it to the model.predict. 了解如何创建子类以及如何将其链接到model.predict。 However, I'm burning my brain on how to create a list with all the predictions. 但是,我正在大肆宣传如何使用所有预测创建列表。 Below is my current code: 以下是我目前的代码:

#Creating a Callback subclass that stores each epoch prediction
class prediction_history(Callback):
    def on_epoch_end(self, epoch, logs={}):
        self.predhis=(model.predict(predictor_train))

#Calling the subclass
predictions=prediction_history()

#Executing the model.fit of the neural network
model.fit(X=predictor_train, y=target_train, nb_epoch=2, batch_size=batch,validation_split=0.1,callbacks=[predictions]) 

#Printing the prediction history
print predictions.predhis

However, all I'm getting with that is a list of predictions of the last epoch (same effect as printing model.predict(predictor_train)). 然而,我所得到的只是上一个时期的预测列表(与打印model.predict(predictor_train)相同的效果)。

The question now is: How do I adapt my code so it adds to predhis the predictions of each one of the epochs? 现在的问题是:如何适应我的代码,以便将其添加到predhis历元的每一个的预测?

You are overwriting the prediction for each epoch, that is why it doesn't work. 你正在覆盖每个时代的预测,这就是为什么它不起作用。 I would do it like this: 我会这样做:

class prediction_history(Callback):
    def __init__(self):
        self.predhis = []
    def on_epoch_end(self, epoch, logs={}):
        self.predhis.append(model.predict(predictor_train))

This way self.predhis is now a list and each prediction is appended to the list at the end of each epoch. 这样self.predhis现在是一个列表,每个预测在每个时代结束时附加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM