Skip to content

Tabular, time-series, scientific, and geometric deep learning · Tabular deep learning · SPE001 · Architecture

TabNet

TabNet learns sequential, example-dependent feature selection for tabular prediction. It combines a neural predictor with sparse feature masks, allowing inspection of which columns contribute at each decision step;

Runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through learn.tabnet. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

TabNet learns sequential, example-dependent feature selection for tabular prediction. It combines a neural predictor with sparse feature masks, allowing inspection of which columns contribute at each decision step; those masks are model behavior, not causal explanations.

Question / hypotheses

The operative question is whether the specified representation, symmetry, likelihood or physical constraint improves held-out behavior for this data domain. This is a learning framework, not a null-hypothesis test; losses, attention weights and predictive confidence are not statistical p-values.

Architecture / computational structure

An attentive transformer produces a sparse feature mask, a feature transformer processes the masked row, and decision outputs accumulate across steps. A prior-scale state discourages uncontrolled reuse of the same features; optional reconstruction pretraining learns from unlabeled rows.

Assumptions

Feature meaning and categorical encodings must be consistent across datasets. Sparse attention can be unstable among correlated columns; masking does not prove causal importance. Batch-normalization behavior and small datasets materially affect training.

Formula / computational definition

Mi=sparsemax⁡(Pi−1⊙hi(ai−1))M_i=\operatorname{sparsemax}(P_{i-1}\odot h_i(a_{i-1})), Pi=Pi−1⊙(γ−Mi)P_i=P_{i-1}\odot(\gamma-M_i), and d=∑iReLU⁡(di)d=\sum_i\operatorname{ReLU}(d_i). Here MiM_i selects features, PiP_i tracks prior use, and γ\gamma controls reuse; supervised loss can include a mask-entropy penalty.

Parameters and notation

Learned or estimated: Feature-transformer weights, attentive projections, shared/step-specific block parameters, categorical embeddings and task-head weights.

Hyperparameters and effects: Decision steps, decision/attention widths, sparsity penalty, relaxation gamma, batch/virtual-batch size, optimizer and preprocessing. More steps increase interaction capacity and cost; larger gamma permits greater feature reuse.

Fitting, fine-tuning, and adaptation

Fit a supervised loss or pretrain reconstruction before fitting the head. For transfer with compatible schemas, initially freeze shared feature blocks and train the new head; then unfreeze at a smaller learning rate. Incompatible columns require a deliberate remapping, not arbitrary checkpoint loading. Validate source retention and recalibrate probabilities on separate target validation data.

How to use and calibrate

Define the independent evaluation unit before preprocessing: row/entity, whole set/object, molecule/trajectory, input function or chronological forecasting origin. Keep target construction and normalization within permitted training data. Compare with linear and boosted-tree baselines using identical leakage-safe splits. Report discrimination/error, calibration, mask stability across seeds, and robustness to missing values or correlated-feature substitutions.

Example application

Predict a laboratory outcome from mixed numeric and categorical measurements while examining per-row feature masks. This is a proposed use case, not a claim of validated performance on supplied real data.

Interpretation / pitfalls

Watch out

Assess model error against domain baselines and the stated assumptions. Component-level mathematical correctness does not establish paper reproduction or scientific validity. Use an independent validation design for model selection and a final held-out evaluation for claims; uncertainty and calibration require separate assessment.

Python and R code examples

Scope: Sparsemax feature selection and TabNet-style reuse prior; fixed scores, no training. Use the shared setup. Dependencies: NumPy / base R. Validation: Python executed; R unexecuted.

Full-model route: Python: an audited TabNet implementation. R: native torch custom blocks or a labeled reticulate bridge. The example implements the sparsemax mask and reuse update; it is not a complete trained TabNet.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Sparsemax feature selection and TabNet-style reuse prior; fixed scores, no training. Use the shared setup.

Shared setup for this chapter
import numpy as np
def sm(x):
    x=np.asarray(x); e=np.exp(x-x.max(axis=-1,keepdims=True)); return e/e.sum(axis=-1,keepdims=True)
def sparsemax(z):
    ordered=np.sort(z)[::-1]; cumulative=np.cumsum(ordered); k=np.arange(1,len(z)+1)
    active=k[1+k*ordered>cumulative][-1]; threshold=(cumulative[active-1]-1)/active
    return np.maximum(z-threshold,0)
scores=np.array([1.2,.8,-.5,.1]); mask=sparsemax(scores)
prior=np.ones(4); gamma=1.3; next_prior=prior*(gamma-mask)
assert np.isclose(mask.sum(),1) and np.any(mask==0)
print(mask,next_prior)
R

Component demo, not a trained model — Sparsemax feature selection and TabNet-style reuse prior; fixed scores, no training. Use the shared setup.

Shared setup for this chapter
sm <- function(x) {
  if(is.null(dim(x))) { e<-exp(x-max(x)); return(e/sum(e)) }
  t(apply(x,1,function(v) { e<-exp(v-max(v)); e/sum(e) }))
}
sparsemax<-function(z) {
  ordered<-sort(z,decreasing=TRUE); cumulative<-cumsum(ordered); k<-seq_along(z)
  active<-max(k[1+k*ordered>cumulative]); threshold<-(cumulative[active]-1)/active
  pmax(z-threshold,0)
}
scores<-c(1.2,.8,-.5,.1); mask<-sparsemax(scores); gamma<-1.3
next_prior<-rep(1,4)*(gamma-mask)
stopifnot(abs(sum(mask)-1)<1e-12,any(mask==0)); print(list(mask=mask,prior=next_prior))

Sources

From the CodeSplash reference, entry SPE001.