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

本文介绍了如何在Keras中训练模型时记录并绘制训练和验证阶段的accuracy与loss变化图。首先,通过`history.history`获取训练历史,然后使用matplotlib库绘制精度和损失曲线。接着,利用PyTorch的交叉熵损失函数评估模型,并用sklearn的accuracy_score计算测试集的准确率。最后,展示了如何将这些信息以图形形式展示出来,帮助理解模型的性能。
Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

一、绘制模型训练过程中的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()

您可能感兴趣的与本文相关的镜像

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值