Now that we defined encoder and decoder, we define the model which takes images as input and returns the output of the decoder which is the reconstructed image:
model = Model(inputs=input_image, outputs=decoder)
Let's look at a summary of the model:
model.summary()
________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) (None, 784) 0 _________________________________________________________________ dense (Dense) (None, 32) 25120 _________________________________________________________________ dense_1 (Dense) (None, 784) 25872 ================================================================= Total params: 50,992 Trainable params: 50,992 Non-trainable params: 0 _________________________________________________________________
Compile the model with loss as a binary cross-entropy and minimize the loss using the adadelta optimizer:
model.compile(optimizer='adadelta', loss='binary_crossentropy')
Now let's train the model.
Generally, we train the model as model.fit(x,y) where x is the input and y is the label. But since autoencoders reconstruct their inputs, the input and output to the model should be the same. So, here, we train the model as model.fit(x_train, x_train):
model.fit(x_train, x_train, epochs=50, batch_size=256, shuffle=True, validation_data=(x_test, x_test))