A variational autoencoder (VAE) extends an autoencoder by replacing its middle encoding vector with a sampling layer. The encoder learns a representation’s mean and variance, samples a latent vector, and sends that vector to the decoder for reconstruction or generation. section 8.5 section 8.5.1
A practical VAE follows this path:
- Encode an image with convolutional layers; increasing strides reduce spatial dimensions without pooling. section 8.5.2
- Flatten the encoder output and produce z_mean and z_log_var as the learned latent parameters. section 8.5.2
- Sample z by adding scaled random noise to the mean: z = z_mean + exp(0.5 × z_log_var) × epsilon. section 8.5.2
- Decode z by reshaping a dense projection and expanding it with transposed convolutions. section 8.5.2
- Train with reconstruction loss plus a KL loss, using binary cross-entropy by default or mean squared error as an alternative. section 8.5.2
Generating a learned manifold
With a two-dimensional latent space, sweep x and y coordinates across selected ranges, decode each coordinate, and arrange the resulting 28×28 outputs into a grid. The grid makes changes across the learned representation space visible. section 8.5.2 section 8.5.1
Listing 8.21 EDL_8_5_VAE.ipynb: VAE hyperparameters
input_shape = (image_size, image_size, 1) #1 batch_size = 128 kernel_size = 3 #2 filters = 16 #3 latent_dim = 2 #4 epochs = 5
Use the hyperparameter listing to inspect input shape, batch size, kernel size, filter count, two-dimensional latent space, and training epochs before modifying the model. section 8.5.2
Listing 8.28 EDL_8_5_VAE.ipynb: Generating manifold images
n = 10
digit_size = 28
figure = np.zeros((digit_size * n, digit_size * n))
grid_x = np.linspace(-4, 4, n) #1
grid_y = np.linspace(-1, 1, n)[::-1]
for i, yi in enumerate(grid_y): #2
for j, xi in enumerate(grid_x): #2
z_sample = np.array([[xi, yi]]) #3
x_decoded = decoder.predict(z_sample) #4
digit = x_decoded[0].reshape
(digit_size, digit_size) #5
figure[i * digit_size: (i + 1) * digit_size,
j * digit_size: (j + 1) * digit_size] = digit
Use the manifold-generation listing to trace how a coordinate grid becomes decoded images assembled into a visual output. section 8.5.2
Useful experiments include changing the dataset, training samples, learning rate, batch size, layer counts, and other hyperparameters, then comparing generated results. section 8.4.1 section 8.5.3












