Error in keras when compiling autoencoder?
<p>This is the model of my autoencoder: </p>

<pre><code>input_img = Input(shape=(1, 32, 32))

x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 2, 2, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)

x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
</code></pre>

<p>This is my fit and predict function:</p>

<pre><code>autoencoder.fit(X_train, X_train,
            nb_epoch=10,
            batch_size=128,
            shuffle=True,
            validation_data=(X_test, X_test))

decoded_imgs = autoencoder.predict(X_test)
</code></pre>

<p>When i try to compile this i get the following error. 
All the images of my dataset are 32x32 pixels. Why this error then ? </p>

<pre><code>Exception: Error when checking model target: expected convolution2d_7 to have shape (None, 1, 28, 28) but got array with shape (4200, 1, 32, 32)
</code></pre>

<p>What change do i need to do in my model such that the input shape becomes (1,32,32)  ? </p>

<p>That was simple: </p>

<pre><code>input_img = Input(shape=(1, 32, 32))

x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 2, 2, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)

x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
</code></pre>

<p>You forgot about adding appropriate <code>border_mode='same'</code> in the 6th convolutional layer. </p>

python machine-learning neural-network keras autoencoder