# Entropy, Cross-Entropy, and KL Divergence in LLM Training

> Source: <https://pub.towardsai.net/entropy-cross-entropy-and-kl-divergence-in-llm-training-75b5c767c374?source=rss----98111c9905da---4>
> Published: 2026-07-06 18:01:01+00:00

Member-only story

# Entropy, Cross-Entropy, and KL Divergence in LLM Training

## Each code block in this article is self-contained and can be run independently — that is why a few helper functions (`softmax`

, `kl_divergence`

, etc.) are re-defined across snippets.

Every time a language model trains on a batch of text, three numbers tell the complete story: entropy, cross-entropy, and KL divergence. These quantities are not abstract mathematics — they are the direct reason your model learns, overfits, or refuses to commit to an answer.

Understanding them precisely changes how you read training curves, tune temperature, choose RLHF objectives, and debug why a fine-tuned model collapses or hedges. This article builds each concept from first principles with concrete code, real output, and the production connections that make them matter.

Disclaimer: The opinions expressed in this article are my own and do not represent the views of Google. This content is based solely on publicly available information.

## Part 1: Shannon Entropy — Measuring Uncertainty

Before connecting these concepts to training loss or RLHF, you need a precise definition of uncertainty — one that matches what a language…
