All formulas

Information theory

Cross-entropy

info.cross-entropyAlso called cross entropy, crossentropy, xent, h pq

H(p,q)=−∑i=1npiln⁡qiH(p, q) = -\sum_{i=1}^{n} p_i \ln q_i

Expected information content under q of outcomes drawn from p, in nats. It equals H(p) + DKL(p ‖ q) and is not symmetric in p and q.

Notation

pp

probability vector

p ∈ Δn: pi > 0, ∑pi = 1

qq

second probability vector

q ∈ Δn: qi > 0, ∑qi = 1

nn

number of outcomes

Size, at least 2

H(p,q)H(p, q)

Result: the cross-entropy, in nats

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    p=[0.5,0.5],q=[0.25,0.75]p = [0.5, 0.5], q = [0.25, 0.75]

    Result

    H(p,q)=log⁡(163)2\displaystyle H(p, q) = \frac{\log{\left(\frac{16}{3} \right)}}{2}

    ≈ 0.836988216786

  2. Given

    p=[1/3,2/3],q=[0.5,0.5]p = [1/3, 2/3], q = [0.5, 0.5]

    Result

    H(p,q)=log⁡(2)\displaystyle H(p, q) = \log{\left(2 \right)}

    ≈ 0.69314718056

  3. Given

    p=[0.25,0.75],q=[0.25,0.75]p = [0.25, 0.75], q = [0.25, 0.75]

    Result

    H(p,q)=3log⁡(43)4+log⁡(4)4\displaystyle H(p, q) = \frac{3 \log{\left(\frac{4}{3} \right)}}{4} + \frac{\log{\left(4 \right)}}{4}

    ≈ 0.562335144619

Common mistakes

  • Swaps p and q

    The logarithm is taken of p and weighted by q.

    Fix: Weight by p and take the logarithm of q.

  • Uses log base 2

    The result is in bits.

    Fix: Use the natural logarithm, or multiply by ln 2.

Sources

  • Cover and Thomas 2006

    Cover, Thomas M.; Thomas, Joy A. Elements of Information Theory. 2nd ed. Wiley-Interscience, 2006. doi:10.1002/047174882X

    doi.org
  • Goodfellow et al. 2016, §3.13

    Goodfellow, Ian; Bengio, Yoshua; Courville, Aaron. Deep Learning. MIT Press, 2016.

    deeplearningbook.org