import numpy as np, matplotlib.pyplot as plt
vocab = ["the","cat","sat","mat","sky","ran","blue","quietly"]
logits = np.array([3.2, 2.8, 2.5, 1.0, 0.7, 0.3, -0.5, -1.2])
def softmax(z): e = np.exp(z - z.max()); return e / e.sum()
def topp(p, thr):
o = np.argsort(p)[::-1]; cum = np.cumsum(p[o]); keep = o[:np.searchsorted(cum, thr)+1]
q = np.zeros_like(p); q[keep] = p[keep]; return q / q.sum()
dists = {"T = 0.5 (sharp)": softmax(logits/0.5), "T = 1.0": softmax(logits),
"T = 1.5 (flat)": softmax(logits/1.5), "top-p = 0.9": topp(softmax(logits), 0.9)}
fig, ax = plt.subplots(1, 4, figsize=(7.2, 2.0), sharey=True)
for a, (t, p) in zip(ax, dists.items()):
a.bar(range(len(vocab)), p, color="#5d2c80"); a.set_title(t, fontsize=8)
a.set_xticks(range(len(vocab))); a.set_xticklabels(vocab, rotation=90, fontsize=6)
plt.tight_layout(); plt.show()