← All notes

Negative Log-Likelihood, Entropy, Cross-Entropy, and KL Divergence

Contents

Throughout this note, let zz be an outcome, such as a class, token, or complete sequence, and let pp and qq be probability distributions over the same outcomes.

Negative Log-Likelihood

Suppose an observed outcome is zz and a model assigns it probability q(z)q(z). Its likelihood is q(z)q(z), so its negative log-likelihood (NLL) is

LNLL(z;q)=−log⁡q(z).\mathcal{L}_{\mathrm{NLL}}(z;q) = -\log q(z).

A likely outcome has a small NLL, while an outcome assigned near-zero probability has a very large NLL. For a dataset of NN observed outcomes z1,…,zNz_1,\ldots,z_N, the average loss is

LNLL(q)=−1N∑n=1Nlog⁡q(zn).\mathcal{L}_{\mathrm{NLL}}(q) = -\frac{1}{N}\sum_{n=1}^{N}\log q(z_n).

When the observations are drawn from a distribution pp, the expected NLL under pp is the cross-entropy H(p,q)H(p,q) defined below.

Entropy

In information theory, entropy (HH) measures the average uncertainty, surprise, or information content in a distribution of messages or data. The concept was introduced by Claude Shannon.

For a distribution pp, entropy is

H(p)=−∑zp(z)log⁡p(z).H(p)=-\sum_z p(z)\log p(z).

For example:

Equivalently, entropy is the expected surprise when outcomes come from pp and are evaluated using pp itself:

H(p)=Ez∼p ⁣[−log⁡p(z)].H(p)=\mathbb{E}_{z\sim p}\!\left[-\log p(z)\right].

Cross-Entropy

Cross-entropy measures how well a distribution qq predicts outcomes distributed according to a target distribution pp:

H(p,q)=−∑zp(z)log⁡q(z).H(p,q)=-\sum_z p(z)\log q(z).

This form comes from expected negative log-likelihood. For one outcome zz, the loss under qq is −log⁡q(z)-\log q(z). Because pp produces that outcome with probability p(z)p(z), averaging the loss over outcomes from pp gives

Ez∼p ⁣[−log⁡q(z)]=∑zp(z)[−log⁡q(z)]=H(p,q).\mathbb{E}_{z\sim p}\!\left[-\log q(z)\right] =\sum_z p(z)\left[-\log q(z)\right] =H(p,q).

Here, p(z)p(z) determines how much weight the target distribution gives outcome zz, while −log⁡q(z)-\log q(z) measures how poorly qq predicts it.

KL Divergence

KL divergence measures how one probability distribution differs from another. For discrete distributions, it is1

DKL(p∥q)=∑zp(z)log⁡p(z)q(z).D_{\mathrm{KL}}(p\parallel q) = \sum_z p(z)\log\frac{p(z)}{q(z)}.

For continuous distributions with densities p(z)p(z) and q(z)q(z), the sum becomes an integral:

DKL(p∥q)=∫p(z)log⁡p(z)q(z) dz.D_{\mathrm{KL}}(p\parallel q) = \int p(z)\log\frac{p(z)}{q(z)}\,dz.

KL divergence can also be written as an expectation. When pp is the target distribution and qq is the approximation, the direction from pp to qq is called forward KL:

DKL(p∥q)=Ez∼p ⁣[log⁡p(z)q(z)]=Ez∼p ⁣[log⁡p(z)−log⁡q(z)].\begin{aligned} D_{\mathrm{KL}}(p\parallel q) &= \mathbb{E}_{z\sim p} \!\left[\log\frac{p(z)}{q(z)}\right] \\ &= \mathbb{E}_{z\sim p} \!\left[\log p(z)-\log q(z)\right]. \end{aligned}

Swapping the arguments gives the reverse KL:

DKL(q∥p)=Ez∼q ⁣[log⁡q(z)p(z)]=Ez∼q ⁣[log⁡q(z)−log⁡p(z)].\begin{aligned} D_{\mathrm{KL}}(q\parallel p) &= \mathbb{E}_{z\sim q} \!\left[\log\frac{q(z)}{p(z)}\right] \\ &= \mathbb{E}_{z\sim q} \!\left[\log q(z)-\log p(z)\right]. \end{aligned}

The direction changes the distribution used to weight the expectation: forward KL samples from pp, while reverse KL samples from qq. KL divergence is not symmetric, so the order of the arguments matters:

DKL(p∥q)≠DKL(q∥p).D_{\mathrm{KL}}(p\parallel q) \neq D_{\mathrm{KL}}(q\parallel p).

