Perplexity Viz
Per-position probability bars plus the headline language-model eval metric: mean log p and PP = exp(-mean log p). Each bar's height is the model's probability on the true token; an optional row of log p_i underneath each bar exposes the value actually being summed. A model that nails every token sits at PP = 1; a uniform-over-V guesser sits at PP = V.
Per-token probability and perplexity readout.Perplexity 1.35 over 8 tokens. Mean log-probability -0.299.
PP = exp(-mean log p)N = 8
mean log p-0.299
perplexity1.35
Customize
Model
confident
Display
Installation
npx shadcn@latest add https://craftbits.dev/r/perplexity-viz.jsonUsage
import { PerplexityViz } from "@craft-bits/core";
<PerplexityViz
tokens={[
{ word: "the", probability: 0.92 },
{ word: "cat", probability: 0.78 },
{ word: "sat", probability: 0.65 },
{ word: "on", probability: 0.88 },
{ word: "the", probability: 0.95 },
{ word: "mat", probability: 0.55 },
]}
/>Hide the per-token log p_i row for a more compact placement:
<PerplexityViz tokens={tokens} showLogProb={false} />Understanding the component
- Perplexity, one line.
PP = exp(-(1/N) Σ log p_i). The mean of the log-probs is the cross-entropy of the model on the true sequence (in nats); exponentiating turns it back into a "vocabulary size" — the number of equally-likely options the model is effectively choosing between at each position. - Two readouts, one number. The strip under the chart shows both the raw mean log-prob (handy when you want to talk in cross-entropy / nats) and the perplexity itself (the number you'd quote in a paper). The mean log-prob is negative; perplexity is the positive exponential image of it.
- Bar height =
p_i, notlog p_i. The bars use the linear probability so a "high" bar matches the user's intuition for "the model was confident". The optionallog p_irow underneath each bar is what's actually being summed — useful when teaching why a single low-probability token can crater the whole sentence's perplexity. - Numerical floor. Every probability is clamped at
1e-12beforelogso a stray0doesn't poison the mean. The floor is well below anything a calibrated LM ever emits — real "zero" probabilities still show up as a giant negativelog p_iand pull perplexity up sharply, exactly as the maths demands. - Spring transitions. Bar heights animate with
SPRINGS.smoothfrom@craft-bits/core/motionso swappingtokens(e.g. driving from a scrubber or another model's output) produces a continuous reshape rather than a hard cut. Reduced-motion users snap.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
tokens | readonly { word: string; probability: number }[] | — | One entry per position. probability is clamped to [1e-12, 1] for the maths. |
showLogProb | boolean | true | Render the per-token log p_i row underneath each bar. |
transition | Transition | SPRINGS.smooth | Spring used when tokens changes. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The chart is wrapped in
role="figure"with anaria-describedbysummary that reads "Perplexity 2.34 over 6 tokens. Mean log-probability -0.853." Screen-reader users get the same headline metric a sighted user sees. - The summary uses
aria-live="polite"so changes are announced whentokensis replaced. - All colors come from the
--cb-fg/--cb-accent/--cb-fg-mutedtoken family — contrast is governed by the theme and meets WCAG AA against the default--cb-bg-elevated. - Bar transitions use
SPRINGS.smoothand respectprefers-reduced-motion: reduce— bar heights snap instantly when the user opts out of motion.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/viz/PerplexityViz.tsx). Stripped the lesson-specific Explore / Predict mode strip, theModeStrip/ChallengeBtn/FeedbackBadge/ScoreDots/DoneCardchrome, the syntheticmakeDist(confidence)distribution, the "effective choices" 10×10 grid (a different metric), and theTogglePill/LabeledSlidercontrols — generalised to a pure visualisation primitive that takes a realtokensarray and shows the headline metric. Swapped inlineSPRINGS.snappyforSPRINGS.smoothfrom@craft-bits/core/motionso bar reshapes feel continuous when streaming new token sequences in. - Reference: Perplexity (information theory).