Cross Entropy in LaTeX (H(p, q))

H(p,q)H(p, q)
LaTeX command
H(p, q)
Package
No package needed: built into LaTeX
Mode
Text and math mode

Where it is used

Cross-entropy between a true distribution p and a model q: the expected code length when coding p with q.

Cross-entropy is H(p, q) = -\sum_x p(x) \log q(x), and it is the classification loss: with one-hot labels it reduces to -\log q(y). It decomposes as H(p, q) = H(p) + D_{\text{KL}}(p \| q), which explains why minimizing it over q also minimizes KL divergence. Watch the notation clash: H(X, Y) with random variables usually means joint entropy, so some authors write H_\times(p, q) or \mathrm{CE}(p, q) for clarity. The comma has normal punctuation spacing, so no manual adjustment is needed.

Minimal compilable example

Paste this into an empty .tex file and compile it with pdfLaTeX, XeLaTeX or LuaLaTeX.

H(p,q)=−∑xp(x)log⁡q(x)=H(p)+DKL(p∥q)H(p, q) = -\sum_{x} p(x) \log q(x) = H(p) + D_{\text{KL}}(p \| q)
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Cross Entropy: $H(p, q)$

\[ H(p, q) = -\sum_{x} p(x) \log q(x) = H(p) + D_{\text{KL}}(p \| q) \]
\end{document}

Related symbols

All Machine Learning & AI →

Frequently asked questions

How do I write Cross Entropy in LaTeX?
Type H(p, q) in math mode, for example $H(p, q) = -\sum_{x} p(x) \log q(x) = H(p) + D_{\text{KL}}(p \| q)$. It works in plain LaTeX without loading any package.
Which package do I need for H(p, q)?
None. H(p, q) is part of standard LaTeX, so it compiles with nothing in the preamble but the document class.
Can I use H(p, q) in text mode, or only in math mode?
Both. H(p, q) compiles in running text as well as inside $...$; in a formula keep it in math mode so it gets math fonts and spacing.
Math poweredSchrijf LaTeX met AI

Beste AI LaTeX Editor, Overleaf Alternatief

Octree helpt onderzoekers van 15+ universiteiten sneller onderzoeksartikelen schrijven met AI

  • Genereer mooie TikZ-diagrammen met AI
  • Upload afbeelding en converteer naadloos naar LaTeX
  • Krijg directe hulp bij het schrijven en opmaken van onderzoeksartikelen