liveBook · Manning

Latent space

As explained in Evolutionary Deep Learning: Genetic algorithms and neural networks

A latent space is the multidimensional representation space in which an encoder places input data for decoding or generation.

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

Figure 8.13 Sample learned output of VAE, showing the learned 2D manifold

The learned two-dimensional manifold visualizes how positions in a VAE latent space decode into different generated outputs. section 8.5.1

How Evolutionary Deep Learning: Genetic algorithms and neural networks covers it

How deep it goes
Works through VAE sampling, decoder wiring, loss terms, and two-dimensional traversal without deriving the underlying mathematics.
Examples
Uses 28×28 decoded image grids from two-dimensional VAE coordinates and a GAN generator receiving 100-dimensional noise.
What it assumes
Assumes familiarity with convolutional and Dense layers while explicitly constructing latent sampling and decoder input shapes.
What you can do after
Build it from scratch by wiring VAE sampling and decoder inputs, or by feeding random latent vectors into a GAN generator.
How it connects
Connects variational autoencoders with encoders, decoders, mean, variance, sampling layers, KL loss, and generative adversarial networks.

Also explained in

Compare how 18 books explain latent space →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Evolutionary Deep Learning: Genetic algorithms and neural networks. The section links point to the parts of the book it draws on.