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

一、绘制模型训练过程中的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()
  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值