欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

python简单日志类的创建

时间:2023-08-19

在训练模型的时候,希望把训练过程中的loss,acc等指标记录到文件中以便后续查看。

class Logger(object): def __init__(self): self.terminal = sys.stdout #stdout self.file = None def open(self, file, mode=None): if mode is None: mode ='w' self.file = open(file, mode) def write(self, message, is_terminal=1, is_file=1 ): if 'r' in message: is_file=0 if is_terminal == 1: self.terminal.write(message) self.terminal.flush() #time.sleep(1) if is_file == 1: self.file.write(message) self.file.flush() def flush(self): # this flush method is needed for python 3 compatibility. # this handles the flush command by doing nothing. # you might want to specify some extra behavior here. pass

log.open('log.txt',mode='a') log.write("epoch:{}n".format(epoch)) log.write("loss:{}n".format(loss))

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。