VAE Reparameterization Trick
Implement the reparameterization trick used in Variational Autoencoders.
The encoder outputs mean μ and log-variance logσ2 for each latent dimension. To sample z in a differentiable way:
- Compute standard deviation: σ=e2logσ2
- Sample: z=μ+σ⋅ϵ
where ϵ is a random sample from N(0,1).
Given μ, logσ2, and pre-generated ϵ values, compute the latent sample z.
Return z 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]
- First, we compute the standard deviation σ using the given logσ2 values: σ=e2logσ2=e20.0=e0=1 for both dimensions.
- Then, we calculate the latent sample z by adding the product of σ and ϵ to μ: z=μ+σ⋅ϵ. For the first dimension, z=0.5+1⋅1.0=1.5, and for the second dimension, z=−0.3+1⋅−1.0=−1.3.
- The final output is the list of calculated z values rounded to 4 decimal places: [1.5,−1.3].
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
Background Knowledge
The reparameterization trick is a key component in Variational Autoencoders (VAEs), which are a type of generative model. VAEs consist of an encoder and a decoder. The encoder maps the input to a probabilistic latent space, while the decoder maps the latent space back to the input space. The reparameterization trick allows for backpropagation through the encoder by making the sampling process differentiable.
In VAEs, the encoder outputs the mean (μ) and log-variance (logσ2) of a Gaussian distribution for each latent dimension. To sample from this distribution, we need to use the reparameterization trick, which involves computing the standard deviation (σ) and then sampling from a standard normal distribution (N(0,1)). This trick enables us to backpropagate through the sampling process, allowing us to train the VAE using backpropagation and gradient descent.
The reparameterization trick is essential in VAEs because it allows us to learn a continuous and structured representation of the data. By using a probabilistic latent space, VAEs can generate new samples that are similar to the training data. The reparameterization trick is a crucial step in this process, as it enables us to sample from the latent space in a differentiable way.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.