%matplotlib inline
from d2l import tensorflow as d2l
import random
import tensorflow as tf
from tensorflow_probability import distributions as tfdMost of machine learning is inference under uncertainty:
The chapter’s running example: tossing a fair coin. As the sample count grows, empirical frequencies converge to the true P = 0.5:
The standard d2l prelude (plus a multinomial distribution we’ll use shortly):
A cleaner abstraction: a Multinomial over the categories {heads, tails} with probabilities [0.5, 0.5]. One call returns the count vector for 100 tosses:
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([49., 51.], dtype=float32)>
With 10 000 tosses, the empirical frequencies sit much closer to 0.5:
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.5027, 0.4973], dtype=float32)>
This is the law of large numbers: as n \to \infty the empirical mean converges to the true mean.
Plot the running estimate of P(\text{heads}) and P(\text{tails}) vs. sample count — the curves zigzag toward 0.5:
The variance of the estimate shrinks like 1/\sqrt{n} — doubling accuracy means quadrupling the sample budget.