2. Building an autoencoder using Keras
We're now going to move onto something really exciting, building an autoencoder using the tf.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.
Listing 3.2.1: autoencoder-mnist-3.2.1.py
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.layers import Conv2D, Flatten
from tensorflow.keras.layers import Reshape, Conv2DTranspose
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist...