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 機械学習・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 poweredAIでLaTeXを書く

最高のAI LaTeXエディタ、Overleaf代替

Octreeは15以上の大学の研究者がAIを使って論文をより速く書くのを支援

  • AIで美しいTikZダイアグラムを生成
  • 画像をアップロードしてシームレスにLaTeXに変換
  • 論文の執筆とフォーマットに即座にサポートを取得