Python/Keras - Creating a callback with one predic

2020-06-17 05:45发布

问题:

I'm using Keras to predict a time series. As standard I'm using 20 epochs. I want to know what did my neural network predict for each one of the 20 epochs.

By using model.predict I'm getting only one prediction among all epochs (not sure how Keras select it). I want all predictions, or at least the 10 best.

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.

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.

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. 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)).

The question now is: How do I adapt my code so it adds to predhis the predictions of each one of the epochs?

回答1:

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.