Intuition Why We Need This
When training Variational Autoencoders (VAEs) , we need to backpropagate through a random sampling operation . But gradients can't flow through randomness—it's like trying to optimize a dice roll. The reparameterization trick is a clever algebraic move that separates the randomness from the parameters , making the stochastic node differentiable.
Think of it this way: instead of drawing from a distribution that depends on learned parameters (can't differentiate), we draw from a fixed distribution and then transform that sample using our parameters (totally differentiable).
In a VAE, the encoder outputs parameters of a distribution q ϕ ( z ∣ x ) q_\phi(z|x) q ϕ ( z ∣ x ) , typically N ( μ ϕ ( x ) , σ ϕ 2 ( x ) ) \mathcal{N}(\mu_\phi(x), \sigma_\phi^2(x)) N ( μ ϕ ( x ) , σ ϕ 2 ( x )) . We need to:
Sample z ∼ q ϕ ( z ∣ x ) z \sim q_\phi(z|x) z ∼ q ϕ ( z ∣ x )
Compute loss involving z z z
Backpropagate to update ϕ \phi ϕ (the encoder parameters)
Common mistake Why Naive Sampling Fails
The wrong approach: Sample z z z directly from N ( μ , σ 2 ) \mathcal{N}(\mu, \sigma^2) N ( μ , σ 2 ) .
Why it feels right: This is how we normally sample from distributions in statistics.
Why it's wrong: The sampling operation z ∼ N ( μ , σ 2 ) z \sim \mathcal{N}(\mu, \sigma^2) z ∼ N ( μ , σ 2 ) is non-differentiable with respect to μ \mu μ and σ \sigma σ . If you change μ \mu μ slightly, the random sample doesn't change in a predictable way—it's still random! The gradient ∂ z ∂ μ \frac{\partial z}{\partial \mu} ∂ μ ∂ z is undefined.
The consequence: Backpropagation stops dead. No gradients flow to the encoder.
Start with the property of Gaussian distributions:
If ϵ ∼ N ( 0 , 1 ) \epsilon \sim \mathcal{N}(0, 1) ϵ ∼ N ( 0 , 1 ) (standard normal), then for any μ \mu μ and σ > 0 \sigma > 0 σ > 0 :
z = μ + σ ⋅ ϵ ∼ N ( μ , σ 2 ) z = \mu + \sigma \cdot \epsilon \sim \mathcal{N}(\mu, \sigma^2) z = μ + σ ⋅ ϵ ∼ N ( μ , σ 2 )
Why this step? This is the location-scale property of the Gaussian. We're shifting (μ \mu μ ) and stretching (σ \sigma σ ) a standard normal.
Proof:
E [ z ] = E [ μ + σ ϵ ] = μ + σ ⋅ 0 = μ \mathbb{E}[z] = \mathbb{E}[\mu + \sigma\epsilon] = \mu + \sigma \cdot 0 = \mu E [ z ] = E [ μ + σ ϵ ] = μ + σ ⋅ 0 = μ ✓
Var ( z ) = Var ( μ + σ ϵ ) = σ 2 ⋅ Var ( ϵ ) = σ 2 ⋅ 1 = σ 2 \text{Var}(z) = \text{Var}(\mu + \sigma\epsilon) = \sigma^2 \cdot \text{Var}(\epsilon) = \sigma^2 \cdot 1 = \sigma^2 Var ( z ) = Var ( μ + σ ϵ ) = σ 2 ⋅ Var ( ϵ ) = σ 2 ⋅ 1 = σ 2 ✓
Now the critical rewrite:
Instead of:
z ∼ q ϕ ( z ∣ x ) = N ( μ ϕ ( x ) , σ ϕ 2 ( x ) ) z \sim q_\phi(z|x) = \mathcal{N}(\mu_\phi(x), \sigma_\phi^2(x)) z ∼ q ϕ ( z ∣ x ) = N ( μ ϕ ( x ) , σ ϕ 2 ( x ))
We write:
ϵ ∼ N ( 0 , 1 ) \epsilon \sim \mathcal{N}(0, 1) ϵ ∼ N ( 0 , 1 )
z = μ ϕ ( x ) + σ ϕ ( x ) ⋅ ϵ z = \mu_\phi(x) + \sigma_\phi(x) \cdot \epsilon z = μ ϕ ( x ) + σ ϕ ( x ) ⋅ ϵ
Why this step? Now z z z is a deterministic function of ϕ \phi ϕ (through μ ϕ \mu_\phi μ ϕ and σ ϕ \sigma_\phi σ ϕ ) and the random noise ϵ \epsilon ϵ . The randomness lives only in ϵ \epsilon ϵ , which doesn't depend on our parameters.
The loss typically looks like:
L ( ϕ ) = E q ϕ ( z ∣ x ) [ f ( z , x ) ] \mathcal{L}(\phi) = \mathbb{E}_{q_\phi(z|x)}[f(z, x)] L ( ϕ ) = E q ϕ ( z ∣ x ) [ f ( z , x )]
Old way (doesn't work):
∇ ϕ L = E q ϕ ( z ∣ x ) [ ∇ ϕ f ( z , x ) ] ← STUCK! Can’t compute ∇ ϕ z \nabla_\phi \mathcal{L} = \mathbb{E}_{q_\phi(z|x)}\left[\nabla_\phi f(z, x)\right] \quad \text{← STUCK! Can't compute } \nabla_\phi z ∇ ϕ L = E q ϕ ( z ∣ x ) [ ∇ ϕ f ( z , x ) ] ← STUCK! Can’t compute ∇ ϕ z
New way (reparameterized):
L ( ϕ ) = E p ( ϵ ) [ f ( g ϕ ( ϵ , x ) , x ) ] \mathcal{L}(\phi) = \mathbb{E}_{p(\epsilon)}[f(g_\phi(\epsilon, x), x)] L ( ϕ ) = E p ( ϵ ) [ f ( g ϕ ( ϵ , x ) , x )]
∇ ϕ L = E p ( ϵ ) [ ∇ ϕ f ( g ϕ ( ϵ , x ) , x ) ] \nabla_\phi \mathcal{L} = \mathbb{E}_{p(\epsilon)}\left[\nabla_\phi f(g_\phi(\epsilon, x), x)\right] ∇ ϕ L = E p ( ϵ ) [ ∇ ϕ f ( g ϕ ( ϵ , x ) , x ) ]
Why this step? The expectation is now over p ( ϵ ) p(\epsilon) p ( ϵ ) , which doesn't depend on ϕ \phi ϕ . We can swap the gradient and expectation:
= E p ( ϵ ) [ ∂ f ∂ z ⋅ ∂ g ϕ ∂ ϕ ] = \mathbb{E}_{p(\epsilon)}\left[\frac{\partial f}{\partial z} \cdot \frac{\partial g_\phi}{\partial \phi}\right] = E p ( ϵ ) [ ∂ z ∂ f ⋅ ∂ ϕ ∂ g ϕ ]
The chain rule works! ∂ g ϕ ∂ ϕ \frac{\partial g_\phi}{\partial \phi} ∂ ϕ ∂ g ϕ involves simple derivatives:
∂ ∂ μ ϕ ( μ ϕ + σ ϕ ϵ ) = 1 \frac{\partial}{\partial \mu_\phi}(\mu_\phi + \sigma_\phi \epsilon) = 1 ∂ μ ϕ ∂ ( μ ϕ + σ ϕ ϵ ) = 1
∂ ∂ σ ϕ ( μ ϕ + σ ϕ ϵ ) = ϵ \frac{\partial}{\partial \sigma_\phi}(\mu_\phi + \sigma_\phi \epsilon) = \epsilon ∂ σ ϕ ∂ ( μ ϕ + σ ϕ ϵ ) = ϵ
Worked example Worked Example 1: Single Sample Gradient
Setup: Encoder outputs μ = 2.0 \mu = 2.0 μ = 2.0 , σ = .5 \sigma = .5 σ = .5 . We sample ϵ = 1.3 \epsilon = 1.3 ϵ = 1.3 from N ( 0 , 1 ) \mathcal{N}(0,1) N ( 0 , 1 ) .
Loss function: f ( z ) = ( z − 3 ) 2 f(z) = (z - 3)^2 f ( z ) = ( z − 3 ) 2 (want z z z close to 3).
Step 1: Compute z z z via reparameterization.
z = μ + σ ⋅ ϵ = 2.0 + 0.5 × 1.3 = 2.65 z = \mu + \sigma \cdot \epsilon = 2.0 + 0.5 \times 1.3 = 2.65 z = μ + σ ⋅ ϵ = 2.0 + 0.5 × 1.3 = 2.65
Why this step? This gives us a concrete sample using our deterministic transform.
Step 2: Compute loss.
f ( z ) = ( 2.65 − 3 ) 2 = 0.1225 f(z) = (2.65 - 3)^2 = 0.1225 f ( z ) = ( 2.65 − 3 ) 2 = 0.1225
Step 3: Compute ∂ f ∂ z \frac{\partial f}{\partial z} ∂ z ∂ f (gradient of loss w.r.t. latent).
∂ f ∂ z = 2 ( z − 3 ) = 2 ( 2.65 − 3 ) = − 0.7 \frac{\partial f}{\partial z} = 2(z - 3) = 2(2.65 - 3) = -0.7 ∂ z ∂ f = 2 ( z − 3 ) = 2 ( 2.65 − 3 ) = − 0.7
Why this step? We need to know how f f f changes with z z z to apply the chain rule.
Step 4: Compute ∂ z ∂ μ \frac{\partial z}{\partial \mu} ∂ μ ∂ z (how latent changes with mean).
∂ z ∂ μ = ∂ ∂ μ ( μ + σ ϵ ) = 1 \frac{\partial z}{\partial \mu} = \frac{\partial}{\partial \mu}(\mu + \sigma \epsilon) = 1 ∂ μ ∂ z = ∂ μ ∂ ( μ + σ ϵ ) = 1
Why this step? The reparameterization makes z z z linearly dependent on μ \mu μ .
Step 5: Chain rule for μ \mu μ .
∂ f ∂ μ = ∂ f ∂ z ⋅ ∂ z ∂ μ = − 0.7 × 1 = − 0.7 \frac{\partial f}{\partial \mu} = \frac{\partial f}{\partial z} \cdot \frac{\partial z}{\partial \mu} = -0.7 \times 1 = -0.7 ∂ μ ∂ f = ∂ z ∂ f ⋅ ∂ μ ∂ z = − 0.7 × 1 = − 0.7
Interpretation: Increase μ \mu μ to increase z z z and reduce loss. Gradient descent will update: μ ← 2.0 − lr × ( − 0.7 ) = 2.0 + 0.7 × lr \mu \leftarrow 2.0 - \text{lr} \times (-0.7) = 2.0 + 0.7 \times \text{lr} μ ← 2.0 − lr × ( − 0.7 ) = 2.0 + 0.7 × lr . ✓
Step 6: Compute ∂ z ∂ σ \frac{\partial z}{\partial \sigma} ∂ σ ∂ z .
∂ z ∂ σ = ∂ ∂ σ ( μ + σ ϵ ) = ϵ = 1.3 \frac{\partial z}{\partial \sigma} = \frac{\partial}{\partial \sigma}(\mu + \sigma \epsilon) = \epsilon = 1.3 ∂ σ ∂ z = ∂ σ ∂ ( μ + σ ϵ ) = ϵ = 1.3
Why this step? The noise ϵ \epsilon ϵ acts as the sensitivity factor for σ \sigma σ .
Step 7: Chain rule for σ \sigma σ .
∂ f ∂ σ = ∂ f ∂ z ⋅ ∂ z ∂ σ = − 0.7 × 1.3 = − 0.91 \frac{\partial f}{\partial \sigma} = \frac{\partial f}{\partial z} \cdot \frac{\partial z}{\partial \sigma} = -0.7 \times 1.3 = -0.91 ∂ σ ∂ f = ∂ z ∂ f ⋅ ∂ σ ∂ z = − 0.7 × 1.3 = − 0.91
Interpretation: Increasing σ \sigma σ (with positive ϵ \epsilon ϵ ) increases z z z , reducing loss. ✓
Worked example Worked Example 2: Monte Carlo Estimation
Setup: We don't have analytic expectation, so we approximate with S S S samples.
∇ ϕ L = E p ( ϵ ) [ ∇ ϕ f ( g ϕ ( ϵ , x ) , x ) ] ≈ 1 S ∑ s = 1 S ∇ ϕ f ( g ϕ ( ϵ ( s ) , x ) , x ) \nabla_\phi \mathcal{L} = \mathbb{E}_{p(\epsilon)}\left[\nabla_\phi f(g_\phi(\epsilon, x), x)\right] \approx \frac{1}{S}\sum_{s=1}^S \nabla_\phi f(g_\phi(\epsilon^{(s)}, x), x) ∇ ϕ L = E p ( ϵ ) [ ∇ ϕ f ( g ϕ ( ϵ , x ) , x ) ] ≈ S 1 ∑ s = 1 S ∇ ϕ f ( g ϕ ( ϵ ( s ) , x ) , x )
Step 1: Draw S S S independent samples ϵ ( 1 ) , … , ϵ ( S ) ∼ N ( 0 , 1 ) \epsilon^{(1)}, \ldots, \epsilon^{(S)} \sim \mathcal{N}(0, 1) ϵ ( 1 ) , … , ϵ ( S ) ∼ N ( 0 , 1 ) .
Why this step? Monte Carlo integration—we're approximating the expectation by sampling.
Step 2: For each sample s s s , compute:
z ( s ) = μ ϕ ( x ) + σ ϕ ( x ) ⋅ ϵ ( s ) z^{(s)} = \mu_\phi(x) + \sigma_\phi(x) \cdot \epsilon^{(s)} z ( s ) = μ ϕ ( x ) + σ ϕ ( x ) ⋅ ϵ ( s )
Step 3: Compute individual gradients:
g ( s ) = ∇ ϕ f ( z ( s ) , x ) g^{(s)} = \nabla_\phi f(z^{(s)}, x) g ( s ) = ∇ ϕ f ( z ( s ) , x )
Step 4: Average:
g ^ = 1 S ∑ s = 1 S g ( s ) \hat{g} = \frac{1}{S}\sum_{s=1}^S g^{(s)} g ^ = S 1 ∑ s = 1 S g ( s )
Why this step? The law of large numbers: as S → ∞ S \to \infty S → ∞ , g ^ → E [ g ] \hat{g} \to \mathbb{E}[g] g ^ → E [ g ] . Even small S S S (like 1-10) works well in practice due to the smoothness of neural network losses.
Practical VAE: Often S = 1 S=1 S = 1 (single sample) per training step! The stochasticity across mini-batches provides enough gradient signal.
Worked example Worked Example 3: Multivariate Gaussian
Setup: Latent is d d d -dimensional with diagonal covariance.
q ϕ ( z ∣ x ) = N ( μ ϕ ( x ) , diag ( σ ϕ 2 ( x ) ) ) q_\phi(z|x) = \mathcal{N}(\mu_\phi(x), \text{diag}(\sigma_\phi^2(x))) q ϕ ( z ∣ x ) = N ( μ ϕ ( x ) , diag ( σ ϕ 2 ( x )))
Reparameterization:
ϵ ∼ N ( 0 , I d ) (d independent standard normals) \epsilon \sim \mathcal{N}(0, I_d) \quad \text{(d independent standard normals)} ϵ ∼ N ( 0 , I d ) (d independent standard normals)
z i = μ ϕ , i ( x ) + σ ϕ , i ( x ) ⋅ ϵ i , i = 1 , … , d z_i = \mu_{\phi,i}(x) + \sigma_{\phi,i}(x) \cdot \epsilon_i, \quad i=1,\ldots,d z i = μ ϕ , i ( x ) + σ ϕ , i ( x ) ⋅ ϵ i , i = 1 , … , d
Why this step? With diagonal covariance, each dimension is independent. We just apply the trick element-wise .
In vector form:
z = μ ϕ ( x ) + σ ϕ ( x ) ⊙ ϵ z = \mu_\phi(x) + \sigma_\phi(x) \odot \epsilon z = μ ϕ ( x ) + σ ϕ ( x ) ⊙ ϵ
where ⊙ \odot ⊙ is element-wise multiplication (Hadamard product).
Gradient: Each ∂ z i ∂ μ ϕ , j = δ i j \frac{\partial z_i}{\partial \mu_{\phi,j}} = \delta_{ij} ∂ μ ϕ , j ∂ z i = δ ij and ∂ z i ∂ σ ϕ , j = ϵ i δ i j \frac{\partial z_i}{\partial \sigma_{\phi,j}} = \epsilon_i \delta_{ij} ∂ σ ϕ , j ∂ z i = ϵ i δ ij . The Jacobian is diagonal, so backprop is efficient!
Proof sketch:
Change of variables. If z = μ + σ ϵ z = \mu + \sigma \epsilon z = μ + σ ϵ , then d z = σ d ϵ dz = \sigma \, d\epsilon d z = σ d ϵ , and:
p z ( z ) d z = p ϵ ( z − μ σ ) d z σ p_z(z) dz = p_\epsilon\left(\frac{z-\mu}{\sigma}\right) \frac{dz}{\sigma} p z ( z ) d z = p ϵ ( σ z − μ ) σ d z
The distributions match by construction. The expectations are equal.
The gradient trick works because:
The expectation over ϵ \epsilon ϵ doesn't depend on ϕ \phi ϕ
Leibniz integral rule: ∇ ϕ ∫ p ( ϵ ) f ( g ϕ ( ϵ ) ) d ϵ = ∫ p ( ϵ ) ∇ ϕ f ( g ϕ ( ϵ ) ) d ϵ \nabla_\phi \int p(\epsilon) f(g_\phi(\epsilon)) d\epsilon = \int p(\epsilon) \nabla_\phi f(g_\phi(\epsilon)) d\epsilon ∇ ϕ ∫ p ( ϵ ) f ( g ϕ ( ϵ )) d ϵ = ∫ p ( ϵ ) ∇ ϕ f ( g ϕ ( ϵ )) d ϵ
We can push the gradient inside the expectation
# PyTorch pseudocode
def reparameterize (mu, log_var):
"""
mu: [batch, latent_dim] - mean from encoder
log_var: [batch, latent_dim] - log variance (for numerical stability)
"""
std = torch.exp( 0.5 * log_var) # sigma = exp(log_var / 2)
eps = torch.randn_like(std) # sample epsilon ~ N(0, 1)
z = mu + std * eps # reparameterization
return z
Mnemonic "Noise Outside, Parameters Inside"
R andom noise lives outside (fixed distribution).
P arameters live inside (the transform).
Gradient flows through the transform , not the noise.
Remember: "Separate the sauce from the sausage." The randomness (sauce) is separate from the learned parameters (sausage ingredients). You optimize the sausage recipe, not the randomness of how you stir.
Common mistake Mistake 1: Forgetting to Detach
ϵ \epsilon ϵ
Wrong code:
eps = torch.randn_like(std)
eps.requires_grad = True # DON'T DO THIS
z = mu + std * eps
Why it feels right: "Everything in a neural network needs gradients, right?"
Why it's wrong: ϵ \epsilon ϵ is sampled from a fixed distribution . Gradients w.r.t. ϵ \epsilon ϵ are meaningless—we're not optimizing the random seed! Setting requires_grad=True wastes computation and can cause bugs.
The fix: Keep ϵ \epsilon ϵ as-is. torch.randn_like() creates a tensor with requires_grad=False by default. The gradient will automatically flow through mu and std via the addition and multiplication operations.
Common mistake Mistake 2: Using
σ 2 \sigma^2 σ 2 Instead of σ \sigma σ
Wrong:
var = torch.exp(log_var)
z = mu + var * eps # multiplying by variance, not std!
Why it feels right: The encoder outputs variance, so just use it directly.
Why it's wrong: The reparameterization formula is z = μ + σ ϵ z = \mu + \sigma \epsilon z = μ + σ ϵ , not z = μ + σ 2 ϵ z = \mu + \sigma^2 \epsilon z = μ + σ 2 ϵ . Using variance instead of standard deviation scales the noise incorrectly . If σ = 0.5 \sigma = 0.5 σ = 0.5 , then σ 2 = 0.25 \sigma^2 = 0.25 σ 2 = 0.25 —you'd be underestimating the spread by half!
The fix: Take the square root. Since we typically parameterize with log variance (for numerical stability):
σ = exp ( 1 2 log σ 2 ) = exp ( 0.5 × log_var ) \sigma = \exp(\frac{1}{2}\log \sigma^2) = \exp(0.5 \times \text{log\_var}) σ = exp ( 2 1 log σ 2 ) = exp ( 0.5 × log_var )
Common mistake Mistake 3: Applying to Discrete Distributions
Scenario: You try to reparameterize a categorical distribution (e.g., for discrete VAEs).
Why it feels right: "If it works for continuous distributions, it should work for discrete ones too!"
Why it's wrong: Reparameterization requires a differentiable transform . Discrete sampling (argmax, categorical) has no gradient—small changes in probabilities don't smoothly change the sampled class.
The fix: For discrete latents, use alternative gradient estimators:
Gumbel-Softmax (continuous relaxation)
REINFORCE (score function estimator)
Straight-through estimators
Each has tradeoffs. Gumbel-Softmax is closest in spirit to reparameterization.
The trick extends beyond Gaussians:
Distribution
Reparameterization
Gaussian
z = μ + σ ϵ z = \mu + \sigma \epsilon z = μ + σ ϵ , ϵ ∼ N ( 0 , 1 ) \epsilon \sim \mathcal{N}(0,1) ϵ ∼ N ( 0 , 1 )
Exponential
z = − 1 λ log u z = -\frac{1}{\lambda}\log u z = − λ 1 log u , u ∼ Uniform ( 0 , 1 ) u \sim \text{Uniform}(0,1) u ∼ Uniform ( 0 , 1 )
Logistic
z = μ + s log u 1 − u z = \mu + s \log\frac{u}{1-u} z = μ + s log 1 − u u , u ∼ Uniform ( 0 , 1 ) u \sim \text{Uniform}(0,1) u ∼ Uniform ( 0 , 1 )
Laplace
$z = \mu - b \cdot \text{sgn}(u-0.5) \log(1-2
Key requirement: The distribution must have a location-scale form or an invertible CDF (inverse transform sampling).
Recall Explain to a 12-Year-Old
Imagine you're trying to teach a robot to draw. The robot has a "creativity knob" (that's σ \sigma σ ) and a "center point" knob (that's μ \mu μ ). Every time it draws, it picks a random direction and distance to move from the center.
The problem: if the drawing is too wobbly (random), the robot can't learn which knob settings make good art. It's like trying to improve your basketball shot while someone randomly pushes your arm—you can't tell if it was your aim or the push!
The reparameterization trick is like this: instead of the robot randomly picking both direction AND distance (too random to learn from), we give it a standard random step (like always rolling a regular die), and then the robot scales that step with its knobs. Now if the drawing is bad, the robot knows it was the knob settings, not the randomness.
The knobs control a transform of a fixed random step. That transform is smooth and learnable!
4.5.01-Variational-Autoencoders - the primary application
4.5.02-ELBO-Derivation - where this trick enables gradient-based optimization
4.3.01-KL-Divergence - the KL term in ELBO has a closed form Gaussians
5.2.03-Gumbel-Softmax - the discrete analog
2.4.01-Backpropagation - why gradients need differentiable paths
6.1.02-Policy-Gradients - contrast with REINFORCE (score function method)
#flashcards/ai-ml
What is the core problem that the reparameterization trick solves? :: In VAEs, we need to backpropagate through a stochastic sampling node z ∼ q ϕ ( z ∣ x ) z \sim q_\phi(z|x) z ∼ q ϕ ( z ∣ x ) , but sampling is non-differentiable w.r.t. the distribution parameters ϕ \phi ϕ . Gradients cannot flow through randomness.
What is the reparameterization trick for a Gaussian distribution? Instead of sampling
z ∼ N ( μ , σ 2 ) z \sim \mathcal{N}(\mu, \sigma^2) z ∼ N ( μ , σ 2 ) directly, sample
ϵ ∼ N ( 0 , 1 ) \epsilon \sim \mathcal{N}(0, 1) ϵ ∼ N ( 0 , 1 ) from a fixed distribution, then compute
z = μ + σ ⋅ ϵ z = \mu + \sigma \cdot \epsilon z = μ + σ ⋅ ϵ . This makes
z z z a differentiable function of
μ \mu μ and
σ \sigma σ .
Why can we move the gradient inside the expectation after reparameterization? After reparameterization, the expectation is over
p ( ϵ ) p(\epsilon) p ( ϵ ) which doesn't depend on parameters
ϕ \phi ϕ :
∇ ϕ E p ( ϵ ) [ f ( g ϕ ( ϵ ) ] = E p ( ϵ ) [ ∇ ϕ f ( g ϕ ( ϵ ) ) ] \nabla_\phi \mathbb{E}_{p(\epsilon)}[f(g_\phi(\epsilon)] = \mathbb{E}_{p(\epsilon)}[\nabla_\phi f(g_\phi(\epsilon))] ∇ ϕ E p ( ϵ ) [ f ( g ϕ ( ϵ )] = E p ( ϵ ) [ ∇ ϕ f ( g ϕ ( ϵ ))] . The Leibniz integral rule allows swapping gradient and integral when the integration domain is parameter-free.
What is ∂ z ∂ μ \frac{\partial z}{\partial \mu} ∂ μ ∂ z when z = μ + σ ϵ z = \mu + \sigma \epsilon z = μ + σ ϵ ? ∂ z ∂ μ = 1 \frac{\partial z}{\partial \mu} = 1 ∂ μ ∂ z = 1 (a constant). This means changes in
μ \mu μ directly shift
z z z by the same amount.
What is ∂ z ∂ σ \frac{\partial z}{\partial \sigma} ∂ σ ∂ z when z = μ + σ ϵ z = \mu + \sigma \epsilon z = μ + σ ϵ ? ∂ z ∂ σ = ϵ \frac{\partial z}{\partial \sigma} = \epsilon ∂ σ ∂ z = ϵ . The gradient depends on the sampled noise value. When
ϵ \epsilon ϵ is large,
σ \sigma σ has more influence on
z z z .
In practice, how many samples S S S do VAEs use for the reparameterization gradient estimator? Often just
S = 1 S=1 S = 1 (single sample per training step). The mini-batch stochasticity across many training steps provides sufficient gradient signal despite the variance of a single-sample estimator.
Why do we typically parameterize with log-variance instead of variance? Numerical stability. Variance must be positive, but log-variance can be any real number. Computing
σ = exp ( 0.5 ⋅ log_var ) \sigma = \exp(0.5 \cdot \text{log\_var}) σ = exp ( 0.5 ⋅ log_var ) ensures
σ > 0 \sigma > 0 σ > 0 and avoids optimization constraints.
What is the correct formula to get σ \sigma σ from log-variance? σ = exp ( 0.5 ⋅ log σ 2 ) = exp ( 1 2 log_var ) \sigma = \exp(0.5 \cdot \log\sigma^2) = \exp(\frac{1}{2} \text{log\_var}) σ = exp ( 0.5 ⋅ log σ 2 ) = exp ( 2 1 log_var ) , because
log σ = 1 2 log σ 2 \log \sigma = \frac{1}{2}\log\sigma^2 log σ = 2 1 log σ 2 .
Can the reparameterization trick be applied to discrete distributions like Categorical? Not directly. Discrete sampling has no gradient (argmax is non-differentiable). Alternatives include Gumbel-Softmax (continuous relaxation) or REINFORCE (score function estimator).
For a multivariate Gaussian with diagonal covariance, how does reparameterization work? Apply element-wise:
ϵ ∼ N ( 0 , I d ) \epsilon \sim \mathcal{N}(0, I_d) ϵ ∼ N ( 0 , I d ) and
z = μ + σ ⊙ ϵ z = \mu + \sigma \odot \epsilon z = μ + σ ⊙ ϵ where
⊙ \odot ⊙ is element-wise multiplication. Each dimension is independent.
Backprop through sampling
Intuition Hinglish mein samjho
Dekho, VAE jaise generative models train karte waqt ek bada problem ata hai: humein encoder se nikle hue random sample z z z ke through backpropagation karna padta hai