1. Goal:
Write the epoch during training, as well as the loss, train_acc and other results obtained after training into the csv file.
2. Implementation steps:
Create a new csv file for subsequent recording of time, epoch, loss, accuracy and other results.
Get the data and write the results into a csv file. (Note: This is just an example, so train_Loss and train_acc are fixed numbers written casually).
retrieve data.
Save the data in a one-dimensional list and then convert it to a DataFrame.
Write DataFrame data to csv file.
3. Code (example):
import pandas as pd
from datetime import datetime
# 1.新建csv文件,用于后续记录time、epoch、loss、accuracy等结果。
df = pd.DataFrame(columns=['time', 'epoch', 'train_Loss', 'train_acc']) # 列名
df.to_csv("./train_acc.csv", index=False) # 路径可以根据需要更改
# 2.获取数据,将结果写入csv文件中。(注:这里仅作举例说明,所以train_Loss、train_acc都是随便写的固定数字)
for i in range(10): #假设迭代10次
# 1)获取数据
time = "%s"%datetime.now() #获取当前时间
epoch = i
train_Loss = 0.23333
train_acc = 0.82222
# 2)将数据保存在一维列表,然后转化为DataFrame
list = [time, epoch , train_Loss, train_acc]
# 由于DataFrame是Pandas库中的一种数据结构,它类似excel,是一种二维表,所以需要将list以二维列表的形式转化为DataFrame
data = pd.DataFrame([list])
# 3)将数据写入csv文件
data.to_csv('./train_acc.csv', mode='a', header=False, index=False) # mode设为a,就可以向csv文件追加数据了
4. Result:
reference:
https://blog.csdn.net/weixin_43760844/article/details/113245871 Save training and testing loss, accuracy and other data to files