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

MNIST数字分类模型所有测试样本预测结果一致问题求助

MNIST分类模型所有预测结果一致的问题修复

问题描述

基于MNIST数据集构建0-9数字分类模型时,模型对所有测试样本的预测结果完全相同,predicted_x的所有行数值完全一致。复现代码及输出如下:

复现代码

import numpy as np

from tensorflow import keras
from keras.datasets import mnist
from keras.layers import Dense
from keras.activations import softmax
from keras.models import Sequential
from keras.optimizers import Adam
from keras.losses import SparseCategoricalCrossentropy, CategoricalCrossentropy
import matplotlib.pyplot as plt
from keras.utils import to_categorical

(train_X, train_y), (test_X, test_y) = mnist.load_data()

train_y = to_categorical(train_y, num_classes=10)

train_X, train_y = np.asarray(train_X), np.asarray(train_y)

train_X = train_X.reshape([train_X.shape[0], train_X.shape[1] * train_X.shape[2]])
test_X = test_X.reshape([test_X.shape[0], test_X.shape[1] * test_X.shape[2]])

model  = Sequential(
    [
        Dense(units=25, activation="relu"),
        Dense(units=15, activation="relu"),
        Dense(units=10, activation="linear"),
    ]
)

model.compile(loss=CategoricalCrossentropy(from_logits=True), optimizer=Adam(0.01))

model.fit(train_X, train_y, epochs=3)

predicted_x = model.predict(test_X)
print(predicted_x)

异常输出

[[ 0.07475024  0.21513198 -0.0613958  ...  0.05913167  0.04869198
  -0.03899333]
 [ 0.07475024  0.21513198 -0.0613958  ...  0.05913167  0.04869198
  -0.03899333]
 [ 0.07475024  0.21513198 -0.0613958  ...  0.05913167  0.04869198
  -0.03899333]
 ...
 [ 0.07475024  0.21513198 -0.0613958  ...  0.05913167  0.04869198
  -0.03899333]
 [ 0.07475024  0.21513198 -0.0613958  ...  0.05913167  0.04869198
  -0.03899333]
 [ 0.07475024  0.21513198 -0.0613958  ...  0.05913167  0.04869198
  -0.03899333]]

问题根源

  1. 未做输入数据归一化:MNIST图像像素值范围为0-255,大数值会导致ReLU激活函数快速饱和,梯度消失,模型无法学习到有效特征。
  2. 未指定输入层形状:第一个Dense层未定义input_shape,虽Keras会自动推断,但可能导致初始化异常,影响模型训练逻辑。
  3. 学习率过高:Adam优化器设置的0.01学习率偏大,易导致模型训练不稳定,无法收敛到有效权重。

修复方案

核心修复点

  • 对训练集和测试集的像素值做归一化,将0-255缩放到0-1区间
  • 给第一个Dense层添加input_shape参数,明确输入维度
  • 降低Adam优化器的学习率至0.001,提升训练稳定性

修复后的完整代码

import numpy as np

from tensorflow import keras
from keras.datasets import mnist
from keras.layers import Dense
from keras.models import Sequential
from keras.optimizers import Adam
from keras.losses import CategoricalCrossentropy

# 加载数据集
(train_X, train_y), (test_X, test_y) = mnist.load_data()

# 标签独热编码
train_y = keras.utils.to_categorical(train_y, num_classes=10)

# 数据预处理:展平+归一化
train_X = train_X.reshape([train_X.shape[0], train_X.shape[1] * train_X.shape[2]]) / 255.0
test_X = test_X.reshape([test_X.shape[0], test_X.shape[1] * test_X.shape[2]]) / 255.0

# 构建模型,指定输入形状
model = Sequential(
    [
        Dense(units=25, activation="relu", input_shape=(784,)),
        Dense(units=15, activation="relu"),
        Dense(units=10, activation="linear"),
    ]
)

# 编译模型,调整学习率为0.001
model.compile(loss=CategoricalCrossentropy(from_logits=True), optimizer=Adam(0.001))

# 训练模型,添加batch_size提升训练效率
model.fit(train_X, train_y, epochs=3, batch_size=32)

# 预测并打印前5个样本结果验证
predicted_x = model.predict(test_X)
print(predicted_x[:5])

修复后效果

运行修复后的代码,predicted_x的每行数值会出现明显差异,模型能够根据不同输入输出对应的预测结果,后续可通过np.argmax(predicted_x, axis=1)获取分类标签,验证模型分类能力。

内容的提问来源于stack exchange,提问作者prabuddha atul raj bastola

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 04:37:49