TensorTonic
Study Plans
Study Plan
Diagonal Gaussian KL Divergence
Diffusion ModelsProbabilityGenerative Models
Easy

Compute the KL divergence KL(p ∥ q)\mathrm{KL}(p \,\|\, q)KL(p∥q) between two diagonal Gaussians for every row of a batch, summing over the last axis.

KL(p ∥ q)=12∑d=1D(log⁡σq,d2−log⁡σp,d2+σp,d2+(μp,d−μq,d)2σq,d2−1)\mathrm{KL}(p \,\|\, q) = \frac{1}{2} \sum_{d=1}^{D} \left( \log \sigma_{q,d}^2 - \log \sigma_{p,d}^2 + \frac{\sigma_{p,d}^2 + (\mu_{p,d} - \mu_{q,d})^2}{\sigma_{q,d}^2} - 1 \right)KL(p∥q)=21​d=1∑D​(logσq,d2​−logσp,d2​+σq,d2​σp,d2​+(μp,d​−μq,d​)2​−1)

Here mean_p and log_var_p hold the means μp\mu_pμp​ and log variances log⁡σp2\log \sigma_p^2logσp2​ of the first Gaussian ppp, and mean_q and log_var_q hold those of the second Gaussian qqq. All four arrays have the same shape (..., D), where the last axis indexes the D independent coordinates ddd of one Gaussian and every leading index describes a separate pair of Gaussians. The variances are σ2=exp⁡(log⁡σ2)\sigma^2 = \exp(\log \sigma^2)σ2=exp(logσ2). The divergence is measured from ppp to qqq, and swapping the two Gaussians gives a different value in general.

Return a NumPy array of dtype float64 with the leading shape of the inputs, so inputs of shape (N, D) give shape (N,).

Examples

Input: mean_p = [[1.0,0.0],[0.0,0.0]], log_var_p = [[0.0,0.0],[0.0,0.0]], mean_q = [[0.0,0.0],[0.0,0.0]], log_var_q = [[0.0,0.0],[1.3863,1.3863]]

Output: [0.5,0.636299]

Explanation: The first row compares N(1, 1) with N(0, 1) in its first coordinate, which contributes half the squared mean gap, 0.5. The second row compares unit variances with variances of about 4, which gives about 0.636.

Input: mean_p = [[0.5],[-2.0],[3.0]], log_var_p = [[-1.0],[0.0],[2.0]], mean_q = [[0.5],[-2.0],[3.0]], log_var_q = [[-1.0],[0.0],[2.0]]

Output: [0.0,0.0,0.0]

Input: mean_p = [[[0.0,1.0,-1.0]],[[2.0,0.0,0.5]]], log_var_p = [[[-0.6931,0.0,0.6931]],[[0.0,-2.0,0.0]]], mean_q = [[[0.0,0.0,0.0]],[[1.0,0.0,0.0]]], log_var_q = [[[0.0,0.0,0.0]],[[0.0,0.0,-1.0]]]

Output: [[1.249965],[1.766594]]

Hint 1

np.exp(log_var) recovers each variance from its log.

Hint 2

The ratio of two variances equals the exponential of the difference of their log variances.

Hint 3

np.sum(x, axis=-1) reduces only the coordinate axis, so shape (N, D) becomes (N,).

Requirements

  • Use NumPy array operations.
  • Leave the inputs unchanged.

Constraints

  • mean_p, log_var_p, mean_q and log_var_q are float64 NumPy arrays with the same shape (..., D).
  • The arrays have at least two dimensions, and every dimension has size at least 1.
  • Log variances lie in [-8, 8], so every variance is positive and finite.
  • All values are finite.
Try Similar Problems
Reparameterized Gaussian SampleEasyPosterior Mean and VarianceMediumGaussian Mixture ScoreMediumForward Diffusion SampleEasyNoise Prediction LossEasyPrediction ParameterizationMediumDDPM Ancestral StepMediumDDIM StepHardToy DDPM SamplerHardAutoencoder Forward PassMediumVariational Autoencoder Forward PassMediumGAN Training StepHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Diagonal Gaussian KL Divergence

