torchtsetlin: Fast, Interpretable Tsetlin Machines for PyTorch
Organizations: SimulaMet, Norway · University of Agder, Norway
Abstract
The Tsetlin machine learns propositional rules with teams of Tsetlin automata that are driven by reward and penalty rather than gradients, so its models are interpretable by construction and cheap to evaluate. Its software, however, consists of C and CUDA kernels behind scikit-learn-style wrappers that sit outside the deep learning ecosystem. torchtsetlin implements the Tsetlin machine family as ordinary PyTorch modules: vanilla and weighted, coalesced, regression, convolutional over sequences, images and volumes, and dense per-pixel segmentation. The automata are a tensor buffer, so a model moves with .to(device), is saved with state_dict() and learns from a mini-batch with one update(x,y) call. Its core technique is a batched formulation of Type I and Type II feedback, in which automaton transitions become event counts realised as binomial draws, with one commit per mini-batch. On ten benchmarks taken from the existing libraries, it matches their accuracy, and on all but the two smallest it trains 2.4-10.9X faster than PyTsetlinMachineCUDA and 10.3-81.2X faster than tmu on the same GPU. The package is pure Python, MIT-licensed and on PyPI; every number in this paper is reproduced by one script. Code: https://github.com/vlbthambawita/torchtsetlin; package: https://pypi.org/project/torchtsetlin/.
Figures & tables
| pyTM | pyTM-CUDA | tmu | GT | torchtsetlin | |
|---|---|---|---|---|---|
| Backend | C | PyCUDA | C / PyCUDA | C++ | PyTorch |
| CPU and GPU, one class | ✗ | ✗ | ✓ | ✗ | ✓ |
| Conv. 1-D / 2-D / 3-D | ✗ ✓ ✗ | ✗ ✓ ✗ | ✗ ✓ ✗ | ✗ ✗ | ✓✓✓ |
| Regression / coalesced | ✓ ✗ | ✓ ✗ | ✓✓ | ✗ ✓ | ✓✓ |
| Dense segmentation | ✗ | ✗ | ✗ | ✗ | ✓ |
| Graphs (message passing) | ✗ | ✗ | ✗ | ✗ | ✓ |
| ID | Dataset (task) | Booleanisation; model ( , , ) | Epochs runs | Source of the experiment |
|---|---|---|---|---|
| B1 | Noisy XOR | 12 bits, 40% label noise; TM (10, 15, 3.9) | 200 5 | pyTM, pyTM-Par, pyTM-CUDA demos; Granmo (2018, Tab. 15) |
| B2 | 2-D Noisy XOR | 4 4 images; conv. TM (40, 60, 3.9), patch 2 2 | 5000 5 | pyTM, pyTM-Par demos; Granmo et al. (2019) |
| B3 | Breast cancer ( Wolberg et al., 1995 ) | 10 bits/feature; TM (800, 40, 5.0) | 25 100 splits | pyTM, pyTM-Par demos |
| B4 | MNIST ( LeCun et al., 1998 ) | pixel ; TM (2000, 50, 10) | 250 5 | pyTM, pyTM-Par demos |
| B5 | MNIST | pixel ; w-TM (2000, 5000, 10) | 100 5 | pyTM, pyTM-Par, pyTM-CUDA demos; tmu example; Phoulady et al. (2019) |
| B6 | MNIST, timing | pixel ; , , , budget 20 | 5 3 | Glimsdal et al. (2024, Tab. I) (cross-library); Abeyrathna et al. (2021) |
| existing libraries | torchtsetlin (GPU) | |||||||
|---|---|---|---|---|---|---|---|---|
| Bench | pyTM | Par | CUDA | tmu | GT | seq | batch | b100 |
| B1 | 100.00 | 100.00 | 100.00 0.00 | 100.00 0.00 | 54.94 | 100.00 0.00 | 100.00 0.00 | 100.00 0.00 |
| B2 | 98.62 | 99.97 | 98.82 1.22 | 99.73 0.56 | – | 99.60 0.46 | 99.61 0.27 | 92.47 3.83 |
| B3 | 99.12 | 98.25 | 97.50 1.30 | 97.32 1.25 | 97.37 | 97.49 1.37 | 97.42 1.48 | 96.86 1.74 |
| B4 | 98.02 | 98.08 | 98.21 0.05 | 97.97 0.15 | 98.05 | – | 97.90 0.12 | 97.54 0.13 |
| B5 | 98.12 | 98.07 | 98.11 0.08 | 98.17 0.12 | 10.49 | – | 98.15 0.08 | 98.14 0.06 |
| CPU | GPU (RTX 3090) | |||||||||
| torchtsetlin | ||||||||||
| Bench | pyTM 1 | Par 16 | GT 16 | tmu 1 | torchtsetlin 16 | CUDA | tmu | seq | batch | b100 |
| B1 | 0.00786 | 0.00657 | 0.417 | 0.445 | – | 0.0136 | 1.13 | 0.112 | 0.0421 | 0.0136 |
| B2 | 0.0118 | 0.00741 | – | 0.226 | – | 0.00884 | 0.579 | 0.102 | 0.0439 | 0.00902 |
| B3 | 0.0337 | 0.00643 | 0.0434 | 0.0766 | – | 0.00635 | 0.125 | 0.0163 | 0.00426 | 0.00153 |
| B4 | 9.32 | 2.99 | 46.3 | 32.4 | 69.6 | 3.44 | 19.3 | – | 1.59 | 0.788 |
| Library | acc. (%) | |||||
|---|---|---|---|---|---|---|
| pyTM CPU 1 | 9.07 (4.5) | 20.1 (9) | 95.8 (47) | 174 (84) | 422 (197) | 96.6 0.0 |
| pyTM-Par CPU 4 | 2.25 (1.1) | 5.48 (2) | 37.1 (17) | 97.3 (47) | 262 (149) | 96.9 0.2 |
| pyTM-Par CPU 16 | 1.61 | 2.64 | 11.8 | 36.9 | 115 | 96.8 0.1 |
| GT CPU 1 | 1.04 (0.3) | 3.60 (2) | 35.0 (18) | 70.6 (36) | 177 (90) | 89.6 0.3 |
| GT CPU 4 | 12.8 (4) | 11.9 (5) | 26.0 (11) | 42.9 (18) | 97.1 (31) | 89.1 0.4 |
| GT CPU 16 | – | 29.4 | 36.4 | 45.7 | 79.2 | 89.0 0.6 |
| ID | Library and check | Acc. (%) | Train s/epoch |
|---|---|---|---|
| R1 | pyTM, B5 (README) | 98.16 (98.19) | 8.38 (8.8) |
| R2 | pyTM-Par 10, B5 (README) | 98.16 (98.14) | 4.43 (1.7) |
| R3 | pyTM-CUDA, B8 8000 clauses (README) | 90.70 (90.94) | 42.2 (44.02) |
| R4 | tmu, B5 (example) | 98.22 | 33.1 |
| R6 | torchtsetlin seq, B1 ( Granmo, 2018 , Tab. 15) | 100.00 (99.30) | – |
Interpretable ML
Interpretable machine learning develops models and explanations that make predictions, evidence, and decision processes understandable to people.
See all 336 papers