Practical Exercises Chapter 7
Exercise 1: Build and Train a Simple Autoencoder
Task: Build and train a simple autoencoder to reconstruct images from the MNIST dataset. Evaluate the quality of the reconstructions by visualizing the original and reconstructed images.
Solution:
from tensorflow.keras import layers, models
(x_train, _), (x_test, _) = tf.keras.datasets.mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), 28, 28, 1))
x_test = x_test.reshape((len(x_test), 28, 28, 1))
# Build the autoencoder model
input_img = layers.Input(shape=(28, 28, 1))
x = layers.Conv2D(16, (3, 3), activation='relu', padding ...