By Gibbs’ inequality, both directions are nonnegative. The individual log-ratio terms inside the expectation can still be negative.2

Cross-Entropy and Forward KL

Cross-entropy decomposes into the entropy of the target distribution and the forward KL divergence:

H(p,q)=H(p)+DKL(p∥q)=−∑zp(z)log⁡p(z)+∑zp(z)log⁡p(z)q(z).\begin{aligned} H(p,q) &=H(p)+D_{\mathrm{KL}}(p\parallel q) \\ &=-\sum_z p(z)\log p(z) +\sum_z p(z)\log\frac{p(z)}{q(z)}. \end{aligned}

When pp is fixed, H(p)H(p) is constant. A learned distribution qq can therefore minimize cross-entropy only by minimizing forward KL:

arg⁡min⁡qH(p,q)=arg⁡min⁡qDKL(p∥q).\arg\min_q H(p,q) = \arg\min_q D_{\mathrm{KL}}(p\parallel q).

Both objectives consequently have the same optimum.3 This is why a cross-entropy objective against a fixed teacher or data distribution can be interpreted as forward-KL minimization.

SFT and RL Through the KL Lens

Adapted from Cameron R. Wolfe’s explanation of SFT and RL through KL divergence.[2]

The same KL perspective clarifies an important difference between supervised fine-tuning (SFT) and KL-regularized reinforcement learning. For a fixed prompt ss, let uu denote a complete output sequence. To keep the equations uncluttered, all distributions below are implicitly conditioned on ss.

The key distinction is the distribution from which completions are sampled. SFT averages its loss over a fixed dataset distribution, so its training signal is offline. RL averages reward over completions sampled from the current model, so its training signal is online, or on-policy.

SFT as Forward KL

Let pdatap_{\mathrm{data}} be the distribution represented by the SFT dataset and qθq_\theta the model being trained. The SFT negative log-likelihood is

LSFT(θ)=Eu∼pdata ⁣[−log⁡qθ(u)]=H(pdata,qθ)=H(pdata)+DKL(pdata∥qθ).\begin{aligned} \mathcal{L}_{\mathrm{SFT}}(\theta) &= \mathbb{E}_{u\sim p_{\mathrm{data}}} \!\left[-\log q_\theta(u)\right] \\ &= H(p_{\mathrm{data}},q_\theta) \\ &= H(p_{\mathrm{data}}) + D_{\mathrm{KL}}(p_{\mathrm{data}}\parallel q_\theta). \end{aligned}

Because H(pdata)H(p_{\mathrm{data}}) does not depend on the model parameters, minimizing the SFT loss is equivalent to minimizing the forward KL divergence:

arg⁡min⁡θLSFT(θ)=arg⁡min⁡θDKL(pdata∥qθ).\arg\min_\theta \mathcal{L}_{\mathrm{SFT}}(\theta) = \arg\min_\theta D_{\mathrm{KL}}(p_{\mathrm{data}}\parallel q_\theta).

The data distribution appears first in the KL and therefore supplies the samples in the expectation.

KL-Regularized RL as Reverse KL

For KL-regularized RL, let qθq_\theta be the current policy, qrefq_{\mathrm{ref}} a fixed reference policy, r(u)r(u) the reward, and β>0\beta>0 the strength of the KL penalty. The idealized objective is

JRL(θ)=Eu∼qθ[r(u)]−βDKL(qθ∥qref).J_{\mathrm{RL}}(\theta) = \mathbb{E}_{u\sim q_\theta}[r(u)] - \beta D_{\mathrm{KL}}(q_\theta\parallel q_{\mathrm{ref}}).

Define the reward-induced target distribution

pR∗(u)=1Z qref(u)exp⁡ ⁣(r(u)β),Z=∑u∈Uqref(u)exp⁡ ⁣(r(u)β).p_R^*(u) = \frac{1}{Z}\, q_{\mathrm{ref}}(u) \exp\!\left(\frac{r(u)}{\beta}\right), \qquad Z = \sum_{u\in\mathcal{U}} q_{\mathrm{ref}}(u) \exp\!\left(\frac{r(u)}{\beta}\right).

This target reweights the reference policy toward higher-reward completions. Substituting its log-probability into the reverse KL gives

DKL(qθ∥pR∗)=DKL(qθ∥qref)−1βEu∼qθ[r(u)]+log⁡Z=−1βJRL(θ)+log⁡Z.\begin{aligned} D_{\mathrm{KL}}(q_\theta\parallel p_R^*) &= D_{\mathrm{KL}}(q_\theta\parallel q_{\mathrm{ref}}) - \frac{1}{\beta} \mathbb{E}_{u\sim q_\theta}[r(u)] + \log Z \\ &= -\frac{1}{\beta}J_{\mathrm{RL}}(\theta)+\log Z. \end{aligned}

