How to save predictions for each epoch by Trainer?

During training, I make prediction and evaluate my model at the end of each epoch. I want to save the prediction results every time I evaluate my model. How to achieve this using Trainer?

Thanks a lot!

Anyone can help? Even a few simple instructions would be much appreciated!

Hi, this may be a late reply and you may have already got a solution.

If not, you can try to export your predictions and true labels in to a pandas data frame in side your compute_metrics, sample code below:

def compute_metrics(pred):

      labels = pred.label_ids
      preds = pred.predictions.argmax(-1)
      new_df = pd.DataFrame( )
      new_df['pred_label'] = y_pred
      new_df['true_label'] = y_true
      new_df.to_csv('/content/drive/MyDrive/results.csv', header=True)
      # your regular metrics computation code.... 

Thanks
Satya

This will only save the latest result for evaluation. I think he might need the result after each epoch.

Is there an update on this? Is it possible to pass savedir or filename through the trainer? say something like epoch number, etc.

1 Like