torchtsetlin.datasets¶
Ready-made datasets. Encoders, splits and wrappers for your own data are in
torchtsetlin.data; the guide is Benchmark datasets.
The generators and loaders below were in torchtsetlin.data up to 0.8.6 and remain
importable from there.
Benchmarks that download on first use¶
torchtsetlin.datasets.benchmarks ¶
Benchmark datasets that download themselves on first use (issue #54).
The 15 benchmarks of TM-MCAF (Dumbre, Shende, Granmo and Jiao, "Pruning Redundant Literals in Tsetlin Machines via Minimal Co-Firing Clauses", arXiv 2612.01468), with the paper's Booleanization as the default:
| Class | Source (downloaded on first use) | Booleanization |
|---|---|---|
HIGGS |
OpenML 23512 | 10 quantile bins, one-hot |
Electricity |
OpenML 151 | 10 quantile bins, one-hot |
MagicTelescope |
OpenML 1120 | 10 quantile bins, one-hot |
PhishingWebsites |
OpenML 4534 | one-hot |
BayesianNetworkDataset |
bnlearn pigs / hailfinder networks, sampled locally |
one-hot |
IMDB |
Keras imdb.npz |
5,000-word multi-hot |
Reuters |
Keras reuters.npz (10 most frequent topics) |
5,000-word multi-hot |
BooleanImageDataset |
torchvision MNIST, Fashion-MNIST, KMNIST | pixel > 75 |
Like torchvision datasets, the first use with download=True fetches the original files
into root/<Name>/raw/ (each checked against a pinned SHA-256) and writes a processed
copy to root/<Name>/processed/; later uses load the processed copy and never touch the
network. root defaults to $TORCHTSETLIN_DATA or ./data.
The Booleanization is always fitted on the training split, also for train=False, so the
two splits share thresholds. x/targets hold the split; :meth:tensors returns them.
BayesianNetworkDataset ¶
BayesianNetworkDataset(network: str, target: str, root=None, train: bool = True, download: bool = False, n_samples: int = 10000, sample_seed: int = 0, booleanize: bool = True, test_size: float = 0.2, split_seed: int = 0, oversample: bool = False, device=None, encoder: Any = None)
Bases: _TableDataset
Predict one node of a bnlearn Bayesian network from all the others.
The network (BIF file) is downloaded from the bnlearn repository; n_samples forward
samples are drawn with sample_seed and cached, so the same arguments always give
the same data. Every other node is an input, one-hot encoded over its states.
The TM-MCAF paper uses the targets in :attr:PAPER_TARGETS and does not state how many
samples it drew; 10,000 is this library's default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
str
|
|
required |
target
|
str
|
the node to predict, e.g. |
required |
n_samples
|
int
|
number of forward samples (before the split). |
10000
|
sample_seed
|
int
|
seed of the sampling. |
0
|
encoder
|
Any
|
an encoder for the state codes of all input nodes instead of the one-hot encoding (fitted on a copy, on the training split). |
None
|
Other arguments as for
|
class: |
required |
Source code in src/torchtsetlin/datasets/benchmarks.py
markov_blanket ¶
Markov blanket (parents, children, children's other parents) of node
(default: the target), the ground truth for the paper's Table 4 analysis.
Source code in src/torchtsetlin/datasets/benchmarks.py
IMDB ¶
IMDB(root=None, train: bool = True, download: bool = False, num_words: int = 5000, booleanize: bool = True, oversample: bool = False, device=None, encoder: Any = None)
Bases: _KerasText
IMDB movie reviews, positive vs negative, as Keras ships them (imdb.npz, the
word-index version of Maas et al. 2011): 25,000 training and 25,000 test reviews.
x is the multi-hot vector over the num_words most frequent words (Keras indices:
0 padding, 1 start, 2 out-of-vocabulary, rank + 3 for words), as in TM-MCAF with
num_words=5000. :attr:feature_names gives the word of every position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root, train, download, oversample, device
|
as for :class: |
required | |
num_words
|
int
|
vocabulary size (Keras |
5000
|
booleanize
|
bool
|
|
True
|
encoder
|
Any
|
an encoder instead of the multi-hot vector. It gets the word indices of a
batch of documents as a |
None
|
Source code in src/torchtsetlin/datasets/benchmarks.py
Reuters ¶
Reuters(root=None, train: bool = True, download: bool = False, num_words: int = 5000, top_classes: Optional[int] = 10, test_split: float = 0.2, booleanize: bool = True, oversample: bool = False, device=None, encoder: Any = None)
Bases: _KerasText
Reuters newswire topics as Keras ships them (reuters.npz: 11,228 newswires, 46
topics), split as Keras does (shuffle with seed 113, first 80 % for training).
top_classes=10 (the default, as in TM-MCAF) keeps the 10 topics most frequent in the
training split and relabels them 0..9 in increasing topic id; :attr:topics gives
the original Keras topic ids. top_classes=None keeps all 46.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root, train, download, num_words, booleanize, oversample, device, encoder
|
as for
:class: |
required | |
top_classes
|
Optional[int]
|
number of most frequent topics to keep, or |
10
|
test_split
|
float
|
Keras |
0.2
|
Source code in src/torchtsetlin/datasets/benchmarks.py
BooleanImageDataset ¶
BooleanImageDataset(name: str, root=None, train: bool = True, download: bool = False, threshold: int = 75, split: str = 'official', test_size: float = 0.2, split_seed: int = 0, oversample: bool = False, flatten: bool = False, device=None, encoder: Any = None)
Bases: _CachedDataset
MNIST, Fashion-MNIST or KMNIST, thresholded to one bit per pixel.
torchvision downloads and caches the images (root/MNIST, root/FashionMNIST,
root/KMNIST); install the vision extra. A pixel is True when its 8-bit
intensity is above threshold (75 in TM-MCAF).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
|
required |
split
|
str
|
|
'official'
|
flatten
|
bool
|
|
False
|
encoder
|
Any
|
an encoder instead of the threshold, e.g. |
None
|
Other arguments as for
|
class: |
required |
Source code in src/torchtsetlin/datasets/benchmarks.py
Loaders for torchvision datasets¶
torchtsetlin.datasets.loaders ¶
Loaders that booleanize torchvision datasets into in-memory tensors.
load_torchvision_boolean ¶
load_torchvision_boolean(dataset_cls, root: str, train: bool, encoder: Optional[Callable] = None, threshold: float = 0.3, device=None, download: bool = True, max_samples: Optional[int] = None, **kwargs) -> Tuple[Tensor, Tensor]
Load a torchvision dataset fully into memory and booleanize it.
encoder receives a float tensor (N, C, H, W) scaled to [0, 1] and must return
a Boolean tensor. By default pixels are thresholded at threshold (0.3, as in the
original MNIST experiments). Returns (x_bool, y_long) on device.
Source code in src/torchtsetlin/datasets/loaders.py
load_mnist_boolean ¶
load_mnist_boolean(root: str = './data', train: bool = True, threshold: float = 0.3, encoder: Optional[Callable] = None, device=None, flatten: bool = False, max_samples: Optional[int] = None) -> Tuple[Tensor, Tensor]
MNIST as Boolean tensors (N, 1, 28, 28) (or (N, 784) with flatten=True).
Requires torchvision.
Source code in src/torchtsetlin/datasets/loaders.py
load_segmentation_boolean ¶
load_segmentation_boolean(dataset: Dataset, encoder: Optional[Callable] = None, size: Optional[Union[int, Tuple[int, int]]] = None, n_bits: int = 4, device: Optional[Union[str, device]] = None, max_samples: Optional[int] = None, ignore_index: int = 255, remap: Optional[dict] = None) -> Tuple[Tensor, Tensor]
Load a torchvision segmentation dataset into memory as Boolean planes + label maps.
Segmentation datasets yield (image, mask) PIL pairs rather than the packed tensors
:func:load_torchvision_boolean expects, and the mask must be resized with nearest
interpolation — bilinear would invent class indices that do not exist. This handles both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
an instantiated torchvision segmentation dataset, e.g.
|
required |
encoder
|
Optional[Callable]
|
maps a float image batch |
None
|
size
|
Optional[Union[int, Tuple[int, int]]]
|
|
None
|
n_bits
|
int
|
thermometer levels per channel for the default encoder. |
4
|
device
|
Optional[Union[str, device]]
|
device for the returned tensors. |
None
|
max_samples
|
Optional[int]
|
stop after this many images. |
None
|
ignore_index
|
int
|
mask value to keep as the void class. Pass the same value as the
model's |
255
|
remap
|
Optional[dict]
|
optional |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Source code in src/torchtsetlin/datasets/loaders.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
load_voc_segmentation_boolean ¶
load_voc_segmentation_boolean(root: str = './data', image_set: str = 'train', size: Union[int, Tuple[int, int]] = 96, n_bits: int = 4, device=None, download: bool = True, max_samples: Optional[int] = None, **kwargs) -> Tuple[Tensor, Tensor]
Pascal VOC 2012 segmentation as Boolean planes + label maps (21 classes + void).
Void pixels keep the value 255, so pass ignore_index=255 to the model. Requires
torchvision.
Source code in src/torchtsetlin/datasets/loaders.py
Generated data¶
torchtsetlin.datasets.synthetic ¶
Generated Boolean datasets: XOR variants, shapes, volumes and segmentation scenes.
make_xor ¶
make_xor(n: int, n_features: int = 2, seed: Optional[int] = 0, device=None) -> Tuple[Tensor, Tensor]
Random Boolean vectors labelled by x0 XOR x1 (other features are distractors).
Source code in src/torchtsetlin/datasets/synthetic.py
make_noisy_xor ¶
make_noisy_xor(n: int, n_features: int = 12, noise: float = 0.4, seed: Optional[int] = 0, device=None) -> Tuple[Tensor, Tensor]
The Noisy XOR benchmark of the original Tsetlin machine paper: 12 Boolean features
of which only the first two matter (y = x0 XOR x1); a fraction noise of the
training labels are flipped.
Source code in src/torchtsetlin/datasets/synthetic.py
make_parity ¶
make_parity(n: int, n_bits: int = 4, n_features: int = 8, seed: Optional[int] = 0, device=None) -> Tuple[Tensor, Tensor]
Parity of the first n_bits features (harder than XOR).
Source code in src/torchtsetlin/datasets/synthetic.py
make_2d_noisy_xor ¶
make_2d_noisy_xor(n: int, size: int = 4, noise: float = 0.0, seed: Optional[int] = 0, device=None, max_tries: int = 200) -> Tuple[Tensor, Tensor]
2D Noisy XOR of the Convolutional TM paper: a size x size binary image whose
pixels are random, except for one 2 x 2 patch (at a random position) that carries
the class pattern.
Class 1 patterns are the diagonals [[1,0],[0,1]] / [[0,1],[1,0]], class 0
patterns are the horizontal bars [[1,1],[0,0]] / [[0,0],[1,1]]. Backgrounds are
resampled so that no other window contains any of the four patterns (otherwise the
label would be ambiguous). A fraction noise of the labels is flipped.
Returns images (n, 1, size, size) and labels (n,).
Source code in src/torchtsetlin/datasets/synthetic.py
make_shapes ¶
Toy image classification: a hollow 3x3 circle (class 0) or a 3x3 cross (class 1)
at a random position in a size x size image (Chapter 4 of the Tsetlin machine book).
Returns (n, 1, size, size) Boolean images and labels.
Source code in src/torchtsetlin/datasets/synthetic.py
make_3d_shapes ¶
make_3d_shapes(n: int, size: Tuple[int, int, int] = (8, 12, 12), seed: Optional[int] = 0, device=None) -> Tuple[Tensor, Tensor]
Toy volume classification that needs depth context: a 3x3x3 shape at a random
position in a (D, H, W) volume.
Class 0 (spindle) stacks the 2D slices plus, dot, plus; class 1 (3D cross) stacks
dot, plus, dot. Every single slice therefore looks the same in both classes (a plus
or a dot) — only their order along depth tells them apart, which a 3D window with
kd = 3 sees in one clause.
Returns (n, 1, D, H, W) Boolean volumes and labels.
Source code in src/torchtsetlin/datasets/synthetic.py
make_segmentation_shapes ¶
make_segmentation_shapes(n: int, size: int = 16, seed: Optional[int] = 0, device=None) -> Tuple[Tensor, Tensor]
Toy dense-prediction task: label every pixel background / disc / ring.
Two solid discs and two rings of radius 2-3 are stamped into a size x size Boolean
image. Disc interiors and ring interiors look identical on their own, so the classes are
only separable from a pixel's neighbourhood — which is exactly what a dense Tsetlin
machine gets to see, and what a per-pixel lookup cannot solve.
Returns (n, 1, size, size) Boolean images and (n, size, size) long label maps
with classes 0 = background, 1 = disc, 2 = ring.
Source code in src/torchtsetlin/datasets/synthetic.py
make_scenes ¶
make_scenes(n: int, size: Union[int, Tuple[int, int]] = 32, seed: Optional[int] = 0, device: Optional[Union[str, device]] = None) -> Tuple[Tensor, Tensor]
Synthetic CamVid-like street scenes for semantic segmentation.
Generates the properties that make CamVid hard rather than the photographs themselves (which are not redistributable): a wavy horizon, a large contiguous sky, buildings standing on the horizon, trees, and a road, with per-class texture (window grids, foliage noise, lane markings) and a global illumination jitter so that the classes are not separable by colour alone. Class frequencies are heavily imbalanced, as in CamVid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
number of scenes. |
required |
size
|
Union[int, Tuple[int, int]]
|
|
32
|
seed
|
Optional[int]
|
RNG seed ( |
0
|
device
|
Optional[Union[str, device]]
|
device for the returned tensors. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Tensor
|
long |
Tuple[Tensor, Tensor]
|
( |
Booleanize the images before handing them to a model, for example with
:class:~torchtsetlin.data.ColorThermometerEncoder, which keeps the ordering between
intensity levels that a plain threshold would throw away.
Source code in src/torchtsetlin/datasets/synthetic.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |