1.简介
为了使毕设的数据看起来更加的好看点,记录一下解决问题的过程。
大体内容为:读取log文件,从中获取loss值,accuracy值,并绘制曲线。
2. 曲线绘制
文件读取的形式可以参考上一篇文章,主体不变,主要部分还是在字符串分割上。
def getLossAcc(logFile):
f = open(logFile, "r", encoding='utf-8')
line = f.readline() # 以行的形式进行读取文件
lossArr = []
accuray = []
while line:
nameArr = line.split(" ")
if nameArr[0] == "Test":
lossArr.append(float(nameArr[2]))
accurayStr = nameArr[4]
accurayStr = accurayStr[:6]
print(accurayStr)
accuray.append(float(accurayStr))
line = f.readline()
f.close()
return lossArr, accuray
曲线绘制
def drawLine(arr, xName, yName, title, graduate):
# 横坐标 采用列表表达式
x = [x+1 for x in range(len(arr))]
# 纵坐标
y = arr
# 生成折线图:函数polt
pyplot.plot(x, y)
# 设置横坐标说明
pyplot.xlabel(xName)
# 设置纵坐标说明
pyplot.ylabel(yName)
# 添加标题
pyplot.title(title)
# 设置纵坐标刻度
pyplot.yticks(graduate)
# 显示网格
pyplot.grid(True)
# 显示图表
pyplot.show()
3. 调用方式
if __name__ == '__main__':
loss, accuracy = getLossAcc(r"outputv5_fixImage_with20.log")
print(loss)
print(accuracy)
graduate = []
deGraduate = 5
# lossnew = []
# for i in range(len(loss)):
# if loss[i] < 100:
# lossnew.append(loss[i])
# 计算y的刻度值
for i in range(len(loss)):
if i * deGraduate < max(loss) + deGraduate:
graduate.append(i * deGraduate)
print(graduate)
drawLine(loss, "Iterated", "Loss", "Loss function curve", graduate)
drawLine(accuracy, "Iterated", "Accuracy", "Accuracy function curve", [0, 0.25, 0.5, 0.75, 1])
analyseArr(loss, accuracy)
4. 效果图
log文件的形式