How to decode encoded data from a deep autocoder in Keras (fuzzy textbook)

I followed the tutorial "Creating Auto Coders in Keras": https://blog.keras.io/building-autoencoders-in-keras.html

The first, simple solution works fine. But in the "Deep autoencoder" section, the code presented in the tutorial does not work fully.

Here is my code (until the problem appears) that is just copied from turorial:

from keras.layers import Input, Dense from keras.models import Model encoding_dim = 32 input_img = Input(shape=(784,)) encoded = Dense(128, activation='relu')(input_img) encoded = Dense(64, activation='relu')(encoded) encoded = Dense(32, activation='relu')(encoded) # Multiple encoding decoded = Dense(64, activation='relu')(encoded) # and decoding layers. decoded = Dense(128, activation='relu')(decoded) decoded = Dense(784, activation='sigmoid')(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)) # Crash happens here. 

I get this error:

 Traceback (most recent call last): File "keras_test.py", line 20, in <module> decoder = Model(encoded_input, decoder_layer(encoded_input)) # Crash happens here File "/Users/paulmagnus/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/site-packages/keras/engine/topology.py", line 569, in __call__ self.assert_input_compatibility(inputs) File "/Users/paulmagnus/Library/Enthought/Canopy/edm/envs/User/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) 

I assume that the decoder is associated with the wrong level of decoding and / or that the form of its input or output is incorrect. But what should I do about it?

autoencoder does not require a decoder for autoencoder . I can do the training and encode images by following the rest of the tutorial. But without a decoder I cannot decode the images back to the original format to see if they really look good. This tutorial says nothing about it, and it just shows decoded images without words. I assume that the author suggested that the changes he made for the decoder to achieve this were trivial.

To clarify: the single-layer version works fine, where instead of 3 coding and 3 decoding levels, we only have

 encoded = Dense(32, activation='relu')(input_img) decoded = Dense(784, activation='sigmoid')(encoded) 

and everything else is the same as above. Then there is no error, and I can use decoder to recreate the images.

+5
source share
1 answer
 decoder_layer = autoencoder.layers[-1] decoder = Model(encoded_input, decoder_layer(encoded_input)) 

This code works for single-level, because only the last layer is a decoder in this case and

 decoder_layer = autoencoder.layers[-1] 

this line calls the last layer.

for three-layer encoders and decoders, you must call all 3 levels to determine the decoder. I did the same manual, so I wrote code like this.

 encoded_input = Input(shape=(encoding_dim,)) deco = autoencoder.layers[-3](encoded_input) deco = autoencoder.layers[-2](deco) deco = autoencoder.layers[-1](deco) # create the decoder model decoder = Model(encoded_input, deco). 

Now it works great.

+11
source

Source: https://habr.com/ru/post/1268659/


All Articles