How to design tf.keras callback to save model predictions for each batch and each epoch

I want to create a tf.keras callback to save model predictions for each batch and each epoch during the training using the training data sets.

i have tried the following callback, however it gives error like

AttributeError: 'PredictionCallback' object has no attribute 'X_train'

My code is

class PredictionCallback(tf.keras.callbacks.Callback):    

  def on_epoch_end(self, epoch, logs={}):

    y_pred = self.model.predict(self.X_train)

    print('prediction: {} at epoch: {}'.format(y_pred, epoch))

    pd.DataFrame(y_pred).assign(epoch=epoch).to_csv('{}_{}.csv'.format(filename, epoch)), y_train,validation_data=[X_valid,y_valid],epochs=epochs,batch_size=batch_size,
               callbacks=[model_checkpoint,reduce_lr,csv_logger, early_stopping,PredictionCallback()],

i also tried tensorflow - Create keras callback to save model predictions and targets for each batch during training - Stack Overflow but not get success yet.Hope experts will help me.Thanks.

Hi @titu_kumar, To create callbacks for each batch and epoch you have to define your customs callbacks like

class CustomCallback(keras.callbacks.Callback):
    def on_predict_batch_end(self, batch, logs=None):
        keys = list(logs.keys())
        print("...Predicting: end of batch {}; got log keys: {}".format(batch, keys))
    def on_epoch_end(self, epoch, logs=None):
        keys = list(logs.keys())
        print("End epoch {} of training; got log keys: {}".format(epoch, keys))

For more details please refer to this document. Thank You.