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

Keras简单神经网络model.predict报错:输入维度不兼容问题咨询

问题解决:Keras model.predict() 输入维度不匹配错误

错误原因

你的模型定义了input_shape=[2],说明每个输入样本包含2个特征,而Keras的predict方法要求输入为批量格式(形状为(样本数量, 特征数量)的2维数组)。你传入的test=np.array([2,1])是1维数组(形状(2,)),不符合模型的输入维度要求,因此抛出expected min_ndim=2, found ndim=1的错误。

修复方案

将测试输入转换成2维数组即可,以下两种方法都可以:

  1. 使用np.expand_dims添加维度:
test = np.array([2,1])
test = np.expand_dims(test, axis=0)  # 将形状从(2,)转为(1,2)
print(model.predict(test))
  1. 直接定义为2维数组(外层添加括号):
test = np.array([[2,1]])  # 形状直接为(1,2)
print(model.predict(test))

补充说明

你训练时使用的input变量是2维数组(形状(10,2)),符合模型的输入要求,所以训练过程没有问题,仅预测阶段的输入格式出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 04:06:24