xarray-sql demo trains a Fashion-MNIST MLP entirely in SQL
TL;DR
- The benchmark trains a 784→196→32→10 MLP on Fashion-MNIST with forward pass, backpropagation, and SGD all expressed as SQL queries.
- A SKIP_ZERO_PIXELS flag drops background pixels via a WHERE clause and delivers roughly a 1.8x speedup on Fashion-MNIST.
- Hyperparameters are demo-scale: 500 training samples, 200 test samples, learning rate 0.5, 60 steps, chunk size 250.
Training a neural network with SQL sounds like a party trick, but the nn.py benchmark in the xarray-sql project takes it seriously. The file wires up a three-layer multilayer perceptron for Fashion-MNIST with dimensions 784, 196, 32, and 10, and runs the whole loop — forward pass with tanh activations and linear output, softmax cross-entropy backpropagation, and SGD weight updates in a single unified query — as relational operations on xarray Datasets that have been pivoted into tables.
The tuning knobs give away that this is a demo, not a production trainer. It trains on 500 samples and tests on 200, with a learning rate of 0.5, 60 steps, and a chunk size of 250. Bias units are folded in as constant-1 rows in the weight matrices, which is what lets SGD collapse into one SQL statement. Every five steps the loop logs cross-entropy loss and split-wise accuracy, and the trained weights get exported back to zarr.
The one optimisation worth pausing on is a SKIP_ZERO_PIXELS flag that adds a WHERE clause to drop background pixels from the compute path. Roughly half of Fashion-MNIST pixels are zero, and skipping them yields about a 1.8x speedup. That is the pitch for expressing ML this way in miniature: a sparsity pattern that would usually mean a custom code path becomes one clause.
The honest caveat is what the file is not. It is a small demo on an experimental project — upstream xarray-sql describes itself as "an experiment to provide a SQL interface for array datasets" — and the benchmark does not include accuracy at convergence, a comparison against PyTorch or JAX, or any test at larger sample counts. Take the 1.8x figure as reported on this specific setup, not as a general claim about SQL-based training.
Still, the direction is interesting for anyone whose data already lives in xarray or zarr on cloud storage. If small-model training and inference can be expressed as SQL over the same tables the analytics stack already reads from, you can skip standing up a separate ML environment for the toy end of the workload. Narrow, but real.
Shared on Bluesky by 2 AI experts
-
Sneak peek: I implemented a 3 layer MLP that predicts the fashion MNIST dataset using SQL (with my own autograd impl inside the database). Apparently einsum notation is better expressed as a JOIN. github.com/xqlsystems/…
View on Bluesky →
Originally reported by github.com
Read the original article →Original headline: xarray-sql/benchmarks/nn.py at claude/xarray-sql-mnist-demo · xqlsystems/xarray-sql