71 min total

Generative AI

Variational autoencoders

Variational autoencoders

Imagine a hypothetical scenario where your tailor Sam tells you to organize your clothing into a wardrobe infinitely high and wide, when you want to request an item, you simply tell Sam the location of wardrobe and he will sew the item (from scratch), you will need to place similar items near each other so that Sam can accurately re-create each item given only its location, after several weeks of practice Sam understands the wardrobe layout, it's now possible for you to tell Sam the location of any clothing item and he accurately sews it from scratch. One day you decide to point to an empty space in wardrobe, Sam instead of finding an existing item creates a new piece of clothing that has never been seen before based on surrounding locations. Sam didn’t memorize each clothing item exactly, but rather learned relationship by different clothing items. If jeans are at (3,5) and shorts are at (3,7) whats at (3,6)? Maybe a hybrid jeans and shorts - that’s exactly what VAEs do. In VAEs, this is called sampling from the latent space, the model generates new data by sampling from similar distributions.

If there was another tailor named Alex (Autoencoder) if you would provide coordinated of (3,6) Alex would either: Tell you 'I don’t know what goes there' or try to recreate the nearest known item, but with no smooth blending or interpolation. Sam learned the underlying 'rules' of clothing design and how features smoothly transition between different items, Alex learned (ratta) exact coordinates for specific items.

Think of it like the difference between:

  • A chef who understands cooking principles and can create new dishes by combining flavors (VAE)
  • A chef who can only reproduce exact recipes they've memorized (AE)
GenAI / Mid 01 — Variational autoencoders — notes
GenAI / Mid 01 — Variational autoencoders — notes
GenAI / Mid 01 — Variational autoencoders

Bbut KL Divergence is used in Sparse AEs too? 😵

KL divergence in VAEs is used for totally different purpose - to make sure the learned distributions are similar to a standard normal distribution. Think carefully 🤔 how does the model fucking knows that the neuron is learning a probability distribution ?

How does a neuron understands that it is not learning a casual weight value but a mean/variance value? Though the KL divergence 😎 which forces the latent space to represent a probability distribution, not just a set of fixed values.

VAE Loss function

KL loss is minimized to 0 when and = 1 for all dimensions

You could initialize μ=0 and =1, 😆 but fixing them at zero would prevent the model from learning useful latent representations. Instead, training allows the network to gradually adjust them, leading to better interpolation and generalization.

Once a VAE is trained, whether you discard the encoder or decoder depends on the use case:

For new sample generation → Keep the Decoder, discard the Encoder.

For latent space representation → Keep the Encoder, discard the Decoder.

For full VAE functionality (reconstruction & generation) → Keep both.

Auto encoder:-

GenAI / Mid 01 — Variational autoencoders

Variational Auto Encoder:-

GenAI / Mid 01 — Variational autoencoders
GenAI / Mid 01 — Variational autoencoders