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

Keras函数式API中Dense层后Reshape报错求助

Keras函数式API中Reshape层报错:'tuple' object has no attribute 'shape'

问题代码

codings_size=10
decoder_inputs = tf.keras.layers.Input(shape=[codings_size])
# x=tf.keras.layers.Flatten(decoder_inputs)
x=tf.keras.layers.Dense(3 * 3 * 16)(decoder_inputs),
x=tf.keras.layers.Reshape((3, 3, 16))(x),

报错信息

AttributeError: Exception encountered when calling layer "reshape_28" (type Reshape).

'tuple' object has no attribute 'shape'

Call arguments received by layer "reshape_28" (type Reshape):
  • inputs=('tf.Tensor(shape=(None, 144), dtype=float32)',)

问题原因

代码中Dense层和Reshape层的赋值语句末尾多了逗号,导致变量x被定义为元组类型,而Reshape层需要接收张量(Tensor)对象作为输入,元组没有shape属性,因此触发错误。

修正后的代码

codings_size=10
decoder_inputs = tf.keras.layers.Input(shape=[codings_size])
# x=tf.keras.layers.Flatten(decoder_inputs)
x = tf.keras.layers.Dense(3 * 3 * 16)(decoder_inputs)
x = tf.keras.layers.Reshape((3, 3, 16))(x)

只需去掉两行赋值语句末尾的逗号,让x保持为张量类型即可解决问题。

内容的提问来源于stack exchange,提问作者Linh Chi Nguyen

相关产品推荐
方舟 Agent Plan

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

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