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

Keras搭建DNN帕金森预测模型时层输入维度不兼容(期望21实际接收22)

Keras搭建帕金森DNN二分类模型输入维度不匹配报错

问题场景

基于UCI开源帕金森数据集搭建深度神经网络二分类预测模型,使用Keras框架启动训练时,在第一个epoch阶段触发维度不匹配错误,无法正常完成训练流程。
当前数据集切片结果为195行、23列,其中包含22个特征属性、1个标识样本阴阳的类别标签,对应实现代码如下:

from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
dataset = loadtxt(r'/parkinsons.data - Copy.csv',delimiter=',')
X = dataset[:, 0:22]
y = dataset[:, 22]
X.shape
model = Sequential()
model.add(Dense(21, input_dim=21, activation='relu'))
model.add(Dense(21, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=150, batch_size=10)

核心报错信息

训练启动时触发的核心错误提示为:

ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected axis -1 of input shape to have value 21 but received input with shape (None, 22)

完整的报错栈日志如下:

Epoch 1/150
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-67-39d59395a0a6> in <module>()
----> 1 model.fit(X, y, epochs=150, batch_size=10)

9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    975           except Exception as e:  # pylint:disable=broad-except
    976             if hasattr(e, "ag_error_metadata"):
--> 977               raise e.ag_error_metadata.to_exception(e)
    978             else:
    979               raise

ValueError: in user code:

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step
        y_pred = self(x, training=True)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:259 assert_input_compatibility
        ' but received input with shape ' + display_shape(x.shape))

    ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected axis -1 of input shape to have value 21 but received input with shape (None, 22)

报错核心逻辑为:模型输入层预期接收的输入最后一个维度值为21,但实际传入的特征矩阵最后一维为22,维度不匹配导致训练中断。

问题原因

  • 代码中对特征集的切片操作dataset[:, 0:22]是符合预期的,Python切片遵循左闭右开规则,最终得到的X维度为195行、22个特征,完全匹配数据集的实际特征数量
  • 错误出在第一层Dense层的参数配置:给input_dim传入了21,这个参数的作用是指定输入特征的维度总数,不是该层的神经元数量
  • 因为把input_dim设为21,模型生成的输入层会校验传入数据的最后一维必须是21,和实际X的22维就产生了维度不匹配

修复方案

  • 将第一层Dense层的input_dim参数修改为和实际特征数一致的22,不需要修改该层的神经元配置,修改后的层定义为:
    model.add(Dense(21, input_dim=22, activation='relu'))
  • 若在Jupyter等交互式环境下运行,修改代码前建议重启内核清空内存中的旧计算图,避免之前残留的sequential_10旧模型配置干扰新的实例化过程
  • 修改后模型输入维度和实际特征矩阵维度完全匹配,即可正常启动训练流程

内容的提问来源于stack exchange,提问作者Anu Deepa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.27 06:27:04