keras训练模型时绘出每个epoch的accuracy和loss

keras训练模型时绘出每个epoch的accuracy和loss

  • 一、绘制模型训练过程中的accuracy和loss
  • 二、绘制训练好的模型测试过程中的accuracy和loss
    • 1.交叉熵损失函数
    • 2.评估accuracy_score

一、绘制模型训练过程中的accuracy和loss

# 绘制图形以确保准确性
# 训练集准确率
plt.plot(history.history['accuracy'], label='training accuracy')
# 验证集准确率
plt.plot(history.history['val_accuracy'], label='val accuracy')
plt.title('acc')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.savefig("./result/每一轮准确度图片.png")
plt.legend()
plt.show()

plt.plot(history.history['loss'], label='training loss')
plt.plot(history.history['val_loss'], label='val loss')
plt.title('Loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.savefig("./result/每一轮损失值图片.png")
plt.legend()
plt.show()

二、绘制训练好的模型测试过程中的accuracy和loss

1.交叉熵损失函数

input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)

cross_entropy_loss = torch.nn.CrossEntropyLoss()
output = cross_entropy_loss(input, target)
loss = output

该处使用Cross-Entropy(交叉熵损失函数),参考链接https://zhuanlan.zhihu.com/p/383997503

2.评估accuracy_score

from sklearn.metrics import accuracy_score
model = load_model('my_traffic_classifier.h5')
pred = model.predict_classes(X1_test)
print(accuracy_score(labels_test,pred))
accuracy = accuracy_score(labels_test,pred)

绘制图形

# 测试集准确率
plt.plot(accuracy, label='testing accuracy')
plt.title('acc')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.savefig("./result/每一轮准确度图片.png")
plt.legend()
plt.show()

plt.plot(loss, label='testing loss')
plt.title('loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.savefig("./result/每一轮损失值图片.png")
plt.legend()
plt.show()

你可能感兴趣的:(keras,pytorch,python)