Gradient Flow Viz
A backward-pass animation. Each column is a layer of neurons; a single wave glides from the loss-side (rightmost) toward the input (leftmost), painting the active layer in --cb-warning and tagging it with the gradient magnitude reaching that layer. Sibling of GradientDecayExplorer, which renders the same per-layer magnitude story as a static bar chart — GradientFlowViz adds the dynamic feel of backprop: the user sees the gradient travel.
Backward gradient wave at layer 4 of 4 (output). Gradient magnitude 1.00.
Gradient wave · backwardlayer 04 / 04 · |∂L| 1.00
Customize
Architecture
3-4-4-2
Playback
500
Installation
npx shadcn@latest add https://craftbits.dev/r/gradient-flow-viz.jsonUsage
import { GradientFlowViz } from "@craft-bits/core";
<GradientFlowViz layers={[3, 4, 4, 2]} />Drive the wave from your own training loop:
<GradientFlowViz
layers={[3, 4, 4, 2]}
gradients={[0.0008, 0.012, 0.18, 1]}
/>Drive the cursor from outside (controlled mode) — e.g. sync to a scroll position:
const [layer, setLayer] = useState(0);
<GradientFlowViz
layers={[3, 4, 2]}
currentLayer={layer}
onCurrentLayerChange={setLayer}
playing={false}
/>Understanding the component
- Cursor 0 is the loss side.
currentLayer = 0lights the rightmost column (where backprop starts);currentLayer = depth − 1lights the input layer. The wave travels right → left as the cursor advances — the direction gradients flow during backprop. - One wave, three weights. The active column is fully lit; the two neighbours ahead of the wave glow at 55% and 25% intensity, so the eye can read the wave's direction even at slow paint speeds. Layers further away dim to a static rest state.
--cb-warningis the wave colour. Warning (not accent) because the textbook story this viz teaches is vanishing gradients — the wave shrinks as it travels away from the loss. The default synthesised gradients follow0.55^(depth − 1 − l)so the magnitudes you see on autoplay actually do shrink as the wave moves left.SPRINGS.smoothon the halo. Each layer's halo opacity and node fades use the smooth spring from@craft-bits/core/motion.prefers-reduced-motion: reduceparks the cursor at the input layer with no autoplay and no per-layer transition.setInterval, not RAF. The wave's cadence is set byplaySpeed(default500ms) — asetIntervaladvances the cursor and the spring resolves between ticks. Floored at80msso the spring always has enough time to settle visibly.- Auto layout from
layersalone. Node positions are derived from thelayersshape — change the depth and the diagram re-flows. No coordinates to maintain.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
layers | readonly number[] | — | Neuron count per layer (left → right). Required. |
gradients | readonly number[] | — | Gradient magnitude per layer. Synthesised (vanishing series) when omitted. |
currentLayer | number | — | Controlled wave cursor. 0 = loss-side. Pair with onCurrentLayerChange. |
defaultCurrentLayer | number | 0 | Uncontrolled initial cursor. |
onCurrentLayerChange | (layer) => void | — | Fires when the cursor advances. |
playing | boolean | true | Whether the wave auto-advances backward. |
playSpeed | number | 500 | Milliseconds between wave advances. Floored at 80. |
transition | Transition | SPRINGS.smooth | Spring for the halo / node fades. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The figure is
role="figure"with anaria-labelledbythat names the chart "Gradient wave · backward". - An
aria-live="polite"summary announces the current layer, its semantic label (input/hidden N/output), and the gradient magnitude whenever the wave advances. - Color is never the only signal — the active layer is also tagged with a textual
|∂L|magnitude. - The direction marker ("← gradients ←") above the diagram is
aria-hiddento avoid double-announcement. prefers-reduced-motion: reduceparks the wave on the input layer with no autoplay and no per-layer animation; the gradient readout for that layer stays fully visible.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/viz/GradientFlowViz.tsx). The source mixed an LCG-seeded forward simulation, an Explore/Predict mode strip with a six-question quiz pool, narration heuristics, an RMS bar chart, and theConstructionPrimitiveswidget chrome (ModeStrip,ChallengeBtn,FeedbackBadge,ScoreDots,DoneCard). The library extract distils to the dynamic essence: a backward-travelling wave through the layers with per-layer gradient magnitude. Stripped the simulation, the modes, the predict rounds, the activation/scale sliders, and the RMS axis; replaced project tokens (var(--color-warn-400)) with--cb-warningand inlineSPRINGS.snappywithSPRINGS.smoothfrom@craft-bits/core/motion. Sibling toGradientDecayExplorer(static bar chart) andBackpropFlow(forward+backward layer cursor).