Diffusion ModelsProbabilityGenerative Models
Easy

Compute the KL divergence KL(p ∥ q)\mathrm{KL}(p \,\|\, q)KL(p∥q) between two diagonal Gaussians for every row of a batch, summing over the last axis.

KL(p ∥ q)=12∑d=1D(log⁡σq,d2−log⁡σp,d2+σp,d2+(μp,d−μq,d)2σq,d2−1)\mathrm{KL}(p \,\|\, q) = \frac{1}{2} \sum_{d=1}^{D} \left( \log \sigma_{q,d}^2 - \log \sigma_{p,d}^2 + \frac{\sigma_{p,d}^2 + (\mu_{p,d} - \mu_{q,d})^2}{\sigma_{q,d}^2} - 1 \right)KL(p∥q)=21​d=1∑D​(logσq,d2​−logσp,d2​+σq,d2​σp,d2​+(μp,d​−μq,d​)2​−1)

Here mean_p and log_var_p hold the means μp\mu_pμp​ and log variances log⁡σp2\log \sigma_p^2logσp2​ of the first Gaussian ppp, and mean_q and log_var_q hold those of the second Gaussian qqq. All four arrays have the same shape (..., D), where the last axis indexes the D independent coordinates ddd of one Gaussian and every leading index describes a separate pair of Gaussians. The variances are σ2=exp⁡(log⁡σ2)\sigma^2 = \exp(\log \sigma^2)σ2=exp(logσ2). The divergence is measured from ppp to qqq, and swapping the two Gaussians gives a different value in general.

Return a NumPy array of dtype float64 with the leading shape of the inputs, so inputs of shape (N, D) give shape (N,).

Examples

Input: mean_p = [[1.0,0.0],[0.0,0.0]], log_var_p = [[0.0,0.0],[0.0,0.0]], mean_q = [[0.0,0.0],[0.0,0.0]], log_var_q = [[0.0,0.0],[1.3863,1.3863]]

Output: [0.5,0.636299]

Explanation: The first row compares N(1, 1) with N(0, 1) in its first coordinate, which contributes half the squared mean gap, 0.5. The second row compares unit variances with variances of about 4, which gives about 0.636.

Input: mean_p = [[0.5],[-2.0],[3.0]], log_var_p = [[-1.0],[0.0],[2.0]], mean_q = [[0.5],[-2.0],[3.0]], log_var_q = [[-1.0],[0.0],[2.0]]

Output: [0.0,0.0,0.0]

Input: mean_p = [[[0.0,1.0,-1.0]],[[2.0,0.0,0.5]]], log_var_p = [[[-0.6931,0.0,0.6931]],[[0.0,-2.0,0.0]]], mean_q = [[[0.0,0.0,0.0]],[[1.0,0.0,0.0]]], log_var_q = [[[0.0,0.0,0.0]],[[0.0,0.0,-1.0]]]

Output: [[1.249965],[1.766594]]

Hint 1

np.exp(log_var) recovers each variance from its log.

Hint 2

The ratio of two variances equals the exponential of the difference of their log variances.

Hint 3

np.sum(x, axis=-1) reduces only the coordinate axis, so shape (N, D) becomes (N,).

Requirements

  • Use NumPy array operations.
  • Leave the inputs unchanged.

Constraints

  • mean_p, log_var_p, mean_q and log_var_q are float64 NumPy arrays with the same shape (..., D).
  • The arrays have at least two dimensions, and every dimension has size at least 1.
  • Log variances lie in [-8, 8], so every variance is positive and finite.
  • All values are finite.
Try Similar Problems
Reparameterized Gaussian SampleEasyPosterior Mean and VarianceMediumGaussian Mixture ScoreMediumForward Diffusion SampleEasyNoise Prediction LossEasyPrediction ParameterizationMediumDDPM Ancestral StepMediumDDIM StepHardToy DDPM SamplerHardAutoencoder Forward PassMediumVariational Autoencoder Forward PassMediumGAN Training StepHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.