Latent Space Representations
The middle of the model is a latent space, and the way a VAE fills that space is the subject of this article.
Why an Image Should Not Map to a Single Point
A variational autoencoder, or VAE, takes an image through an encoder and a decoder, but it does not pass through a single fixed code in the middle. The middle of the model is a latent space, and the way a VAE fills that space is the subject of this article. Instead of asking the encoder for one exact point that stands for an input, a VAE asks the encoder to describe a region. A point is then drawn from that region, and the decoder turns the drawn point back into an image.
Keep that picture in mind as you read. The rest of the article traces one input from the moment it enters the encoder to the moment a reconstructed output comes out of the decoder, paying close attention to what is stored at each stage and what the sampling step does with it.
Tracing an Input Through the Three Stages
A VAE has three stages: encoding, sampling, and decoding. In the encoding stage, the input image goes through the encoder. In the sampling stage, the encoder's output is used to draw a latent point z. In the decoding stage, the decoder receives z and produces an output that is meant to resemble the original input. The sampled point z, not the original image and not the distribution parameters alone, is the value that gets decoded.
Read the diagram as a chain of hand-offs. The encoder does not hand the decoder an image. It hands the sampling step a pair of vectors. The sampling step does not hand the decoder those vectors either. It hands over one point drawn from the distribution they describe. Each stage changes what the data represents, and the decoder only ever sees the last of those representations.
What the Encoder Actually Outputs
The encoder is a convolutional network that maps an input image to two vectors. In the implementation described by the source, convolutional layers process the image, the result is flattened, and a dense layer produces an intermediate representation. Two further dense layers then produce z_mean and z_log_var. Together these outputs describe the distribution used for sampling in latent space.
A common first reading is that z_mean and z_log_var are two alternative images or two competing reconstructions. They are neither. They are numbers that describe a statistical distribution. z_mean locates the center of the region the encoder has chosen for this input, and z_log_var is a variance-related parameter that describes how far samples are allowed to spread around that center.
The encoder's output becomes useful only after the sampling step. Before sampling, z_mean and z_log_var are just two sets of numbers. The sampling step is what converts them into a single latent point the decoder can use.
Sampling With Epsilon and the Reparameterization Step
The VAE combines z_mean and z_log_variance with epsilon, which is a random tensor of small values. The result is z, the latent-space point that is passed to the decoder. The standard way to write this combination is shown below. Here $\epsilon$ is the random draw, and the exponential of half of z_log_variance gives the spread used to scale that draw.
Trace the flow in the diagram. z_mean supplies the center. z_log_variance, after being halved and exponentiated, supplies the spread. epsilon supplies the random direction and size of the draw, and it is multiplied by the spread before being added to the center. Because epsilon is random, two passes through the same input can produce two different values of z, even though the encoder outputs stayed the same.
In Keras, code that is not already part of a built-in layer can be placed inside a Lambda layer. This is how the sampling operation is incorporated into the network, so the random draw becomes one more step in the model rather than something handled outside it.
Computing One Sampled Latent Value
For one latent dimension, the encoder outputs z_mean = 0.5 and z_log_variance = -2. Compute z for epsilon = 1.0, then for epsilon = -0.5.
- 1Halve z_log_variance
z_log_variance/ 2 = -2 / 2 = -1. - 2Exponentiate
exp(-1) is approximately 0.368. This is the spread used to scale epsilon.
- 3Compute z for epsilon = 1.0
z = 0.5 + 0.368 × 1.0 = 0.868.
- 4Compute z for epsilon = -0.5
z = 0.5 + 0.368 × (-0.5) = 0.5 - 0.184 = 0.316.
The same encoder output gives two different latent points, 0.868 and 0.316, depending on the random epsilon. Both are drawn from the region described by z_mean = 0.5 and the spread set by z_log_variance = -2.
Why Fixed Codes Are Replaced by Regions
The comparison below sets a single fixed code against the distribution parameters a VAE uses. In the fixed case, the encoder's output for an input is one point, and that same point is passed to the decoder every time. In the VAE case, the encoder's output is a mean and a variance-related parameter, and the decoder receives a fresh sample from the region they describe.
Randomness in a VAE is not there merely to make the model unpredictable. Because samples are drawn around the encoded location, nearby latent points are encouraged to decode into similar images. This gives the latent space continuity: moving to a nearby point should not abruptly change the decoded result. Along with low dimensionality, this continuity helps make directions in latent space meaningful for changing aspects of the data. This is the basis for the source's description of the approach as useful for image generation and for image editing through concept vectors.
Think of the encoder's output as marking a neighborhood rather than a dot. Points sampled from the same neighborhood should decode to outputs that look alike, and the space between neighborhoods is filled with smoothly varying results rather than sudden jumps.
Two Losses Pulling in Opposite Directions
A VAE is trained with two loss functions. Reconstruction loss encourages the decoded sample to match the original input. Regularization loss encourages a well-formed latent space and helps reduce overfitting to the training data. The two pressures act on the same model but favor different things, and both are needed.
Reconstruction loss, on its own, would push the model toward reproducing each training input as accurately as possible. Regularization loss, on its own, would push the latent space toward a well-organized structure. Reconstruction alone focuses on reproducing inputs, while regularization supports the organized latent structure that makes sampling and manipulation useful. Training balances the two.
Common Mistakes About VAE Outputs
Common mistake Treating z_mean and z_log_var as two reconstructed images
What it looks likeAssuming the encoder's two output vectors are two alternative versions of the input.
Why it failsThe source states that these outputs describe the distribution used for sampling. They are not two images and not two alternative reconstructions.
Do this insteadRead z_mean and z_log_var as numbers that define a region. Only the sampled point z is passed to the decoder.
Common mistake Decoding the encoder output directly
What it looks likeSending z_mean straight to the decoder and skipping the sampling step.
Why it failsThe source says the encoder's output becomes useful only after the sampling step, and that the sampled point z is what gets decoded.
Do this insteadAlways insert the sampling step between the encoder and the decoder.
Common mistake Thinking epsilon is a learned parameter
What it looks likeAssuming the random tensor epsilon is adjusted by training like a weight.
Why it failsThe source describes epsilon as a random tensor of small values that is combined with
z_meanandz_log_variance.Do this insteadTreat epsilon as a random draw that changes on every pass, while the encoder's outputs are what the network computes.
Common mistake Assuming randomness is there just to add noise
Why it failsThe source explains that sampling around the encoded location encourages nearby latent points to decode into similar images, which gives the latent space continuity.
Do this insteadUnderstand the randomness as a means of organizing the latent space.
Common mistake Believing reconstruction loss alone is enough to train a useful latent space
Predicting the Effect of Zero Epsilon
What do you think happens?
If epsilon is set to exactly zero for one latent dimension, what value does z take, given the formula z = z_mean + exp(z_log_variance / 2) times epsilon?
Reveal answer
z equals z_mean
The spread term is multiplied by epsilon. When epsilon is zero, that term vanishes, leaving only z_mean. The draw becomes deterministic, so the sample sits exactly at the center of the region the encoder described.
Using the worked example, keep z_mean = 0.5 and z_log_variance = -2, and compute z for epsilon = 2.0. Then explain in one sentence what a larger spread (a larger z_log_variance) would do to the range of values z can take across different epsilon draws.
Hints
- Start by computing exp(-1) again, since
z_log_variance/ 2 is unchanged. - Multiply the spread by epsilon before adding
z_mean. - A larger
z_log_varianceincreases the exponential term, which widens the spread of possible z values.
Key Points to Carry Forward
- 1A VAE moves an input through encoding, sampling, and decoding, and the decoder receives the sampled point z rather than a fixed code.
- 2The encoder outputs
z_meanandz_log_var, which describe a distribution rather than a single point or a reconstruction. - 3z is built from
z_mean, the spread fromz_log_variance, and a random epsilon, so each pass can give a different latent point from the same region. - 4Reconstruction loss pulls toward faithful reproduction, while regularization loss pulls toward an organized, continuous latent space that reduces overfitting.
Key Takeaways
- A VAE moves an input through encoding, sampling, and decoding, and the decoder receives the sampled point z rather than a fixed code.
- The encoder outputs
z_meanandz_log_var, which describe a distribution rather than a single point or a reconstruction. - z is built from
z_mean, the spread fromz_log_variance, and a random epsilon, so each pass can give a different latent point from the same region. - Reconstruction loss pulls toward faithful reproduction, while regularization loss pulls toward an organized, continuous latent space that reduces overfitting.