## Building autoencoders using Keras

We're now going to move onto something really exciting, building an autoencoder using Keras library. For simplicity, we'll be using the MNIST dataset for the first set of examples. The autoencoder will then generate a latent vector from the input data and recover the input using the decoder. The latent vector in this first example is 16-dim.

Firstly, we're going to implement the autoencoder by building the encoder. *Listing* *3.2.1* shows the encoder that compresses the MNIST digit into a 16-dim latent vector. The encoder is a stack of two `Conv2D`

. The final stage is a `Dense`

layer with 16 units to generate the latent vector. *Figure 3.2.1* shows the architecture model diagram generated by `plot_model()`

which is the same as the text version produced by `encoder.summary()`

. The shape of the output of the last `Conv2D`

is saved to compute the dimensions of the decoder input layer for easy reconstruction of the MNIST image.

The following Listing 3.2.1, shows `autoencoder-mnist...`