brand banner

Total Probability, Total Expectation, and the GAN Discriminator Loss

Law of total probability (marginalization), law of total expectation, and how averaging binary cross-entropy over real and fake images gives the GAN discriminator loss.

Updated Β· 3 min read Β· #probability Β· #machine-learning

Total Probability, Total Expectation, and the GAN Discriminator Loss

Law of total probability (marginalization), law of total expectation, and how averaging binary cross-entropy over real and fake images gives the GAN discriminator loss.

Law of total probability (marginalization)

If the cases 𝑑 cover every possibility and never overlap, split an event 𝐴 by case, then add the cases back up:

𝑃(𝐴)=βˆ‘π‘‘π‘ƒ(𝐴|𝑑)𝑃(𝑑)

When the cases are binary, π‘‘βˆˆ{0,1}:

𝑃(𝐴)=𝑃(𝐴|𝑑=1)𝑃(𝑑=1)+𝑃(𝐴|𝑑=0)𝑃(𝑑=0)

Summing a joint distribution over 𝑑, 𝑝(π‘Ž)=βˆ‘π‘‘π‘(π‘Ž,𝑑), is called marginalization. It is the same law, because 𝑝(π‘Ž,𝑑)=𝑝(π‘Ž|𝑑)𝑝(𝑑).

Law of total expectation

The same split works for an average. Average the quantity 𝐿 within each case, then weight each case by its probability:

𝔼[𝐿]=βˆ‘π‘‘π‘ƒ(𝑑)𝔼[𝐿|𝑑]

Written compactly, this is 𝔼[𝐿]=𝔼[𝔼[𝐿|𝑑]]. The inner expectation averages within one case, and the outer one averages over the cases.

From BCE to the discriminator loss 𝐽𝐷

A GAN’s discriminator 𝐷 outputs 𝑑̂=𝐷(β‹…)∈(0,1), its estimated probability that an image is real. The label is 𝑑=1 for a real image π‘₯ and 𝑑=0 for a fake image 𝐺(𝑧), where 𝐺 is the generator and 𝑧 is a random noise vector. The binary cross-entropy (BCE) loss for one image is

𝐿=βˆ’π‘‘logπ‘‘Μ‚βˆ’(1βˆ’π‘‘)log(1βˆ’π‘‘Μ‚).

Step 1: the loss in each case. Substituting the label makes one term vanish:

  • If 𝑑=1: 𝐿=βˆ’log𝐷(π‘₯).
  • If 𝑑=0: 𝐿=βˆ’log(1βˆ’π·(𝐺(𝑧))).

Step 2: split the expected loss by label. Let πœ‹=𝑃(𝑑=1) be the fraction of images that are real. The law of total expectation gives

𝔼[𝐿]=πœ‹π”Όπ‘₯[βˆ’log𝐷(π‘₯)]+(1βˆ’πœ‹)𝔼𝑧[βˆ’log(1βˆ’π·(𝐺(𝑧)))].

𝔼π‘₯ averages over real images and 𝔼𝑧 averages over noise vectors.

Step 3: equal batches. GAN training shows 𝐷 equal numbers of real and fake images, so πœ‹=12. Multiplying by 2 gives

𝐽𝐷=2𝔼[𝐿]=𝔼π‘₯[βˆ’log𝐷(π‘₯)]+𝔼𝑧[βˆ’log(1βˆ’π·(𝐺(𝑧)))].

The factor of 2 does not change which 𝐷 minimizes the loss.

Example. Take one real image with 𝐷(π‘₯)=0.9 and one fake with 𝐷(𝐺(𝑧))=0.2. Then

𝐽𝐷=βˆ’ln0.9βˆ’ln(1βˆ’0.2)β‰ˆ0.105+0.223=0.329,

and the expected BCE over this pool is 𝔼[𝐿]=𝐽𝐷2β‰ˆ0.164.

Caveat. The original GAN paper writes 𝐽𝐷 directly as β€œloss on real + loss on fake”. The total-expectation derivation is a way to interpret it, not how the paper derives it.


  • Maximum Likelihood and Cross-Entropy (61%) β€” A mathematical textbook connecting likelihood, entropy, KL divergence, statistical estimation, and probabilistic machine learning, with worked examples and solved exercises.
  • Bayesian Inference: Alice’s Coin, Likelihood, MLE, and MAP (49%) β€” One hidden coin connects conditional independence, Bayesian updating, prediction, Naive Bayes, and maximum-likelihood estimation.
  • Data Augmentation for Bias Term (43%) β€” Adding a constant feature folds the intercept into linear regression’s weight vector without conflating it with statistical bias.
  • Why the Multivariate Chain Rule Is a Sum (42%) β€” As t moves, x and y move at once. The rate of change of f is the directional derivative along the velocity (x'(t), y'(t)), and a dot product is a sum β€” so each intermediate variable contributes its own term.
  • MAE vs MSE: The Nudge Argument (42%) β€” Why the median minimises absolute error and the mean minimises squared error, by nudging the guess.
  • Background (42%) β€” Recap of prereq knowledge for COMP 3804 (Fall 2025).
  • The Kernel Trick (SVM) (40%) β€” Understanding the kernel trick in SVMs, focusing on the roles of alpha and the inference formula.
  • Harmonic Mean (40%) β€” Equal-distance average speed, F1-score, parallel resistance, and HM ≀ GM ≀ AM.

Β© 2026 James Yap

Personal Website and Knowledge Base