ValueError: Input 0 is incompatible with layer dense_6 in keras

人盡茶涼 提交于 2019-12-11 02:32:30

问题


I am trying to build a deep autoencoder by following this link, but I got this error:

ValueError: Input 0 is incompatible with layer dense_6: expected axis -1 of input shape to have value 128 but got shape (None, 32)

The code:

input_img = Input(shape=(784,))
encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(32, activation='relu')(encoded)

decoded = Dense(64, activation='relu')(encoded)
decoded = Dense(128, activation='relu')(decoded) #decode.shape = (?,128)
decoded = Dense(784, activation='relu')(decoded)

autoencoder = Model(input_img, decoded)

encoder = Model(input_img, encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input)) #ERROR HERE
...

This is the Error I got:

Traceback (most recent call last):
  File "autoencoder_deep.py", line 37, in <module>
    decoder = Model(encoded_input, decoder_layer(encoded_input))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/keras/engine/topology.py", line 569, in __call__
    self.assert_input_compatibility(inputs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/keras/engine/topology.py", line 479, in assert_input_compatibility
    ' but got shape ' + str(x_shape))
ValueError: Input 0 is incompatible with layer dense_6: expected axis -1 of input shape to have value 128 but got shape (None, 32)

Any suggestion or comment is greatly appreciated. Thank you.


回答1:


Following this answer try:

# retrieve the last layer of the autoencoder model 
decoder_layer1 = autoencoder.layers[-3]
decoder_layer2 = autoencoder.layers[-2]
decoder_layer3 = autoencoder.layers[-1]

# create the decoder model
decoder = Model(input=encoded_input, 
output=decoder_layer3(decoder_layer2(decoder_layer1(encoded_input))))


来源:https://stackoverflow.com/questions/44371383/valueerror-input-0-is-incompatible-with-layer-dense-6-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!