版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1338328
本文主要内容是使用python matplotlib
绘制accuracy, cost曲线。在使用机器学习算法训练时往往需要输出训练的accuracy以及cost,但是最直观的方法还是绘制对应的曲线(根据训练的迭代期n),本文给出简要的绘制方法。
代码如下,也可见stackoverflow:
import numpy as np
import matplotlib.pyplot as plt
def draw_result(lst_iter, lst_loss, lst_acc, title):
plt.plot(lst_iter, lst_loss, '-b', label='loss')
plt.plot(lst_iter, lst_acc, '-r', label='accuracy')
plt.xlabel("n iteration")
plt.legend(loc='upper left')
plt.title(title)
plt.savefig(title+".png") # should before show method
plt.show()
def test_draw():
lst_iter = range(100)
lst_loss = [0.01 * i - 0.01 * i ** 2 for i in xrange(100)]
# lst_loss = np.random.randn(1, 100).reshape((100, ))
lst_acc = [0.01 * i + 0.01 * i ** 2 for i in xrange(100)]
# lst_acc = np.random.randn(1, 100).reshape((100, ))
draw_result(lst_iter, lst_loss, lst_acc, "sgd_method")
if __name__ == '__main__':
test_draw()
输出如下:
实际中的使用,也可见: