模型训练过程可视化展示
在机器学习项目中,训练过程的实时监控至关重要。本文将介绍如何构建一个完整的训练过程可视化系统。
核心监控指标配置
1. 训练损失与验证损失
import matplotlib.pyplot as plt
import numpy as np
# 记录训练历史
history = {
'loss': [],
'val_loss': [],
'accuracy': [],
'val_accuracy': []
}
# 实时更新图表
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
ax1.plot(history['loss'], label='Training Loss')
ax1.plot(history['val_loss'], label='Validation Loss')
ax1.set_title('Loss Curve')
ax1.legend()
ax2.plot(history['accuracy'], label='Training Accuracy')
ax2.plot(history['val_accuracy'], label='Validation Accuracy')
ax2.set_title('Accuracy Curve')
ax2.legend()
plt.show()
2. 学习率变化监控
# 使用TensorBoard回调
from tensorflow.keras.callbacks import TensorBoard
import datetime
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)
告警配置方案
关键阈值设置:
- 验证损失增长超过5%触发告警
- 准确率下降超过2%触发警告
- 训练时间超过预设阈值(如2小时)发出提醒
实时监控脚本:
import time
import requests
def monitor_training():
while True:
# 检查最新指标
latest_metrics = get_latest_metrics()
if latest_metrics['val_loss'] > threshold:
send_alert('Validation loss increased', 'critical')
time.sleep(300) # 每5分钟检查一次
通过上述配置,可以实现训练过程的实时可视化和自动化告警,确保模型训练质量。

讨论