python保存log日志,实现用log日志画图
在神经网络训练中,我们常常需要画出lossfunction的变化图,log日志里会显示每一次迭代的lossfunction的值,于是我们先把log日志保存为log.txt文档,再利用这个文档来画图。
1,先来产生一个log日志。
importmxnetasmx importnumpyasnp importos importlogging logging.getLogger().setLevel(logging.DEBUG) #Trainingdata logging.basicConfig(filename=os.path.join(os.getcwd(),'log.txt'),level=logging.DEBUG)#把log日志保存为log.txt train_data=np.random.uniform(0,1,[100,2]) train_label=np.array([train_data[i][0]+2*train_data[i][1]foriinrange(100)]) batch_size=1 num_epoch=5 #EvaluationData eval_data=np.array([[7,2],[6,10],[12,2]]) eval_label=np.array([11,26,16]) train_iter=mx.io.NDArrayIter(train_data,train_label,batch_size,shuffle=True,label_name='lin_reg_label') eval_iter=mx.io.NDArrayIter(eval_data,eval_label,batch_size,shuffle=False) X=mx.sym.Variable('data') Y=mx.sym.Variable('lin_reg_label') fully_connected_layer=mx.sym.FullyConnected(data=X,name='fc1',num_hidden=1) lro=mx.sym.LinearRegressionOutput(data=fully_connected_layer,label=Y,name="lro") model=mx.mod.Module( symbol=lro, data_names=['data'], label_names=['lin_reg_label']#networkstructure ) model.fit(train_iter,eval_iter, optimizer_params={'learning_rate':0.005,'momentum':0.9}, num_epoch=20, eval_metric='mse',) model.predict(eval_iter).asnumpy() metric=mx.metric.MSE() model.score(eval_iter,metric)
上面的代码中logging.basicConfig(filename=os.path.join(os.getcwd(),'log.txt'),level=logging.DEBUG)#把log日志保存为log.txt就是把log日志保存为log.txt文件。
2,log.txt文档如下。
INFO:root:Epoch[0]Train-mse=0.470638 INFO:root:Epoch[0]Timecost=0.047 INFO:root:Epoch[0]Validation-mse=73.642301 INFO:root:Epoch[1]Train-mse=0.082987 INFO:root:Epoch[1]Timecost=0.047 INFO:root:Epoch[1]Validation-mse=41.625072 INFO:root:Epoch[2]Train-mse=0.044817 INFO:root:Epoch[2]Timecost=0.063 INFO:root:Epoch[2]Validation-mse=23.743375 INFO:root:Epoch[3]Train-mse=0.024459 INFO:root:Epoch[3]Timecost=0.063 INFO:root:Epoch[3]Validation-mse=13.511120 INFO:root:Epoch[4]Train-mse=0.013431 INFO:root:Epoch[4]Timecost=0.063 INFO:root:Epoch[4]Validation-mse=7.670062 INFO:root:Epoch[5]Train-mse=0.007408 INFO:root:Epoch[5]Timecost=0.063 INFO:root:Epoch[5]Validation-mse=4.344374 INFO:root:Epoch[6]Train-mse=0.004099 INFO:root:Epoch[6]Timecost=0.063 INFO:root:Epoch[6]Validation-mse=2.455608 INFO:root:Epoch[7]Train-mse=0.002274 INFO:root:Epoch[7]Timecost=0.062 INFO:root:Epoch[7]Validation-mse=1.385449 INFO:root:Epoch[8]Train-mse=0.001263 INFO:root:Epoch[8]Timecost=0.063 INFO:root:Epoch[8]Validation-mse=0.780387 INFO:root:Epoch[9]Train-mse=0.000703 INFO:root:Epoch[9]Timecost=0.063 INFO:root:Epoch[9]Validation-mse=0.438943 INFO:root:Epoch[10]Train-mse=0.000391 INFO:root:Epoch[10]Timecost=0.125 INFO:root:Epoch[10]Validation-mse=0.246581 INFO:root:Epoch[11]Train-mse=0.000218 INFO:root:Epoch[11]Timecost=0.047 INFO:root:Epoch[11]Validation-mse=0.138368 INFO:root:Epoch[12]Train-mse=0.000121 INFO:root:Epoch[12]Timecost=0.047 INFO:root:Epoch[12]Validation-mse=0.077573 INFO:root:Epoch[13]Train-mse=0.000068 INFO:root:Epoch[13]Timecost=0.063 INFO:root:Epoch[13]Validation-mse=0.043454 INFO:root:Epoch[14]Train-mse=0.000038 INFO:root:Epoch[14]Timecost=0.063 INFO:root:Epoch[14]Validation-mse=0.024325 INFO:root:Epoch[15]Train-mse=0.000021 INFO:root:Epoch[15]Timecost=0.063 INFO:root:Epoch[15]Validation-mse=0.013609 INFO:root:Epoch[16]Train-mse=0.000012 INFO:root:Epoch[16]Timecost=0.063 INFO:root:Epoch[16]Validation-mse=0.007610 INFO:root:Epoch[17]Train-mse=0.000007 INFO:root:Epoch[17]Timecost=0.063 INFO:root:Epoch[17]Validation-mse=0.004253 INFO:root:Epoch[18]Train-mse=0.000004 INFO:root:Epoch[18]Timecost=0.063 INFO:root:Epoch[18]Validation-mse=0.002376 INFO:root:Epoch[19]Train-mse=0.000002 INFO:root:Epoch[19]Timecost=0.063 INFO:root:Epoch[19]Validation-mse=0.001327
3,利用log.txt文件来画图。
importre importmatplotlib.pyplotasplt importnumpyasnp defmain(): file=open('log.txt','r') list=[] #searchthelineincludingaccuracy forlineinfile: m=re.search('Train-mse',line) ifm: n=re.search('[0]\.[0-9]+',line)#正则表达式 ifnisnotNone: list.append(n.group())#提取精度数字 file.close() plt.plot(list,'go') plt.plot(list,'r') plt.xlabel('count') plt.ylabel('accuracy') plt.title('Accuracy') plt.show() if__name__=='__main__': main()
以上这篇python保存log日志,实现用log日志来画图就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。