如何使用Tensorboard的数据,自己使用plot()函数,在同一副图中画出多条loss曲线

最近在写论文时,需要在一张图中,同时绘制出多个模型的loss变化曲线,之前虽然在训练模型时,也会使用Tensorboard来观察loss曲线的变化,但是只限于观看,并没有对立里面的数据进行提取和分析。后查找资料,成功解决问题,现将教程分享如下:

一、下载Tensorboard中的loss曲线的数据

1.选中左上角的 Show data download links

2.选中右下角的下载文件的格式,在这里,我选择的是csv格式,但实际数据下载下来的文件格式是.txt

如何使用Tensorboard的数据,自己使用plot()函数,在同一副图中画出多条loss曲线

二、修改下载后的文件内容及格式

选在csv文件格式下载后的文件,实际是.txt的格式,并且最上面一行是 Wall time,Step,Value

1.我们需要将最上面的一行删除

2.将文件的后缀名直接改成 .csv,我们就得到了Tensorboard的csv数据格式

三、使用Pycharm 进行数据处理

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.font_manager import FontProperties
import csv

'''读取csv文件'''
def readcsv(files):
    csvfile = open(files, 'r')
    plots = csv.reader(csvfile, delimiter=',')
    x = []
    y = []
    for row in plots:
        y.append((row[2])) 
        x.append((row[1]))
    return x ,y


mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'


plt.figure()
x2,y2=readcsv("scalars3.csv")
plt.plot(x2, y2, color='red', label='Default')
#plt.plot(x2, y2, '.', color='red')

x,y=readcsv("scalars.csv")
plt.plot(x, y, 'g',label='Without BN')

x1,y1=readcsv("scalars2.csv")
plt.plot(x1, y1, color='black',label='Without DW and PW')

x4,y4=readcsv("scalars4.csv")
plt.plot(x4, y4, color='blue',label='Without Residual learning')

plt.xticks(fontsize=16)
plt.yticks(fontsize=16)

plt.ylim(0, 16)
plt.xlim(0, 104800)
plt.xlabel('Steps',fontsize=20)
plt.ylabel('Score',fontsize=20)
plt.legend(fontsize=16)
plt.show()

如何使用Tensorboard的数据,自己使用plot()函数,在同一副图中画出多条loss曲线