Skip to content

Buyer's Guide

A KL Comparison Needs Its Reference Distribution and Direction

In D(P||Q), P supplies the probability weights and Q supplies the comparison probabilities. Reversing the direction changes both roles, so the same pair can produce different KL values. For a finite categorical population, the requested direction is finite exactly when the comparison assigns positive probability everywhere its reference has positive mass. A usable report therefore identifies the reference, comparison, shared event mapping and support, alongside the logarithm and reduction.

Technical contentPublished Updated

01

The reference determines which events weight the comparison

A report describing KL between two distributions leaves a consequential choice unresolved: which distribution supplies the expectation? For the finite categorical comparison here, D(P||Q) = sum over events i of P_i × ln(P_i / Q_i). Equivalently, it is the expected log-ratio when events are weighted by P. The reference is P; the distribution being compared against it is Q.

The same event must occupy the same position in both distributions. If position C names one class in P and another in Q, their ratio does not describe the intended event comparison. The related softmax guide addresses obtaining a unit-sum distribution on the intended axis. Here both supplied distributions are already normalized over one shared, declared event population.

Calling P the reference identifies its mathematical role. It does not establish that P is the true population distribution or that its events represent the buyer's application. Those claims need evidence about where the supplied probabilities came from.

The examples use natural logarithms, so their units are nats, and sum all class contributions for one distribution comparison. They are original exact-arithmetic illustrations using stipulated probabilities, without a model run or measured evaluation outcome.

References: [SC1]

02

KL64: follow the P weights through all three events

KL64 supplies exactly three ordered events A, B and C. Let P = (1/2, 3/8, 1/8) and Q = (1/4, 1/4, 1/2). Both distributions sum to one and have positive probability at every event.

For D(P||Q), event A contributes (1/2) × ln((1/2)/(1/4)) = (1/2) ln 2. Event B contributes (3/8) × ln((3/8)/(1/4)) = (3/8) ln(3/2). Event C contributes (1/8) × ln((1/8)/(1/2)) = (1/8) ln(1/4).

Adding all three terms gives D(P||Q) = (1/2) ln 2 + (3/8) ln(3/2) + (1/8) ln(1/4). Because ln(1/4) = −2 ln 2, this simplifies to (1/4) ln 2 + (3/8) ln(3/2), approximately 0.325336 nats.

The weights 1/2, 3/8 and 1/8 all come from P. In particular, C's comparison probability is 1/2, but its log-ratio receives weight 1/8. Reading Q_C as that weight would change the quantity being calculated.

References: [SC1]

03

Reversing the direction changes the weights and the ratios

Keep every probability and event position in KL64 fixed. Now ask for D(Q||P). A contributes (1/4) ln(1/2), B contributes (1/4) ln(2/3), and C contributes (1/2) ln 4.

Their sum is D(Q||P) = (1/4) ln(1/2) + (1/4) ln(2/3) + (1/2) ln 4 = (3/4) ln 2 − (1/4) ln(3/2), approximately 0.418494 nats.

Reversing only the log-ratio while retaining P's weights would negate the forward sum. That would not compute reverse KL. Reverse KL also replaces each P weight with the corresponding Q weight. In KL64, C changes from a small negative contribution weighted by 1/8 to a large positive contribution weighted by 1/2.

An individual term is negative when its reference probability is smaller than its comparison probability. KL64's forward C term is approximately −0.173287 nats. The complete divergence between normalized distributions is nonnegative; its individual contributions need not be. Neither an individual contribution nor the total is a correctness probability.

The different totals establish asymmetry for this pair. They do not describe changed predictions or prove that either distribution is a better model. Comparing any distribution with itself gives zero, because every positive-mass event has log-ratio ln 1.

References: [SC1] [SC2]

04

Zero support can make only one direction infinite

Consider a separate stipulated pair over the same ordered events: P = (1/2, 1/2, 0) and Q = (1/2, 1/4, 1/4). The support of a distribution is its set of events with positive probability. Here P's support is A and B, while Q's includes A, B and C.

In D(P||Q), A contributes (1/2) ln 1 = 0 and B contributes (1/2) ln 2. C contributes zero because its reference weight P_C is zero. The forward total is therefore (1/2) ln 2, approximately 0.346574 nats, despite Q assigning positive mass to C.

In D(Q||P), C has positive reference weight Q_C = 1/4 and zero comparison probability P_C = 0. Its contribution is positive infinity under the documented relative-entropy convention, so the reverse total is infinite. The other finite terms cannot cancel that infinity.

SciPy 1.16.2 rel_entr defines a zero contribution when its first mass is zero and its second is nonnegative, including when both are zero. For a positive first mass and zero second mass, it defines positive infinity. These conventions define the mathematical boundary used here; they are not a claim about an executed PyTorch boundary return.

