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

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

总要价:

大家好,我收到一个属性错误,我不太确定是什么原因。我正在关注此 autoencoder tutorial 并尝试复制一些代码。你能告诉我哪里错了吗?

自动编码器构建:

# 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)
])

当编码器和解码器连接在以下部分时,这是错误出现的时候:

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

错误:

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'

我试过的:

  1. 我用谷歌搜索了错误,post 建议更改输入形状。我不太了解自动编码器,但架构似乎已经有了这个参数。

  2. 另一个 建议添加 input_shape(已经存在),然后使用 build()

如有任何建议,我们将不胜感激!

组合 encoderdencoder 的最简单方法是实例化一个 Input 层并通过 encoderdecoder

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

here 是 运行 笔记本