python绘制loss和accuracy曲线

一、导入工具包

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot

二、定义画图函数

def plot_acc_loss(loss, acc):
    host = host_subplot(111)  # row=1 col=1 first pic
    plt.subplots_adjust(right=0.8)  # ajust the right boundary of the plot window
    par1 = host.twinx()   # 共享x轴

    # set labels
    host.set_xlabel("steps")
    host.set_ylabel("test-loss")
    par1.set_ylabel("test-accuracy")

    # plot curves
    p1, = host.plot(range(len(loss)), loss, label="loss")
    p2, = par1.plot(range(len(acc)), acc, label="accuracy")

    # set location of the legend,
    # 1->rightup corner, 2->leftup corner, 3->leftdown corner
    # 4->rightdown corner, 5->rightmid ...
    host.legend(loc=5)

    # set label color
    host.axis["left"].label.set_color(p1.get_color())
    par1.axis["right"].label.set_color(p2.get_color())

    # set the range of x axis of host and y axis of par1
    # host.set_xlim([-200, 5200])
    # par1.set_ylim([-0.1, 1.1])

    plt.draw()
    plt.show()

     三、保存训练数据并调用

eval_loss_list = []
eval_acc_list = []

# save testint result...
eval_loss_list.append()
eval_acc_list.append()


plot_acc_loss(eval_loss_list, eval_acc_list)

python绘制loss和accuracy曲线_第1张图片

你可能感兴趣的:(python绘制loss和accuracy曲线)