Skip to content

Benchmark datasets

torchtsetlin.datasets ships loaders for the datasets of published Tsetlin-machine benchmarks. Like torchvision datasets, they download from the original source the first time you use them and read a local cache afterwards:

import torchtsetlin as tt

train = tt.datasets.HIGGS(root="./data", train=True, download=True)   # downloads once
test = tt.datasets.HIGGS(root="./data", train=False)                  # read from the cache

model = tt.TsetlinMachine(train.n_features, train.n_classes, 100, T=1000, s=1.0, weighted=True)
trainer = tt.Trainer(model, batch_size=50)
trainer.fit(train.tensors(), epochs=10)
print(trainer.test(test.tensors()))

root defaults to the TORCHTSETLIN_DATA environment variable, else ./data. Without download=True a missing dataset raises RuntimeError instead of reaching the network.

The TM-MCAF benchmarks

These are the 15 datasets of Dumbre, Shende, Granmo and Jiao, Pruning Redundant Literals in Tsetlin Machines via Minimal Co-Firing Clauses (arXiv 2612.01468). Each loader uses that paper's preprocessing by default.

Paper name Loader Original source Booleanization (default) Split (default)
PIGS nodes p48124091, p82265990, p82154888 BayesianNetworkDataset("pigs", target) bnlearn pigs.bif.gz (441 nodes), sampled locally one-hot per node stratified 80/20
HAILFINDER nodes WindFieldMt, CombMoisture, CurPropConv BayesianNetworkDataset("hailfinder", target) bnlearn hailfinder.bif.gz (56 nodes), sampled locally one-hot per node stratified 80/20
HIGGS HIGGS OpenML 23512 (98,050 rows, 28 features) 10 quantile bins, one-hot stratified 80/20
ELECTRICITY Electricity OpenML 151 (45,312 rows, 8 features) 10 quantile bins, one-hot stratified 80/20
MAGIC MagicTelescope OpenML 1120 (19,020 rows, 10 features) 10 quantile bins, one-hot stratified 80/20
PHISHING PhishingWebsites OpenML 4534 (11,055 rows, 30 categorical features) one-hot stratified 80/20
MNIST, FMNIST, KMNIST BooleanImageDataset(name) torchvision (needs the vision extra) pixel > 75 official 60k/10k
IMDB IMDB Keras imdb.npz multi-hot, 5,000 most frequent words official 25k/25k
REUTERS Reuters Keras reuters.npz, 10 most frequent topics multi-hot, 5,000 most frequent words Keras 80/20

tt.datasets.BayesianNetworkDataset.PAPER_TARGETS lists the six target nodes, and examples/tm_mcaf_datasets.py loads all 15 datasets and trains a weighted machine at the paper's settings.

What the paper leaves open

The paper does not specify every step, so the loaders make these documented choices:

  • Bayesian-network samples: the paper does not say how many samples it drew. The default is n_samples=10_000 forward samples with sample_seed=0, cached per (n_samples, sample_seed). Every node except the target is an input.
  • Image split: the paper names only IMDB and REUTERS as using their native split, so it probably split the 70,000 images 80/20 as well. The default is the official split; split="paper" gives the stratified 80/20 split over all 70,000 images.
  • Class balance: the paper oversamples the training split with RandomOverSampler. Pass oversample=True for the same random oversampling (seeded, training split only).
  • Missing values: the 9 rows of HIGGS with a missing value are dropped.
  • Text vocabulary: the num_words cut follows Keras: indices 0 to 2 are padding, start and out-of-vocabulary, a word of rank r is index r + 3, and every index at or above num_words becomes 2. The tests check this against the Keras load_data rules.

Choosing the encoding

Every loader takes encoder=: any Booleanization encoder (or a callable returning Booleans) replaces the paper's default. The loader fits a copy of it on the training split, also when you build the test split, so both splits share thresholds and your own object stays unchanged:

enc = tt.data.ThermometerEncoder(8)
train = tt.datasets.HIGGS(root, encoder=enc)
test = tt.datasets.HIGGS(root, train=False, encoder=enc)          # same thresholds as train

tt.datasets.BooleanImageDataset("fashion-mnist", root, encoder=tt.data.AdaptiveThresholdEncoder(11, 2.0))
tt.datasets.IMDB(root, encoder=tt.data.HypervectorEncoder(5000, dim=2048))
Loader What the encoder receives
tables (HIGGS, Electricity, MagicTelescope) the numeric columns as (N, F) floats; categorical columns stay one-hot and are appended
all-categorical tables (PhishingWebsites, BayesianNetworkDataset) every column as category codes (N, F)
BooleanImageDataset float images (N, 1, 28, 28) scaled to [0, 1] (threshold is then unused)
IMDB, Reuters the word indices of a batch of documents, (n, max_len) padded with -1 (the input of HypervectorEncoder), after the num_words cut

The encoder must return Boolean (or 0/1) values. dataset.encoder is the fitted copy. For tables, feature_names come from the encoder's feature_names(names) when it has one (ThermometerEncoder does, e.g. "m_bb>=0.86"), otherwise they read "column#k"; the one-hot categorical bits keep names such as "day=3".

Smaller changes need no encoder: n_bins= (tables), threshold= (images) and num_words= (text).

Using the loaded data

Every dataset exposes the same attributes:

Attribute Meaning
x, targets the Boolean features and the labels of this split (tensors() returns both)
classes, n_classes, n_features label names and sizes
feature_names a name per Boolean feature, e.g. "m_bb in [0.86, 1.02)", "day=3" or a word
data raw values of the split (tabular and images)
encoder the fitted QuantileOneHotEncoder (tabular); fitted on the training split, also for train=False
feature_groups the raw column of every Boolean feature (tabular), for per-column importance

Set model.feature_names = train.feature_names and the rules print in the original units. BayesianNetworkDataset.markov_blanket() returns the target's Markov blanket from the network structure, which is the ground truth the paper uses to count irrelevant literals in a clause. Pass booleanize=False to get the raw values (tabular) or word-index sequences (text) and encode them yourself.

Downloads and the cache

  • Each raw file is checked against a pinned SHA-256 after the download and only then moved into root/<Name>/raw/. A failed or corrupted download leaves nothing behind.
  • The parsed data is stored in root/<Name>/processed/ and loaded on every later use, with no network access and no checksum. Delete that folder to rebuild it from the raw files.
  • The Keras .npz files hold ragged arrays that NumPy can only read with allow_pickle=True. The loaders open them only after the checksum has matched the pinned value.
  • MNIST, Fashion-MNIST and KMNIST are downloaded and cached by torchvision itself, in root/MNIST, root/FashionMNIST and root/KMNIST.
  • The datasets keep their own licences and are not redistributed with torchtsetlin. OpenML lists the four tables as public; check each source before redistributing derived data.