VAE Reparameterization Trick
Implement the reparameterization trick used in Variational Autoencoders.
The encoder outputs mean and log-variance for each latent dimension. To sample in a differentiable way:
- Compute standard deviation:
- Sample:
where is a random sample from .
Given , , and pre-generated values, compute the latent sample .
Return as a list rounded to 4 decimal places.
Example
mu = [0.5, -0.3] log_var = [0.0, 0.0] epsilon = [1.0, -1.0]
[1.5, -1.3]
- Inputs: each list has one entry per latent dimension, here 2.
muholds the encoder's means ,log_varholds , andepsilonholds noise already drawn from : . - Spread: a log-variance of means , so in both dimensions. The noise is passed through unscaled.
- Shift and scale: dimension 1 gives ; dimension 2 gives .
- Output:
[1.5, -1.3], one per dimension, same length as the inputs. Both values already have fewer than 4 decimal places, so rounding leaves them as is. - Notice: is a deterministic function of and once is fixed, which is why gradients can flow through it. With the spread becomes , so , gives .
Constraints
- mu: 1D list of means
- log_var: 1D list of log-variances
- epsilon: 1D list of random normal samples (same length)
- Return 1D list of z values rounded to 4 decimal places