You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow模型训练异常:损失恒定、精度不稳定、验证精度为0

模型无学习效果的问题分析与解决

你的模型训练后输出均匀分布(置信度≈1/7≈14.3%),损失稳定在ln(7)≈1.946,说明模型完全没有学到区分伪影类别的特征,以下是核心问题和修复方案:

一、数据管道与预处理问题

  1. batch_size过小
    batch_size=1会导致Adam优化器的梯度估计噪声极大,训练完全不稳定,精度波动剧烈。根据显存容量,将batch_size调整为16/32/64,比如:
    batch_size = 16  # 按需调整
    
  2. 未启用数据缓存
    注释掉.cache()会导致每次epoch都重新加载图像,训练效率低且数据波动大。恢复缓存:
    train_ds = train_ds.cache().shuffle(buffer_size=1000).prefetch(buffer_size=AUTOTUNE)
    val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
    
  3. 数据增强不合理且未生效
    伪影是图像压缩产生的高频细节,几何变换(旋转、翻转)会破坏伪影的原始结构,反而干扰特征学习;且你定义了data_augmentation但未应用到训练流程。
    • 若伪影与图像方向无关,可移除旋转/翻转,保留轻微缩放;
    • 将增强层加入模型,或映射到训练数据:
      # 方案1:加入模型
      model = Sequential([
        data_augmentation,  # 放在Rescaling前
        layers.Rescaling(1.0/255, input_shape=(img_height, img_width, 1)),
        # ... 后续层
      ])
      # 方案2:映射到训练集
      train_ds = train_ds.map(lambda x, y: (data_augmentation(x, training=True), y))
      

二、模型结构缺陷

  1. 特征提取能力不足
    你的网络通道数太少(2→4→8→16),且大步长卷积(stride=4)严重丢失伪影细节。调整方案:
    • 增大卷积通道数,比如从16开始逐步提升;
    • 用stride=2替代stride=4,或加入MaxPooling层降采样,保留更多特征:
      model = Sequential([
        layers.Rescaling(1.0/255, input_shape=(img_height, img_width, 1)),
        layers.Conv2D(16, (3, 3), padding='same', activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(32, (3, 3), padding='same', activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(128, (3, 3), padding='same', activation='relu'),
        layers.MaxPooling2D((2, 2)),
        # 继续添加2-3层卷积
        layers.Flatten(),
        layers.Dense(128, activation='relu'),
        layers.Dense(num_classes, activation='softmax')
      ])
      
  2. 缺少归一化层
    在卷积层后加入BatchNormalization,稳定训练过程,加速收敛:
    layers.Conv2D(16, (3, 3), padding='same', activation='relu'),
    layers.BatchNormalization(),
    layers.MaxPooling2D((2, 2)),
    
  3. 任务选型优化
    伪影程度是连续变量,7类是程度分级,相比分类,用回归任务(预测伪影程度的连续值)可能更合理,修改输出层和损失函数:
    # 输出层改为单个神经元,用线性激活
    layers.Dense(1, activation='linear')
    # 编译时用MAE或MSE损失
    model.compile(optimizer='adam', loss='mean_absolute_error', metrics=['mae'])
    

三、训练流程问题

  1. 模型保存时机错误
    你在训练前就保存了模型,覆盖了训练后的结果,应移到model.fit之后:
    history = model.fit(...)
    model.save("./model/AAT")  # 训练完成后保存
    
  2. 验证集异常排查
    验证精度始终为0,检查验证集的类别是否正确:
    # 打印验证集的类别分布
    val_labels = []
    for _, labels in val_ds:
        val_labels.extend(labels.numpy())
    print(np.unique(val_labels, return_counts=True))
    
    确认验证集包含所有7个类别,且标签与训练集对应。

四、数据本身验证

可视化不同类别的图像,确认伪影程度的差异是否足够明显:

plt.figure(figsize=(14,7))
for class_idx, class_name in enumerate(class_names):
    img_path = list(data_dir.glob(f"{class_name}/*.jpg"))[0]
    img = tf.keras.utils.load_img(img_path, color_mode="grayscale")
    ax = plt.subplot(2,4,class_idx+1)
    plt.imshow(img, cmap='gray')
    plt.title(class_name)
    plt.axis('off')
plt.show()

如果不同类别的视觉差异极小,可能需要调整数据生成方式,增强伪影的区分度。


内容的提问来源于stack exchange,提问作者Josef Jílek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 20:37:34