Positional Encoding Viz
A heatmap visualisation of the sinusoidal positional-encoding matrix used by transformers since "Attention Is All You Need." Each row is a position in the sequence; each column is an embedding dimension. Even columns hold sin(pos / base^(2k/d)), odd columns hold cos(pos / base^(2k/d)) — so low columns oscillate fast across positions, high columns oscillate slowly, and the resulting matrix encodes every position with a unique pattern an attention layer can recover.
Positional encoding heatmap.Sinusoidal positional encoding, 32 positions by 64 dimensions, base 10000.
Customize
Shape
32
64
Wavelength
10000
Color
diverging
Installation
npx shadcn@latest add https://craftbits.dev/r/positional-encoding-viz.jsonUsage
import { PositionalEncodingViz } from "@craft-bits/core";
<PositionalEncodingViz seqLen={32} dModel={64} />Drop the standard transformer parameters and pick the diverging color scheme so positive and negative values separate visually:
<PositionalEncodingViz
seqLen={64}
dModel={128}
base={10000}
colorScheme="diverging"
/>Highlight one row and one column to teach how a single position interacts with each dimension:
<PositionalEncodingViz
seqLen={32}
dModel={64}
highlightRow={8}
highlightCol={4}
/>Understanding the component
- The formula. For position
posand dimensioncol, the encoding issin(pos / base^(2k/d))whencol = 2k, andcos(pos / base^(2k/d))whencol = 2k+1. The base is10000in the original transformer paper. The geometricbase^(2k/d)denominator makes the wavelength grow exponentially with the dimension index — low dims have short wavelengths, high dims have long wavelengths. - Why this matters. Because every dimension oscillates at a different rate, every position vector is unique, and any two positions a fixed distance apart relate to each other by a rotation — which is exactly the kind of relative-position signal attention layers can pick up via dot products.
- Two color schemes.
diverging(default) preserves sign — positive values tintvar(--cb-accent), negative values tintvar(--cb-warning), and magnitude drives alpha. Zero is near-transparent.accentdrops the sign and tintsvar(--cb-accent)by absolute value only — useful when teaching magnitude alone. - Optional highlights. Pass
highlightRowto outline one position row with a solid accent stroke; passhighlightColto outline one dimension column with a dashed accent stroke. Both can be set simultaneously; either can be omitted. - Spring transitions. When a highlight index changes, the outline animates with
SPRINGS.smoothfrom@craft-bits/core/motion.prefers-reduced-motion: reducecollapses the spring to an instant swap. - Pure primitive. The component computes the matrix internally from
seqLen,dModel, andbase— there is no externally-provided matrix prop. Animations are limited to highlight outlines; the cells themselves are static rectangles for fast renders even at largeseqLenanddModel.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
seqLen | number | 32 | Sequence length (number of rows). |
dModel | number | 64 | Model dimension (number of columns). |
base | number | 10000 | Wavelength base. The standard transformer value is 10000. |
colorScheme | "diverging" | "accent" | "diverging" | Sign-preserving (accent / warning) vs magnitude-only (accent). |
highlightRow | number | — | Highlight one position row with an accent outline. |
highlightCol | number | — | Highlight one dimension column with an accent outline. |
size | number | 360 | SVG side length in pixels. |
transition | Transition | SPRINGS.smooth | Spring for highlight transitions. |
className | string | — | Merged onto the root via cn(). |
Accessibility
- The outer element is
role="figure"with anaria-labeland a visually hiddenaria-live="polite"summary — screen readers hear the matrix dimensions, base, and active highlights whenever they change. - Color is never the only signal — the diverging scheme distinguishes sign with two distinct hues (accent and warning), and the accent scheme is unambiguous because there is no sign to lose.
- The component is read-only — no interactive handles, so no keyboard or pointer behaviour to enumerate.
- Animations respect
prefers-reduced-motion: reduce— highlight springs collapse to an instant swap.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/viz/PositionalEncodingViz.tsx). The source was a full lesson widget bundled with Explore / Predict / Challenge mode strips, dot-product similarity charts, line waveforms across positions and dimensions, history-undo state, and preset bookmarks. The library extract is the pure visualisation primitive — the heatmap of the encoding matrix itself, plus optional row + column highlights. Mode strips, similarity charts, and narration belong in the consuming lesson.