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
相关产品推荐
相关产品推荐

