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

创建tf.keras.Sequential模型遇TypeError,疑与input_shape相关

问题分析与解决

你的input_shape是正常的,错误根源是**tf.keras.Sequential的构造参数格式不正确**。

你当前的代码直接将多个Dense层作为位置参数传入Sequential,但Sequential.__init__只接受1-3个位置参数(第一个参数应为层的列表/元组,其余是可选的命名参数),你传入了3个层加默认的self,总共4个位置参数,因此触发TypeError。

修正后的代码

input_shape = [x_train_scaled.shape[1]]
# 将所有层放入一个列表中传入Sequential
model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=64, input_shape=input_shape, activation='relu'),
    tf.keras.layers.Dense(units=64, activation='relu'),
    tf.keras.layers.Dense(units=1)
])
model.summary()

关键说明

  • tf.keras.Sequential的标准用法是接收一个层的列表作为第一个参数,而非多个独立的层参数。
  • 你的input_shape = [6]是正确的,符合Dense层对输入形状的要求(无需包含批量维度)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:22:02