torchtsetlin.train¶
torchtsetlin.train.trainer ¶
A PyTorch-style training loop for Tsetlin machines.
Trainer ¶
Trainer(model: TsetlinMachineBase, device=None, callbacks: Optional[Sequence[Callback]] = None, metrics: Optional[Dict[str, Callable]] = None, batch_size: int = 32, eval_batch_size: Optional[int] = None, verbose: bool = True)
Train, validate and test Tsetlin machines with the familiar fit / evaluate /
predict trio, PyTorch DataLoader support and callbacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
TsetlinMachineBase
|
any :class: |
required |
device
|
str | device | None
|
where to run (defaults to the model's device; the model is moved if given). |
None
|
callbacks
|
Optional[Sequence[Callback]]
|
list of :class: |
None
|
metrics
|
Optional[Dict[str, Callable]]
|
extra metrics |
None
|
batch_size
|
int
|
default batch size when |
32
|
eval_batch_size
|
Optional[int]
|
batch size for evaluation (defaults to |
None
|
verbose
|
bool
|
print one line per epoch. |
True
|
Example
trainer = Trainer(model, device="cuda", callbacks=[EarlyStopping(patience=5)]) history = trainer.fit((x_train, y_train), epochs=30, val_data=(x_val, y_val)) trainer.evaluate((x_test, y_test))
Source code in src/torchtsetlin/train/trainer.py
fit ¶
fit(train_data: DataLike, epochs: int = 10, val_data: Optional[DataLike] = None, batch_size: Optional[int] = None, shuffle: bool = True, eval_train: bool = False) -> History
Train for epochs epochs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_data
|
DataLike
|
|
required |
epochs
|
int
|
number of passes over the data. |
10
|
val_data
|
Optional[DataLike]
|
optional validation data evaluated after each epoch ( |
None
|
batch_size
|
Optional[int]
|
overrides the trainer default for tensor / |
None
|
shuffle
|
bool
|
shuffle tensor / |
True
|
eval_train
|
bool
|
also evaluate on the training data each epoch ( |
False
|
Source code in src/torchtsetlin/train/trainer.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 | |
predict ¶
Predictions (or vote sums with return_votes=True) for a dataset / tensors.
Source code in src/torchtsetlin/train/trainer.py
evaluate ¶
Compute metrics over data (model in eval() mode).
Source code in src/torchtsetlin/train/trainer.py
test ¶
Alias of :meth:evaluate with a test_ prefix.
evaluate ¶
evaluate(model: TsetlinMachineBase, data: DataLike, batch_size: int = 512, **kwargs) -> Dict[str, float]
Functional shortcut: Trainer(model, verbose=False).evaluate(data).
Source code in src/torchtsetlin/train/trainer.py
predict ¶
Functional shortcut for :meth:Trainer.predict.
torchtsetlin.train.callbacks ¶
Callbacks for :class:torchtsetlin.train.Trainer (Keras / Lightning flavoured).
Callback ¶
Base callback. Override any of the hooks; trainer and model are attached by
the trainer before training starts.
EarlyStopping ¶
EarlyStopping(monitor: str = 'val_accuracy', patience: int = 10, mode: str = 'max', min_delta: float = 0.0, restore_best: bool = True)
Bases: Callback
Stop training when a monitored metric stops improving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monitor
|
str
|
key in the epoch logs (e.g. |
'val_accuracy'
|
patience
|
int
|
epochs without improvement before stopping. |
10
|
mode
|
str
|
|
'max'
|
min_delta
|
float
|
minimum change to count as an improvement. |
0.0
|
restore_best
|
bool
|
reload the best |
True
|
Source code in src/torchtsetlin/train/callbacks.py
ModelCheckpoint ¶
ModelCheckpoint(path: str, monitor: str = 'val_accuracy', mode: str = 'max', save_best_only: bool = True, verbose: bool = False)
Bases: Callback
Save model.state_dict() to path whenever the monitored metric improves
(save_best_only=True) or after every epoch.
Source code in src/torchtsetlin/train/callbacks.py
CSVLogger ¶
LambdaCallback ¶
Bases: Callback
Build a callback from plain functions, e.g. LambdaCallback(on_epoch_end=lambda e, logs: ...).
Source code in src/torchtsetlin/train/callbacks.py
HyperparameterSchedule ¶
Bases: Callback
Change a model hyper-parameter ("s", "T", "drop_clause_p", ...) at the start
of each epoch: schedule(epoch) -> value. Handy for annealing the specificity.
Source code in src/torchtsetlin/train/callbacks.py
ProgressLogger ¶
torchtsetlin.train.history ¶
Training history container.
History ¶
Per-epoch metrics collected by :class:torchtsetlin.train.Trainer.
history.epochs is a list of dicts; history["val_accuracy"] returns the series of a
metric (None where missing); history.best("val_accuracy") the best value/epoch.