Skip to content

TorchLens for teaching how networks work

Teaching needs a picture of the whole computation, not a module list: every operation the forward pass actually ran, in order, with the shapes flowing between them, so a student can follow a residual connection, an attention head or a recurrent loop from input to output. TorchLens draws that picture from one call on any PyTorch model, lets a student click into any single operation to see its inputs, outputs and source line, and does the same for a 1943 threshold-neuron network and a modern transformer alike.

The lists below start with drawing and inspecting, then move to the tutorial notebooks and the Model Menagerie. Every link is to a file in this repository or to the Menagerie site.

Start here

  • See the graph and inspect any operation: one call draws every operation the forward ran, including ones no module returns; one lookup gives any operation's output, module and arguments.
  • Visualize notebook: render a model graph and learn to read the layer labels.
  • Tutorial notebooks: eleven short notebooks on laptop-sized models, each one rung up from the last. Notebook 01 is "capture and look", 02 reads one activation like a dict, 03 draws the graph when the summary is too long to read. They run on a laptop CPU in about a minute each, or in the browser on Colab, so they work as lab exercises.
  • TorchLens in 10 minutes: trace, intervene, run a backward pass, see control flow drawn as IF, THEN and ELSE, and compare two traces.

Drawing the graph

Inspecting every operation

Hands-on exercises

The Model Menagerie

  • The Model Menagerie: a browsable gallery of over 6,600 neural network architectures, from McCulloch and Pitts's 1943 network of threshold neurons to current models, each with its full computational graph drawn by TorchLens. For a course it is a ready-made source of worked drawings: show the same operation vocabulary across eight decades of designs.
  • The same three lines on any architecture: the trace, draw and edit calls on six architecture families from the Menagerie, the shortest proof that the tool works on whatever a student brings.
  • Model summaries: a one-call table of every layer with shapes and parameter counts, for the first lecture before the graph.

Further reading