For this finite categorical problem, the support decision is exact: the requested direction is finite if and only if the reference support is contained in the comparison support. A displayed rounded zero cannot establish an exact zero. The report needs the underlying probability or its explicit zero convention before this boundary can be resolved.

References: [SC2]

05

Translate API roles into the underlying distributions

For mathematical D(P||Q), SciPy 1.16.2 stats.entropy associates pk with P and qk with Q. Its documented sum follows those probability roles. The routine also normalizes inputs that do not sum to one; KL64 already supplies normalized distributions, so no such change is needed.

PyTorch 2.14 KLDivLoss names its arguments differently. With log_target=False, input stores ln Q and target stores P. Substituting these roles into the documented pointwise expression gives P_i × (ln P_i − ln Q_i). The stored input is a log-probability representation of the comparison distribution; it is not the mathematical probability Q itself.

With log_target=True, target instead stores ln P, while its exponential supplies P's weights. The reference remains P. An argument name or position alone therefore cannot establish which distribution weights a reported comparison; the representation flag matters too.

Keep the class sum fixed when reading the scalar. For one batch row with three class columns, PyTorch's documented sum and batchmean reductions both retain that row's complete class sum, since batchmean divides by one. Its default mean divides the sum by three. That element mean would change the reported quantity even with the correct direction. The related cross-entropy guide examines a separate weighted-mean handoff problem.

References: [SC1] [PT1] [PT2]

06

Use the direction and support to resolve the report

Suppose one fictional KL64 report gives 0.325336 nats and another gives 0.418494 nats. The first can correctly report D(P||Q), while the second correctly reports D(Q||P). Their scalar difference alone provides no evidence that a model revision improved or deteriorated. If the requested objective was specifically P-weighted comparison against Q, only the first supplies that objective.

A useful short handoff statement for the first report is: reference P = (1/2, 3/8, 1/8); comparison Q = (1/4, 1/4, 1/2); ordered events A, B, C; natural logs; full class sum for one distribution; D(P||Q) approximately 0.325336 nats. An API-based report additionally identifies the library version, the probability or log-probability representation of each argument, and the actual reduction.

For supplied empirical distributions, retain the represented population and how its event probabilities were obtained. If transformations changed the supplied probabilities or their support, identify the resulting distributions being compared. This preserves what the scalar describes without assuming that a report's reference label proves population truth.

The support example exposes a different discrepancy: a finite forward value cannot answer a request for the infinite reverse quantity. Before interpreting an unexplained finite scalar for that reverse case, establish its actual distributions, direction and support. A generic KL label leaves the objective unresolved.

The receiving team can then determine whether the reported comparison answers the agreed question. Task quality and application acceptance require their own evidence. The related perplexity guide covers reference-token likelihood, while the sampling guide covers the support retained for a next-token draw.

07

Connect a separately needed equipment project to physical installation

If the selected application independently requires equipment mounting or physical IT cabling, Leviathan's rack-and-stack service and related project inquiry provide a route to discuss that work. Share a nonconfidential equipment list, site and released rack/cable package to establish physical scope and availability. Leviathan's published scope includes agreed mounting, IT cabling, labels and installation records. Distribution analysis, loss implementation and model evaluation remain with the appointed application team.

References: [L1]

08Sources

Primary sources

  1. [SC1]
    SciPy 1.16.2 — scipy.stats.entropyChecked 2026-10-21. Defines discrete relative entropy, pk/qk roles, normalization and logarithm units. Applied to supplied normalized categorical distributions over a shared event mapping.
  2. [SC2]
    SciPy 1.16.2 — scipy.special.rel_entrChecked 2026-10-21. Defines eventwise relative entropy and exact zero/infinity cases. The support example applies these mathematical conventions without asserting another library's runtime boundary behavior.
  3. [PT1]
    PyTorch 2.14 — KLDivLossChecked 2026-10-21. Documents input/target roles, log representations and reduction meanings for the named version. Used to interpret a reported comparison, rather than prescribe training or evaluation configuration.
  4. [PT2]
    PyTorch v2.14.0 — KLDivLoss source definitionChecked 2026-10-21. Tagged class definition confirms the probability/log-probability expressions and class-sum versus element-mean distinction.
  5. [L1]
    Leviathan Systems — Rack & Stack / ColocationChecked 2026-10-21. Public scope for agreed equipment mounting, physical IT cabling, labels and installation records for a separately identified equipment project.

Contact

Does selected equipment need physical installation?

Share a nonconfidential equipment list, site and released rack/cable package to discuss physical installation scope, requirements and availability.