问题描述
在训练模型时,想把终端打印出来的结果全部保存在一个实验日志中,方便以后查看。(如有更简便的方法请大家赐教,抱拳感谢!)
解决办法
1.首先创建一个实验结果的保存路径
output_dir = './ResultData'
os.makedirs(output_dir, exist_ok=True)
2.新建一个空的文件
timeslot = int(time.time())
dt = time.strftime("%Y%m%d%H%M%S", time.localtime(timeslot))
file = open(f'./ResultData/Baseline_01_exp{args.seed}_{args.dataset}_noniid[{args.degree_noniid}]_labelrate[{args.shot}]_FedavgFixmatch_{dt}_log.txt','w')
3.在print的时候,将其写入文件中(只写入文件中,终端不会打印结果),例如:
print('hello world',file=file)
文件中就会保存下来这句话,如图:
4.如果想在终端看到print的结果,把file=file去掉即可。
print('hello world')
以上是本人的笨办法,嘻嘻~