Search code examples
pythontensorflowkerasdeep-learning

error reshaping a Dense layer in Keras functional API


Hi I want to reshape a layer after a Dense layer but it returns funny error. Here is the code

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),

Here is the error

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)',)

Solution

  • The mistake is you added the additional commas. Just delete them. Like this:

    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)