深度学习模型训练结果可视化: 训练集和验证集的准确率和损失曲线

本文展示如何使用 Python 代码绘制深度学习模型训练结果的准确率和损失曲线,帮助理解模型训练过程和性能。

代码示例:

accuracy = history.history['accuracy']
test_accuracy = history.history['val_accuracy']
loss = history.history['loss']
test_loss = history.history['val_loss']
epochs_range = range(10)

plt.figure(figsize=(50, 5))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, accuracy, label='Training Acc')
plt.plot(epochs_range, test_accuracy, label='Test Acc')
plt.legend()
plt.title('Training and Test Acc')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training loss')
plt.plot(epochs_range, test_loss, label='Test loss')
plt.legend()
plt.title('Training and Test loss')
plt.show()

代码解释:

  1. accuracy = history.history['accuracy']: 从训练历史记录中获取模型在训练集上的准确率。

  2. test_accuracy = history.history['val_accuracy']: 从训练历史记录中获取模型在验证集上的准确率。

  3. loss = history.history['loss']: 从训练历史记录中获取模型在训练集上的损失。

  4. test_loss = history.history['val_loss']: 从训练历史记录中获取模型在验证集上的损失。

  5. epochs_range = range(10): 定义训练轮数的范围。

  6. plt.figure(figsize=(50, 5)): 创建一个图形,并设置其大小。

  7. plt.subplot(1, 2, 1): 在图形中创建一个子图。

  8. plt.plot(epochs_range, accuracy, label='Training Acc'): 绘制训练集准确率的曲线。

  9. plt.plot(epochs_range, test_accuracy, label='Test Acc'): 绘制验证集准确率的曲线。

  10. plt.legend(): 添加图例。

  11. plt.title('Training and Test Acc'): 添加子图标题。

  12. plt.subplot(1, 2, 2): 在图形中创建另一个子图。

  13. plt.plot(epochs_range, loss, label='Training loss'): 绘制训练集损失的曲线。

  14. plt.plot(epochs_range, test_loss, label='Test loss'): 绘制验证集损失的曲线。

  15. plt.legend(): 添加图例。

  16. plt.title('Training and Test loss'): 添加子图标题。

  17. plt.show(): 显示图形。

结论:

通过绘制训练集和验证集的准确率和损失曲线,我们可以直观地观察模型在训练过程中的性能变化,例如模型是否过拟合、训练集和验证集上的性能差异等。这有助于我们更好地理解模型的训练过程并进行调整,以获得更好的训练结果。


原文地址: https://www.cveoy.top/t/topic/or0V 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录