A latent space is the vector space between a model’s input and its generated or reconstructed output. An encoder can place an input in that space; a decoder uses the resulting vector to produce an image. In generative models, points in the space act as compact inputs from which new outputs are made. section 8.5.1 section 9.1.2
A variational autoencoder (VAE) treats the latent representation as a distribution rather than a single fixed encoding. Its encoder produces a mean and variance, then a sampling layer creates a latent vector Z. Feeding Z to the decoder supports reconstruction and generation; varying the learned range can reveal a two-dimensional manifold of outputs. section 8.5.1 section 8.5.2
A practical VAE latent-space implementation has these parts:
- Reduce convolutional encoder output with Flatten and Dense layers, then produce parallel z_mean and z_log_var vectors sized by latent_dim. section 8.5.2
- Sample random epsilon values with the batch and latent dimensions, then scale and shift them using the learned variance and mean to form z. section 8.5.2
- Make the decoder accept an input shaped as latent_dim, expand it with Dense and Reshape layers, and use transposed convolutions to generate an image. section 8.5.2
- Balance reconstruction loss with KL loss when assembling the VAE loss. section 8.5.2
Walking a two-dimensional VAE space
With latent_dim set to 2, create a grid of coordinate pairs, pass each pair to decoder.predict, reshape each result as a 28×28 image, and arrange the images into a grid. The result lets you inspect how decoded output changes across sampled locations. section 8.5.2
A GAN uses latent space differently: it starts from random noise rather than an encoded image. The generator takes a noise vector whose length is latent_dim, expands it through Dense, Reshape, upsampling, and convolutional layers, and produces an image. During training, newly sampled noise creates fake images for the discriminator and drives generator updates. section 9.1.2 section 9.1.2
The learned two-dimensional manifold visualizes how positions in a VAE latent space decode into different generated outputs. section 8.5.1

















