Home > Software engineering >  Keras: AttributeError: 'Sequential' object has no attribute '_nested_inputs'
Keras: AttributeError: 'Sequential' object has no attribute '_nested_inputs'

Time:09-23

Overall Ask:

Hi all, I am getting an attribute error and I am not quite sure what the cause is. I am following this autoencoder tutorial and trying to replicate some of the code. Can you tell me where I am going wrong?

Autoencoder Build:

# This is the dimension of the original space
input_dim = maxlen

# This is the dimension of the latent space (encoding space)
latent_dim = 2

encoder = Sequential([
    Dense(128, activation='relu', input_shape=(input_dim,)),
    Dense(64, activation='relu'),
    Dense(32, activation='relu'),
    Dense(latent_dim, activation='relu')
])

decoder = Sequential([
    Dense(64, activation='relu', input_shape=(latent_dim,)),
    Dense(128, activation='relu'),
    Dense(256, activation='relu'),
    Dense(input_dim, activation=None)
])

When the encoder and decoder are connected in the following part, this is when the error arises:

autoencoder = Model(inputs=encoder.input, outputs=decoder(encoder.output))

The error:

AttributeError: 'Sequential' object has no attribute '_nested_inputs'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<command-55734003> in <module>
----> 1 autoencoder = Model(inputs=encoder.input, outputs=decoder(encoder.output))
      2 
      3 autoencoder.compile(loss='mse', optimizer='adam')

/databricks/python/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py in input(self)
    241       AttributeError: If no inbound nodes are found.
    242     """
--> 243     return self._nested_inputs
    244 
    245   @property

AttributeError: 'Sequential' object has no attribute '_nested_inputs'

What I tried:

  1. I googled the error, and a post suggested altering the input shape. I do not know a lot about Autoencoders, but the architecture appears to already have this parameter.

  2. Another post suggested adding input_shape (which is already there), and then using build()

Any advice would be appreciated!

CodePudding user response:

The simplest way to combine your encoder and dencoder is to instantiate an Input layer and pass it through encoder and decoder.

inp = Input((input_dim,))
autoencoder = Model(inputs=inp, outputs=decoder(encoder(inp)))
autoencoder.compile('adam', 'mse')

here is the running notebook

  • Related