Learning Diagrams: A Graphical Language for Compositional Training Regimes

Mason Lary, Richard Samuelson, Alexander Wilentz, Alina Zare, Matthew Klawonn, James FairbanksICLR 2025

TL;DR

This paper introduces learning diagrams, a graphical language that treats complex machine learning training setups as structured data rather than hard-coded scripts. By leveraging category theory, these diagrams automatically compile into unique loss functions, allowing researchers to build, compose, and manipulate sophisticated multi-model regimes—like knowledge distillation and few-shot learning—with mathematical rigor and modular ease.

The Problem: Pipelines as Code

In modern deep learning, we rarely train a single model in isolation. We often juggle "training regimes" involving multiple interacting components: a teacher distilling knowledge to a student, a vision encoder feeding a language model, or a shared backbone trained across multiple tasks.

Currently, these regimes are typically implemented as "pipelines as code." This approach is brittle; it makes it difficult to swap architectures, reuse specific sub-workflows, or formally verify that a complex setup is doing what we think it is. The authors argue for a shift to "pipelines as data," where the relationships between models, datasets, and loss functions are explicitly defined in a structured, graphical format that can be manipulated programmatically.

Methods: A Category Theoretic Approach

The authors introduce a formalism based on Category Theory to give these diagrams a rigorous mathematical foundation. Here is the breakdown of their approach:

  • Learning Graphs: A training setup is represented as a directed graph. Nodes represent spaces (like image sets or label categories), and edges represent maps between them (like neural networks or data loading functions).
  • Commuting Diagrams: The core philosophy is that machine learning is a search for "commuting diagrams." In simple terms, if there are two different paths in the graph to get from point A to point B, training aims to make the outputs of those two paths as similar as possible.
  • Lawvere Metric Spaces: To make this mathematically sound, the authors treat spaces as "Lawvere metric spaces," which allow for asymmetric distances (useful for loss functions like Cross-Entropy).
  • Compilation to Loss: A learning diagram "compiles" into a single, global loss function. This loss is essentially the sum of the "disagreements" between all parallel paths in the diagram that the user has specified should "agree."
  • Composition via Colimits: This is the "Lego-like" feature. Using a categorical operation called a colimit, researchers can "glue" simple diagrams together to form complex ones (e.g., joining two separate task diagrams at a shared encoder node).
Composition of learning diagrams using colimits
  • Implementation: They provide a Julia library, DiagrammaticLearning.jl, which supports models from both PyTorch and Flux.jl.

Key Insights and Ideas

  • Syntax vs. Semantics: By separating the structure of the training (the graph) from the implementation (the specific weights and code), you can swap a ResNet for a Vision Transformer (ViT) by simply changing one edge's assignment without rewriting the training loop.
  • Loss as a Property of the Graph: Instead of manually writing loss = criterion(pred, target), the loss is automatically derived from the paths in the diagram. If the diagram says Path A should equal Path B, the system generates the necessary math.
  • Formal Manipulation: Because the regimes are data, you can use "graph homomorphisms" to find specific patterns within a large training setup and modify them (e.g., setting all vision encoders in a complex multi-modal system to "eval" mode simultaneously).

Key Results

The researchers demonstrated that their high-level abstraction doesn't sacrifice performance. In fact, it often makes it easier to improve results by facilitating rapid experimentation:

  • Image Captioning: By easily swapping the original GoogLeNet encoder for a more modern ViT-B-16 within the diagram, they achieved a BLEU score of 71.39 on Flickr8k, significantly outperforming the original 2015 paper's score of 63.
Image captioning performance across different encoders
  • Knowledge Distillation: Their "compiled" diagram produced a distilled student model with 69.21% accuracy on CIFAR-10, comparable to the 70.81% achieved by the official, manually-coded PyTorch implementation.
  • Few-Shot Learning: They successfully replicated the complex "SimpleShot" baseline on datasets like Mini-ImageNet and Cifar-FS, showing that the framework can handle the intricate data-sampling and multi-task requirements of meta-learning.

Limitations and Future Work

While promising, the framework has a few current limitations:

  • Finite Data Constraint: Currently, the mathematical formalism is primarily designed for finite datasets (where loss is a sum). Extending this to continuous distributions is a necessary next step.
  • Mini-batching: While the software implementation uses mini-batches, the current category-theoretic proofs don't explicitly account for the stochastic nature of modern training algorithms.
  • Optimization Semantics: The paper focuses on the semantics of the loss function, but not yet on the semantics of the optimizer (like Adam or SGD) or the gradient descent process itself.
  • Scalability: While powerful for complex multi-model setups, the overhead of defining a diagram might be overkill for a simple, single-model classifier.

Summary updated

BibTeX
@inproceedings{lary2025learning,
  author = {Mason Lary and Richard Samuelson and Alexander Wilentz and Alina Zare and Matthew Klawonn and James Fairbanks},
  title = {{Learning Diagrams: A Graphical Language for Compositional Training Regimes}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=dqyuCsBvn9},
}
Figure