TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement KL Divergence

Loss Functions
Medium

Compute the Kullback-Leibler divergence from distribution PPP to distribution QQQ:

DKL(P ∥ Q)=∑iPilog⁡(PiQi)D_{\mathrm{KL}}(P\,\|\,Q) = \sum_i P_i\log\left(\frac{P_i}{Q_i}\right)DKL​(P∥Q)=i∑​Pi​log(Qi​Pi​​)

Here, PiP_iPi​ and QiQ_iQi​ are corresponding probabilities. A term with Pi=0P_i=0Pi​=0 contributes zero. Clamp positive-indexed QiQ_iQi​ values to at least eps before division, then return the sum as a Python float.

Loading visualization...

Examples

Input: p = [0.4, 0.6], q = [0.5, 0.5], eps = 1e-12

Output: 0.020136

Explanation: The distributions are similar but not identical, so the divergence is small and positive.

Input: p = [0.3, 0.7], q = [0.3, 0.7], eps = 1e-12

Output: 0.0

Input: p = [0.9, 0.1], q = [0.5, 0.5], eps = 1e-12

Output: 0.368064

Hint 1

Create a mask with positive = p > 0.

Hint 2

Use np.clip(q[positive], eps, None) before computing the log ratio.

Requirements

  • Ignore terms where p is zero
  • Clamp the corresponding q values to at least eps
  • Compute all positive-p terms with NumPy
  • Return the divergence as a Python float

Constraints

  • p and q are equal-length probability distributions
  • Both distributions contain nonnegative values and sum to 1
  • eps is positive
  • Use NumPy only
Try Similar Problems
Cross Entropy LossSoftmax FunctionLabel Smoothing LossInfo Nce LossFocal Loss

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Implement KL Divergence

Loss Functions
Medium

Compute the Kullback-Leibler divergence from distribution PPP to distribution QQQ:

DKL(P ∥ Q)=∑iPilog⁡(PiQi)D_{\mathrm{KL}}(P\,\|\,Q) = \sum_i P_i\log\left(\frac{P_i}{Q_i}\right)DKL​(P∥Q)=i∑​Pi​log(Qi​Pi​​)

Here, PiP_iPi​ and QiQ_iQi​ are corresponding probabilities. A term with Pi=0P_i=0Pi​=0 contributes zero. Clamp positive-indexed QiQ_iQi​ values to at least eps before division, then return the sum as a Python float.

Loading visualization...

Examples

Input: p = [0.4, 0.6], q = [0.5, 0.5], eps = 1e-12

Output: 0.020136

Explanation: The distributions are similar but not identical, so the divergence is small and positive.

Input: p = [0.3, 0.7], q = [0.3, 0.7], eps = 1e-12

Output: 0.0

Input: p = [0.9, 0.1], q = [0.5, 0.5], eps = 1e-12

Output: 0.368064

Hint 1

Create a mask with positive = p > 0.

Hint 2

Use np.clip(q[positive], eps, None) before computing the log ratio.

Requirements

  • Ignore terms where p is zero
  • Clamp the corresponding q values to at least eps
  • Compute all positive-p terms with NumPy
  • Return the divergence as a Python float

Constraints

  • p and q are equal-length probability distributions
  • Both distributions contain nonnegative values and sum to 1
  • eps is positive
  • Use NumPy only
Try Similar Problems
Cross Entropy LossSoftmax FunctionLabel Smoothing LossInfo Nce LossFocal Loss

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.