PIXELBANKv9.1.0
Menu

VAE Reparameterization Trick

Implement the reparameterization trick used in Variational Autoencoders.

The encoder outputs mean μ\mu and log-variance log⁡σ2\log\sigma^2 for each latent dimension. To sample zz in a differentiable way:

  1. Compute standard deviation: σ=elog⁡σ22\sigma = e^{\frac{\log\sigma^2}{2}}
  2. Sample: z=μ+σ⋅ϵz = \mu + \sigma \cdot \epsilon

where ϵ\epsilon is a random sample from N(0,1)\mathcal{N}(0, 1).

Given μ\mu, log⁡σ2\log\sigma^2, and pre-generated ϵ\epsilon values, compute the latent sample zz.

Return zz as a list rounded to 4 decimal places.

Example:

Input:
mu = [0.5, -0.3]
log_var = [0.0, 0.0]
epsilon = [1.0, -1.0]
Output:
[1.5, -1.3]
Reasoning:
  • First, we compute the standard deviation σ\sigma using the given log⁡σ2\log\sigma^2 values: σ=elog⁡σ22=e0.02=e0=1\sigma = e^{\frac{\log\sigma^2}{2}} = e^{\frac{0.0}{2}} = e^{0} = 1 for both dimensions.
  • Then, we calculate the latent sample zz by adding the product of σ\sigma and ϵ\epsilon to μ\mu: z=μ+σ⋅ϵz = \mu + \sigma \cdot \epsilon. For the first dimension, z=0.5+1⋅1.0=1.5z = 0.5 + 1 \cdot 1.0 = 1.5, and for the second dimension, z=−0.3+1⋅−1.0=−1.3z = -0.3 + 1 \cdot -1.0 = -1.3.
  • The final output is the list of calculated zz values rounded to 4 decimal places: [1.5,−1.3][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
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.
VAE Reparameterization Trick - Medium | PixelBank