[英]PyTorch save prediction as CSV for each epoch
I'm trying save the predictions I am getting from a model in PyTorch as csv.我正在尝试将我从 PyTorch 中的 model 获得的预测保存为 csv。 The following code.
以下代码。 However, the code seem to overwrite the prediction at each epoch and the final.csv file only contains 3 values.
但是,代码似乎覆盖了每个时期的预测,最终的 csv 文件仅包含 3 个值。 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. 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.