这篇文章将为大家详细讲解有关大数据开发中如何绘制损失函数,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
文件夹下内容:
文件夹布局:
—— 绘制损失函数曲线 —— —— drwaLossFunction.py —— —— loadFromPickle.py —— —— drwaLossCurve_tensorboard.py —— —— drwaLossCurve_matplotlib.py
文件: drwaLossFunction.py
# 获取训练世代和相应损失值import osimport pickledef createPickleFiles():'''用于生成三个pickle文件,分别用于保存训练世代数、置信度损失和定位损失'''myFolderPath = r'logs'files = os.listdir(myFolderPath) # 列表类型 元素是文件名 字符串类型 files.sort(key=lambda x:int(x[5:x.index('-')]), reverse=False) # 按照训练世代排序loss4loc = list() # 记录定位损失loss4cof = list() # 记录置信度损失record4epoch = list() # 记录训练世代数for item in files:info = item.split('-')record4epoch.append(int(info[0][5:]))loss4loc.append(float(info[1][3:]))loss4cof.append(float(info[2][4:-4]))with open('绘制损失函数曲线/临时文件-EPOCH.pickle','wb') as file4epoch:pickle.dump(record4epoch,file4epoch) # 保存世代信息with open('绘制损失函数曲线/临时文件-LOSS4CONFIDENT.pickle','wb') as file4conf:pickle.dump(loss4cof,file4conf) # 保存置信度损失信息with open('绘制损失函数曲线/临时文件-LOSS4LOCATION.pickle','wb') as file4loc:pickle.dump(loss4loc,file4loc) # 保存定位损失信息if __name__ == '__main__':createPickleFiles()
文件: loadFromPickle.py
# 从pickle文件中加载世代数和相应损失import pickledef getInfo():'''返回三个列表,分别用于保存世代、置信度损失和定位损失'''info = list()fileNames = ['绘制损失函数曲线/临时文件-EPOCH.pickle','绘制损失函数曲线/临时文件-LOSS4CONFIDENT.pickle','绘制损失函数曲线/临时文件-LOSS4LOCATION.pickle'] # 三个文件的名字for name in fileNames:with open(name, 'rb') as f:item = pickle.load(f)info.append(item)return info if __name__ == '__main__':info = getInfo()# print(info[0]) # 世代# print(info[1]) # 置信度损失# print(info[2]) # 定位损失
文件: drwaLossCurve_tensorboard.py
from torch.utils.tensorboard import SummaryWriterimport loadFromPickleimport drwaLossFunction drwaLossFunction.createPickleFiles()info = loadFromPickle.getInfo()writer = SummaryWriter('绘制损失函数曲线/单独绘制')for epoch, conf, loc in zip(*info):writer.add_scalar('置信度损失', conf, epoch)writer.add_scalar('定位损失', loc, epoch)writer.close()writer = SummaryWriter('绘制损失函数曲线/三者对比')for epoch, conf, loc in zip(*info):writer.add_scalars('损失函数', { 'conf':conf,'loc':loc,'conf + loc': conf + loc}, epoch)writer.close()# '''# tensorboard --logdir=绘制损失函数曲线# '''
文件: drwaLossCurve_matplotlib.py
# 使用matplotlib绘制损失函数曲线 import drwaLossFunctionimport loadFromPickleimport matplotlibimport matplotlib.pyplot as plt matplotlib.rcParams['font.family'] = 'SimHei' # 'SimHei' # 'STSong'matplotlib.rcParams['font.size'] = 15 # 修改字体大小plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 drwaLossFunction.createPickleFiles()info = loadFromPickle.getInfo()epoch, conf, loc = info plt.plot(epoch, loc,'rx-.', label='定位损失')plt.plot(epoch, conf, 'go-', label='置信度损失')plt.xlabel('训练世代: epoch')plt.ylabel('损失值: loss')plt.legend(loc='upper right', frameon=True)plt.show()
关于“大数据开发中如何绘制损失函数”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/223756.html