← Back to list

Understanding Latent Variables & Latent Space

A latent variable: compressed semantic representation learned automatically by the neural network.

Shayan Ali · 2026-05-09 20:23 · 50 claps · 2.1 min read
#artificial-intelligence #deep-learning #latent-diffusion-model #stable-diffusion #machine-learning
Open on Medium ↗
Wiki topics: MM · Multimodal & Generative Media ML · Machine Learning AI · AI · General EDU · Education & Learning 🔭 · Astronomy & Space 📰 · Journalism & News

Understanding Latent Variables & Latent Space

A latent variable: compressed semantic representation learned automatically by the neural network.

Is not directly observed but explains hidden structure in the data.

Examples:

For face images:

Latent variables may encode:

  • Hair style
  • Pose
  • Lighting
  • Facial expression
  • Age

observed : x = (pixel) latent : z = (Hair style, Age, Pose, Lighting, Facial expression) different z → different pixels || same z → similar pixels latent varabile explain variation Mathematically:

z~ p(z) latent while x ~ p(x/z) observed so p(x) = ∫p(x/z)p(z)dx ; You can nerver observe z but without it p(x) would be imposible and complex

Summary: latent variable is an observed variable introduces to explain and organize the variability

Latent ≠ noise: noise is typically random and unstructured, while a latent variable represents structured hidden factors that explain the data.

Latent space: The collection of all latent variables forms the latent space.

Mathematically:

z € Z , z~ p(z) while mapping x = f(z)

which means:

  • Standard Gaussian distribution
  • Mean = 0
  • Variance = 1

Neural Network as a compression machine

Input:

Image: 256 × 256 × 3 = 196,608 numbers

and the network gradually compresses that information into a much smaller representation:

z = [y1, y2] OR z ∈ R^128

This compressed representation is called a: “Latent Variable / Latent Vector”

The network learns:

  • what information matters,
  • what can be discarded,
  • and how to represent the essence of the image in fewer numbers.

How Neural Networks Create Latent Variables

Suppose input is:

64 × 64 grayscale image
Flattened : 
4096 number

The encoder neural network might look like:

4096 → 1024 → 256 → 64 → 2

Each layer compresses information.

Input Image
     ↓
Neural Network Layers
     ↓
Compressed Representation
     ↓
Latent Vector z

CNN Example for Images

For images, encoders are usually CNNs.

Example:

Image
  ↓
Conv Layer
  ↓
Conv Layer
  ↓
Pooling
  ↓
Flatten
  ↓
Dense Layer
  ↓
Latent Vector z

CNNs progressively extract: edges, textures, shapes, objects, semantic meaning. Then compress them into latent dimensions.

Transformer Example for Text

For text:

Sentence
   ↓
Tokenizer
   ↓
Embedding
   ↓
Transformer Layers
   ↓
Hidden State
   ↓
Latent Representation

The latent vector may encode: topic, sentiment, syntax, semantics, writing style.

Why Different Latent Sizes Exist

Small Latent Size (2, 8, 16)

Pros:

  • strong compression
  • interpretable
  • visualization possible

Cons:

  • information loss

Large Latent Size (256, 512, 1024)

Pros:

  • preserves detail
  • better generation quality

Cons:

  • harder to interpret
  • more computation

메타데이터
post_id
442b96f87f47
slug
understanding-latent-variables-latent-space-442b96f87f47
url
https://medium.com/@iaamshayan/understanding-latent-variables-latent-space-442b96f87f47
canonical_url
https://medium.com/@iaamshayan/understanding-latent-variables-latent-space-442b96f87f47
author_url
https://medium.com/@iaamshayan
status
ok
fetched_at
2026-08-12 20:31:36