PyTorch save prediction as CSV for each epoch
I'm trying save the predictions I am getting from a model in PyTorch as csv. The following code. However, the code seem to overwrite the prediction at each epoch and the final .csv file only contains 3 values. Any idea what I am doing wrong please:
output = model(images)
preds = output.sum(dim=[1,2,3])
np.savetxt('preds.csv', preds.detach())



Assuming your output will be float, you can map that to str and apply join as a comma separated string, finally append row to csv.
import pandas as pd
import numpy as np
import torch

output = torch.rand(5,5,5,5)
preds = output.sum(dim=[1,2,3])

print(preds)

with open('preds.csv','a') as fd:
    fd.write( ','.join(map(str, preds.detach().tolist())) + '\n')

