得到每个epoch的loss和predict精度后,就可以愉快地画图直观地看出训练结果和收敛性了。
# coding:utf-8
import matplotlib.pyplot as plt
data_dir = "D:\\result.txt"
Train_Loss_list = []
Train_Accuracy_list = []
Valid_Loss_list = []
Valid_Accuracy_list = []
f1 = open(data_dir,'r')
data = []
#把训练结果输出到result.txt里,比较笨的办法,按字节位数去取数字结果
for line in f1:
if(line.find('train')>=0):
# if (string.find(line, 'train') != -1):
Train_Loss_list.append(line[12:18])
Train_Accuracy_list.append(line[24:30])
if (line.find('valid') >= 0):
# if (string.find(line, 'valid') != -1):
Valid_Loss_list.append(line[12:18])
Valid_Accuracy_list.append(line[24:30])
f1.close()
#迭代了30次,所以x的取值范围为(0,30),然后再将每次相对应的准确率以及损失率附在x上
x1 = range(0, 30)
x2 = range(0, 30)
y1 = Train_Accuracy_list
y2 = Train_Loss_list
y3 = Valid_Accuracy_list
y4 = Valid_Loss_list
plt.subplot(2, 1, 1)
# plt.plot(x1, y1, 'o-',color='r')
plt.plot(x1, y1, 'o-',label="Train_Accuracy")
plt.plot(x1, y3, 'o-',label="Valid_Accuracy")
plt.title('Test accuracy vs. epoches')
plt.ylabel('Test accuracy')
plt.legend(loc='best')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-',label="Train_Loss")
plt.plot(x2, y4, '.-',label="Valid_Loss")
plt.xlabel('Test loss vs. epoches')
plt.ylabel('Test loss')
plt.legend(loc='best')
plt.show()