Macalinao StudioLearnAll enriched lessons ↗
College path · 7 sectionsSwitch to high school ↗

College / LONGER LESSON 14

VAE and diffusion: two different learning problems

Work a Gaussian KL, reparameterize a latent, and distinguish corruption from denoising.

Preparation: Means, noise and squares; probability densities for college.

Plan 20–35 minutes with the activities; this is a study estimate, not a timed requirement. Both paths have complete narration. Your notes and progress are kept separately for each level.

Section 01 / 07

Compressing is not the same as denoising

College path. Work a Gaussian KL, reparameterize a latent, and distinguish corruption from denoising.

Preparation: Means, noise and squares; probability densities for college. Work through one section at a time. Predict the experiment result before changing its controls, then explain what the example leaves out.

A VAE specifies a generative model with latent prior and decoder likelihood, plus an approximate posterior used for training. Diffusion specifies forward noising transitions and learns a reverse generative process. Latent diffusion composes a representation model with diffusion over its codes; it does not make the VAE objective identical to a denoising objective. State which random variables, distributions and loss belong to each component.

Worked step 1. Encoder: input x becomes parameters of a latent distribution.

Worked step 2. Decoder: a sampled latent z produces a reconstruction distribution.

Worked step 3. Diffusion: a clean sample is progressively corrupted and a reverse process is learned.

Try it yourself. Does training a VAE automatically train a diffusion denoiser?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. The models solve different objectives. A combined system needs the relevant components trained or supplied separately.

Section 02 / 07

Balance reconstruction and the latent prior

The negative evidence lower bound is expected negative log-likelihood plus KL(q(z|x)||p(z)). Maximizing the ELBO is equivalent to minimizing its negative. For a standard normal prior and diagonal Gaussian posterior, the KL has a closed form. A squared-error reconstruction term corresponds to particular Gaussian-likelihood assumptions and scaling; it is not a universal replacement for any decoder likelihood. Distinguish a beta-weighted variant from the unweighted ELBO.

Worked step 1. Write a reconstruction penalty that decreases when the decoder fits the input.

Worked step 2. Add the posterior-to-prior KL term for negative ELBO.

Worked step 3. Check whether your code minimizes loss or maximizes a bound before choosing signs.

−ELBO = E_q[−log p(x|z)] + KL(q(z|x)||p(z))

Negative ELBO is expected negative reconstruction log likelihood plus posterior-to-prior KL divergence.

Try it yourself. Why is subtracting a nonnegative KL from a minimized reconstruction loss suspicious?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. It would reward increasing that divergence under the basic negative-ELBO objective. Always check objective direction and the intended variant.

Section 03 / 07

Compute one Gaussian KL

For q=N(μ,σ²) and p=N(0,1), KL=0.5(μ²+σ²−1−log σ²). Sum across independent latent dimensions. The result is nonnegative and vanishes at μ=0,σ²=1. If a network predicts log variance ℓ, use exp(ℓ) for variance and exp(ℓ/2) for standard deviation. Do not put σ directly into the variance term or reverse the sign of the logarithm.

Worked step 1. Set μ=1 and σ²=1.

Worked step 2. Compute 0.5×(1+1−1−0)=0.5.

Worked step 3. Set μ=0 with the same variance to obtain zero.

KL = ½(μ²+σ²−1−log σ²)

Half the sum of mean squared and variance, minus one and log variance.

Try it yourself. What is the KL at μ=0 and variance four?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. One half times four minus one minus log four, approximately 0.8069. It is positive despite the matching mean.

Section 04 / 07

Move randomness into a standard sample

The reparameterization z=μ+σ⊙ε with ε~N(0,I) expresses sampling through a parameter-free noise source and differentiable transformations. Conditional on ε, derivatives with respect to μ and σ are direct. This does not eliminate sampling variance or make every random operation differentiable. For log-variance parameter ℓ, σ=exp(ℓ/2), and its chain rule must be included.

Worked step 1. Choose μ=1, σ=2 and ε=−0.5.

Worked step 2. Multiply two by minus one half to get minus one.

Worked step 3. Add the mean one to obtain z=0.

z=μ+σ⊙ε; ε~N(0,I)

Sample standard normal noise, scale it by standard deviation and add the mean.

Try it yourself. If the network outputs log variance, should you exponentiate it directly to get standard deviation?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. Exponentiating log variance gives variance. Standard deviation is the exponential of half the log variance.

Section 05 / 07

Forward noise uses sampled noise

The forward marginal is x_t=√ᾱ_t x_0+√(1−ᾱ_t)ε with ε sampled from a standard normal. In an epsilon-prediction parameterization, training compares εθ(x_t,t) with that sampled ε, subject to the chosen weighting. The model prediction belongs to the learned reverse process or reconstruction estimate; substituting it into the forward corruption changes the training construction.

Worked step 1. Set x₀=1, ᾱ=0.64 and ε=−1.

Worked step 2. Compute 0.8×1+0.6×(−1)=0.2.

Worked step 3. The known training noise is minus one, even before the denoiser makes a prediction.

x_t=√ᾱ_t x₀+√(1−ᾱ_t)ε

Multiply the clean signal by the square root of cumulative alpha and noise by the square root of one minus cumulative alpha.

Try it yourself. Why are 0.64 and 0.36 not the coefficients used directly here?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. They are variance fractions. The amplitude coefficients are their square roots, 0.8 and 0.6.

Section 06 / 07

Separate an oracle check from generation

The oracle estimate x₀=(x_t−√(1−ᾱ_t)ε)/√ᾱ_t verifies a forward marginal when ᾱ_t>0. Replacing ε with a prediction introduces error that is amplified at low signal levels. Reverse sampling additionally depends on parameterization, schedule and variance choices. Keep likelihood modeling, approximate posterior inference, forward diffusion and learned reverse sampling distinct when evaluating a combined latent diffusion pipeline.

Worked step 1. From x_t=0.2 and ε=−1, add 0.6 to recover 0.8.

Worked step 2. Divide by 0.8 to recover x₀=1.

Worked step 3. An imperfect predicted noise would produce a different reconstruction.

Try it yourself. Does exact recovery with known noise prove the denoiser is accurate?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. It checks algebra with information the model does not receive. Learned prediction quality requires separate evaluation.

Section 07 / 07

Mastery and transfer

Bring the pieces together. First answer the checkpoint without the worked solutions. Then explain the mechanism in your own words, using one numerical example from this lesson.

For q=N(0,1) and p=N(0,1), KL is… Option 1: −1. Option 2: 0. Option 3: 1.

Pause here and choose an answer before continuing. The checkpoint explanation follows.

Reveal worked solution

The correct choice is option 2: 0. Identical distributions have zero KL.

Transfer task. Change one assumption in the worked example. State your prediction, calculate or test it in Workbench, then explain whether the result supports your prediction.

Reveal worked solution

Self-check rubric. Name the changed assumption, keep the other inputs fixed, show at least one intermediate calculation, and distinguish what the toy example guarantees from what would require a trained model or a real measurement.

Sources and scope

Original worked examples and teaching text, grounded in these primary references. Numerical fixtures explain mechanisms; model quality and hardware performance require separate measurements. Both levels share key examples, with different explanations and checkpoints.

Reset this college path?

This removes only this path’s notes, progress and experiments from this browser.