Because ZZ does not depend on θ\theta, maximizing this KL-regularized RL objective is equivalent to minimizing DKL(qθ∥pR∗)D_{\mathrm{KL}}(q_\theta\parallel p_R^*). This is a reverse KL because the learned policy appears first and the fixed reward-induced target appears second.

The explicit term DKL(qθ∥qref)D_{\mathrm{KL}}(q_\theta\parallel q_{\mathrm{ref}}) inside JRLJ_{\mathrm{RL}} is the regularizer that keeps the policy near the reference. The reverse-KL interpretation comes from rewriting the complete reward-plus-regularization objective.

Mode Covering Versus Mode Seeking

These two KL directions help explain the different tendencies of SFT and RL:

A toy mixture model compares forgetting under forward-KL supervised fine-tuning and reverse-KL reinforcement learning. Forward KL stretches the new mode and draws probability mass away from the old mode, while reverse KL shifts the new mode toward the target with little change to the old mode.
A toy mixture model of forgetting dynamics: forward-KL SFT moves probability mass from the old mode to cover the target, while reverse-KL RL shifts the new mode toward the target and largely preserves the old mode. Source: Chen et al., “Retaining by Doing: The Role of On-Policy Data in Mitigating Forgetting” [1], Figure 1.

Mode covering and mode seeking describe tendencies, not guarantees. The practical behavior also depends on model capacity, optimization, reward quality, sampling, and the strength β\beta of the reference-policy constraint.

TODO

Footnotes

  1. Take two distributions over three outcomes:

    zzp(z)p(z)q(z)q(z)p(z)q(z)\frac{p(z)}{q(z)}log⁡p(z)q(z)\log \frac{p(z)}{q(z)}p(z)log⁡p(z)q(z)p(z)\log \frac{p(z)}{q(z)}
    A0.500.252.02.00.6930.6930.34650.3465
    B0.300.251.21.20.1820.1820.05460.0546
    C0.200.500.40.4−0.916-0.916−0.1832-0.1832

    Summing the final column gives

    DKL(p∥q)=0.3465+0.0546−0.1832=0.2179≈0.218.D_{\mathrm{KL}}(p\parallel q) =0.3465+0.0546-0.1832 =0.2179\approx0.218. ↩
  2. For outcome C in the worked example, log⁡p(C)q(C)=−0.916\log\frac{p(\mathrm{C})}{q(\mathrm{C})}=-0.916. This is one term inside the weighted sum, not the complete KL divergence. Other terms compensate for it, and the total KL remains nonnegative. ↩

  3. For a numerical check, suppose p=(0.8,0.2)p=(0.8,0.2) and q=(0.6,0.4)q=(0.6,0.4). Using natural logarithms:

    QuantityCalculation
    Cross-entropyH(p,q)=−[0.8log⁡(0.6)+0.2log⁡(0.4)]=0.592H(p,q)=-[0.8\log(0.6)+0.2\log(0.4)]=0.592
    Target entropyH(p)=−[0.8log⁡(0.8)+0.2log⁡(0.2)]=0.500H(p)=-[0.8\log(0.8)+0.2\log(0.2)]=0.500
    KL divergenceDKL(p∥q)=0.8log⁡0.80.6+0.2log⁡0.20.4=0.092D_{\mathrm{KL}}(p\parallel q)=0.8\log\frac{0.8}{0.6}+0.2\log\frac{0.2}{0.4}=0.092

    The displayed values satisfy H(p,q)=H(p)+DKL(p∥q)=0.500+0.092=0.592H(p,q)=H(p)+D_{\mathrm{KL}}(p\parallel q)=0.500+0.092=0.592. If qq improves until q=p=(0.8,0.2)q=p=(0.8,0.2), then DKL(p∥q)=0D_{\mathrm{KL}}(p\parallel q)=0 and H(p,q)=H(p)=0.500H(p,q)=H(p)=0.500. The minimum cross-entropy occurs when qq matches pp, but it does not generally become zero because the fixed target entropy remains. ↩

Bibliography

  1. Howard Chen, Noam Razin, Karthik Narasimhan, and Danqi Chen, “Retaining by Doing: The Role of On-Policy Data in Mitigating Forgetting,” arXiv preprint arXiv:2510.18874, 2025.
  2. Cameron R. Wolfe, “SFT and RL Training Objectives as Forward and Reverse KL Divergence,” X, 2026.