Visualizations
455 / 500

455 · Machine learning

Autograd Circuit

Type a formula and watch reverse-mode autodiff run on a printed circuit board.

A typed expression is parsed by recursive descent and compiled into a computational graph, with identical subexpressions merged so a value can fan out and its gradient arrive from several paths. The graph is laid out as a circuit board by a small Sugiyama pipeline: longest-path layers, vias for edges that skip a layer, barycentre sweeps to reduce crossings and left-edge track assignment so the chamfered copper traces never overlap. Inputs are rotary knobs, operations are chips showing their value in amber and their gradient in cyan, and each input pin is tagged with the local derivative the backward pass multiplies by. Amber current flows forward while cyan gradient flows back, a periodic sweep replays both passes in the order the algorithm visits the nodes, and the panel checks every gradient against a finite difference and plots the function with the tangent its gradient predicts.

Try it. Drag a knob up or down to change an input and every value and gradient updates at once. Click a chip to see its local derivatives spelled out, click a gradient row to plot along that variable, and pick a preset (a two-unit network that reuses w*x, a neuron, a shared subexpression, a logistic loss and more). Type (or click the expression box, which selects it) to write your own expression with + - * / ^ and functions like sin, exp, log, tanh, sigmoid and relu, then press Enter. The left and right arrow keys switch preset, up and down nudge the focused input, and Tab changes it.

  • Reverse-mode automatic differentiation
  • Recursive descent parsing
  • Common subexpression elimination
  • Sugiyama layered graph layout
  • Left-edge channel routing

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build an interactive autograd visualizer: I type a math expression and see its computational graph, with values flowing forward and gradients flowing backward. Use JavaScript and the HTML canvas element, in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Give it a dark green circuit-board background.
- Write a small recursive descent parser for + - * / ^, parentheses, numbers, variable names and a few functions (sin, exp, log, tanh). Build a graph of nodes, reusing the same node for repeated variables.
- Forward pass: evaluate nodes in topological order. Backward pass: set the output's gradient to 1, then walk the nodes in reverse, and for each node add its gradient times each local derivative (for a*b that is b and a) into its inputs.
- Lay the graph out in columns by depth from the inputs, draw each operation as a box showing its value and gradient, and connect them with lines. Draw each variable as a knob I can drag up and down to change its value.

Once that works, make it beautiful:
- Route the connections as right-angled traces with 45 degree corners, like copper on a circuit board, and animate amber dashes moving forward and cyan dashes moving backward along them.
- Label each input pin with the local derivative used there, and add a periodic sweep that lights up the nodes in forward order and then in backward order.
- Check every gradient against a central finite difference and show both.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as merging repeated subexpressions so gradients accumulate from several paths, a plot of the function with its tangent line, or reducing wire crossings with a barycenter ordering.
PreviousEye OpticsReal ray tracing through a model human eye that focuses, blurs and wears glasses. NextJoukowski AirfoilExact flow past a wing, made by conformally mapping flow around a circle.

Related visualizations

  • Syntax RailroadType and image A recursive descent parser drives a train through railroad syntax diagrams.
  • PCB AnnealerAlgorithms Simulated annealing places chips on a circuit board, then a maze router lays copper.
  • Symbolic RegressionMachine learning Genetic programming breeds formulas until it rediscovers the law behind the data.
  • Circuit SimulatorPhysics A SPICE-style solver runs a live schematic: LEDs blink, capacitors charge, current flows.
  • RRT* ParkingAlgorithms A self-driving car grows a tree of curved manoeuvres, then reverses into its spot.
  • Diff GraphAlgorithms Myers' O(ND) diff, the algorithm behind git diff, as a search through the edit graph.
  • Diffusion SketchpadMachine learning A diffusion model trains live and condenses pure noise into whatever shape you draw.
  • Ghost RacersMachine learning Sixty neural-net cars evolve to race a track, then face one you draw.
  • Growing Neural CellsMachine learning Cells running one tiny learned rule grow a gecko from a single seed and heal its wounds.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs