Compute the Kullback-Leibler divergence between a diagonal Gaussian posterior and the standard-normal prior used by a variational autoencoder.
DKL(q(z∣x)∥N(0,I))=−2B1i=1∑Bj=1∑L(1+logσij2−μij2−exp(logσij2))Here, B is batch size, L is latent width, μij is a posterior mean, and logσij2 is its log-variance. Sum over latent coordinates, then average over the batch. Return the divergence as a Python float.
Examples
Input: mu = [[0,0]], log_var = [[0,0]]
Output: 0
Explanation: A zero mean and zero log-variance describe the standard normal, so the divergence is zero.
Input: mu = [[1,2]], log_var = [[0,0]]
Output: 2.5
Input: mu = [[0.5,-0.25],[1,0]], log_var = [[-1,0.5],[0.2,-0.4]]
Output: 0.480206
Hint 1
Recover variance with np.exp(log_var).
Hint 2
Reduce the latent axis before averaging the batch.
Requirements
- Use NumPy.
- Apply the closed-form diagonal-Gaussian KL expression.
- Sum over latent coordinates and average over samples.
- Return a finite Python float.
Constraints
- mu and log_var have the same shape (B, L).
- Both arrays have dtype float64 and contain finite values.
- B and L are positive.
All steps in VAE
Related practice
Accepts: array
Accepts: array
KL Divergence
Compute the Kullback-Leibler divergence between a diagonal Gaussian posterior and the standard-normal prior used by a variational autoencoder.
DKL(q(z∣x)∥N(0,I))=−2B1i=1∑Bj=1∑L(1+logσij2−μij2−exp(logσij2))Here, B is batch size, L is latent width, μij is a posterior mean, and logσij2 is its log-variance. Sum over latent coordinates, then average over the batch. Return the divergence as a Python float.
Examples
Input: mu = [[0,0]], log_var = [[0,0]]
Output: 0
Explanation: A zero mean and zero log-variance describe the standard normal, so the divergence is zero.
Input: mu = [[1,2]], log_var = [[0,0]]
Output: 2.5
Input: mu = [[0.5,-0.25],[1,0]], log_var = [[-1,0.5],[0.2,-0.4]]
Output: 0.480206
Hint 1
Recover variance with np.exp(log_var).
Hint 2
Reduce the latent axis before averaging the batch.
Requirements
- Use NumPy.
- Apply the closed-form diagonal-Gaussian KL expression.
- Sum over latent coordinates and average over samples.
- Return a finite Python float.
Constraints
- mu and log_var have the same shape (B, L).
- Both arrays have dtype float64 and contain finite values.
- B and L are positive.
All steps in VAE
Related practice
Accepts: array
Accepts: array