Gradient Histogram Viz
A frequency histogram of a flat gradient array with the running mean drawn as a vertical accent-coloured line and the count / mean / std / min / max summarised above the chart. Use it to diagnose the two failure modes of backprop in one glance: vanishing gradients collapse the distribution onto zero with vanishingly small variance, while exploding gradients smear the tails across many orders of magnitude.
Gradient histogram across 4096 values. Mean 3.21e-5, standard deviation 0.0497, range -0.1761 to 0.1693.
Gradient distributionn = 4096
- mean
- 3.21e-5
- std
- 0.0497
- max
- 0.1693
- min
- -0.1761
Customize
Distribution
healthy
Histogram
32
Display
Installation
npx shadcn@latest add https://craftbits.dev/r/gradient-histogram-viz.jsonUsage
import { GradientHistogramViz } from "@craft-bits/core";
<GradientHistogramViz gradients={layer.grad.flat()} />Switch to a log frequency axis when a healthy distribution has a fat tail:
<GradientHistogramViz gradients={grads} logScale />Tune the bucket count for the underlying sample size — more bins for a high-resolution shape, fewer to denoise tiny tensors:
<GradientHistogramViz gradients={grads} bins={64} />Hide the stat readouts in dense layouts:
<GradientHistogramViz gradients={grads} showStats={false} />Understanding the component
- Two passes over the array. The first pass computes count, sum, min, max, and the mean. The second computes the variance against that mean, then buckets every finite value into one of
binsequal-width buckets betweenminandmax. Non-finite values (NaN,±Infinity) are silently dropped — they would otherwise destroy the histogram range. - The mean line uses
--cb-accent. Drawn over the bars so it stays visible against any bucket height. The line is dashed and labelledmean, springs to its new position whengradientschanges, and hides when the input has zero variance (min === max) since the line would be meaningless. logScaleis a frequency transform, not a value transform. Bar heights becomelog10(count + 1) / log10(maxCount + 1). The+ 1keeps the 0/1 transition smooth and avoids-Infinity. The x-axis still spans the raw[min, max]linearly.SPRINGS.smoothon every bar. Each bar'syandheightsprings independently whengradientschanges — the animation feels like a wave settling into the new distribution.prefers-reduced-motion: reducecollapses every spring to an instant snap.- Empty-state is graceful. If
gradientscontains no finite values, the chart renders an empty grid with the text "no finite gradients" centred in the plot area, and the stats grid shows0and—instead ofNaN. - Bin clamping.
binsis clamped to[4, 128]— too few and the distribution shape is meaningless; too many and individual buckets become single-sample noise.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
gradients | readonly number[] | — | Flat array of gradient values. Non-finite values are dropped. |
bins | number | 24 | Number of histogram buckets. Clamped to [4, 128]. |
showStats | boolean | true | Show the mean / std / min / max readouts above the chart. |
logScale | boolean | false | Log10 frequency axis (heights only — x-axis stays linear). |
transition | Transition | SPRINGS.smooth | Spring for bar-height transitions. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The figure is
role="figure"with anaria-labelledbythat names the chart "Gradient distribution." - An
aria-live="polite"summary announces the sample count, mean, standard deviation, and value range whenever the input changes. - The stat readouts use a semantic
<dl>/<dt>/<dd>structure so the label / value relationship is preserved for assistive tech. - The mean line carries
aria-hiddenbecause its meaning is already in the summary; redundant narration would clutter the live region. - Color is never the only signal — the mean line is labelled
mean, and every stat appears as text. prefers-reduced-motion: reducesnaps every bar transition; no per-bar easing is shown.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/viz/GradientHistogramViz.tsx). The source paired a fixed pedagogical log-scale gradient distribution with a movable fp16-representable-range overlay and aLabeledSlider-driven loss-scale picker — a single-purpose teaching artefact for the mixed-precision lesson. Reframed as a generic histogram primitive: accepts arbitrarygradients: number[], computes its own bucket counts, draws a mean overlay, and exposes thebins/showStats/logScaleaxes that any backprop diagnostic needs. Stripped the fp16-window simulation, scale-step slider, safe-zone overlay, "outside %" narration, and bell-curve pre-computation.