Skip to main content
ByFrank Hutter
Pretrain a tiny model in seconds and watch it learn decision boundaries it has never seen. This cookbook is fully self-contained and made for teaching: it builds a tiny tabular foundation model, using the TabPFN-2-style architecture of nanoTabPFN and simpler synthetic pretraining data. Here we implement this whole pretraining pipeline, in three parts:
  1. The prior – code for sampling new classification datasets every time it is called.
  2. The model – a transformer-like architecture that attends across the rows and the columns of a table.
  3. The training loop – learn to predict the label of new rows based on labeled rows.
The model and training loop are further simplified from nanoTabPFN (github.com/automl/nanotabpfn), a minimal re-implementation of the TabPFN v2 architecture.

Let’s pretrain a tiny TabPFN together!

0. Setup

Everything runs on plain PyTorch. A GPU (or Apple Silicon) makes pretraining faster, but a CPU works too. In Colab, pick a GPU runtime under Runtime → Change runtime type.

Plotting and evaluation helpers

These helpers pick the device, define three fixed test datasets that the model never sees in pretraining (a line, a slow sine wave and a sine + fast wave boundary, each with 40 labeled and 200 hold-out points), and plot and score predictions. They are not part of TabPFN itself, so feel free to skip ahead.

1. The prior: generating the datasets to pretrain on

We sample synthetic tables with 50 rows each, split randomly into train and test rows. We start simple: a random boundary line f(x1)=a+bx1f(x_1) = a + b x_1 with a,b∼N(0,1)a, b \sim \mathcal{N}(0, 1). We use x1∼N(0,1)x_1 \sim \mathcal{N}(0, 1) and x2∼f(x1)+N(0,1)x_2 \sim f(x_1) + \mathcal{N}(0, 1); the label is 1 if the point lies above the line and 0 below it, with a little noise near the boundary.
1. The prior: generating the datasets to pretrain on

2. The model: a transformer over table cells

Every cell of the table becomes a vector, so a batch of tables is a tensor of shape (batch, rows, columns, embedding). The label is appended as one more column: classes 0 and 1 enter the model as −1-1 and +1+1, so that 0 can mean “unknown” for the rows we want to predict. Each layer applies attention in two directions:
  • between features – within a row, cells attend to each other,
  • between datapoints – within a column, every row attends to the labeled rows.
The output for each test row is two logits, one per class. Their softmax is the predicted probability of each class, to quantify model uncertainty.

3. The pretraining loop

Every step in pretraining draws 32 fresh tables, gives the model the labeled rows, and scores its predicted class probabilities on the unlabeled rows with cross-entropy. Minimizing this loss is learning to predict in context. The learning rate decays to zero on a cosine schedule, and every 10 % of the steps we check accuracy on three test datasets the model never trained on.

A first, fast run

A small model (48-dimensional embeddings, 2 layers) pretrained for 750 steps: about 10 seconds on a GPU or Apple Silicon.

A simple evaluation on three datasets

We use three test datasets with different boundaries: a straight line, a slow sine wave, and a sine + fast wave. Our model has only seen data from lines during pre-training. The pretrained model sees 40 labeled points (red dots and blue crosses) as context; the background is its prediction everywhere else: red for class 1, blue for class 0, and white where the model is unsure.
A simple evaluation on three datasets We now compare our model’s accuracy on hold-out points against logistic regression and a random forest fitted on the same 40 points.

4. A richer prior

We now make the boundary functions a bit more complex by adding a sine wave on top of the line, with amplitude A∼U(0,2)A \sim \mathcal{U}(0, 2), frequency ω∼U(0.5,5)\omega \sim \mathcal{U}(0.5, 5) and phase φ∼U(0,2π)\varphi \sim \mathcal{U}(0, 2\pi). The new boundary is f(x1)=a+bx1+Asin⁡(ωx1+φ)f(x_1) = a + b x_1 + A \sin(\omega x_1 + \varphi).

A run with the richer prior

A run with the richer prior

Results with the richer prior

Results with the richer prior

5. Scaling up

We now scale up by doubling three knobs: a wider embedding (48 → 96), more layers (2 → 4) and more training steps (750 → 1,500). This run takes about 1 minute on a GPU or Apple Silicon.

Evaluating the larger model

The large model now follows the wavy boundary. The white uncertainty band runs along the boundary and gets wider where labeled points of the two classes are far apart. The model also beats logistic regression and random forests on this data.
Evaluating the larger model

Next steps

Ready for more complex data? Continue with Pretrain a Tabular Foundation Model from Scratch, which uses a richer random-network prior and evaluates zero-shot on real datasets.