torchtsetlin.metrics¶
torchtsetlin.metrics ¶
Lightweight, device-agnostic metrics for Tsetlin machine outputs.
Vote sums behave like logits, so torchmetrics / scikit-learn work too; these helpers
avoid extra dependencies and stay on the GPU.
accuracy ¶
confusion_matrix ¶
confusion_matrix(votes_or_pred: Tensor, y: Tensor, n_classes: Optional[int] = None, ignore_index: Optional[int] = None) -> Tensor
(K, K) confusion matrix with rows = true class, columns = predicted class.
ignore_index drops every entry whose true label equals it (the "void" class of
segmentation datasets), so it is counted neither as a hit nor as a miss.
Source code in src/torchtsetlin/metrics.py
precision_recall_f1 ¶
Per-class precision / recall / F1 from a confusion matrix.
Source code in src/torchtsetlin/metrics.py
classification_report ¶
classification_report(votes_or_pred: Tensor, y: Tensor, class_names: Optional[Sequence[str]] = None, digits: int = 3) -> str
Text report similar to scikit-learn's classification_report.
Source code in src/torchtsetlin/metrics.py
regression_metrics ¶
MAE, RMSE and R^2.
Source code in src/torchtsetlin/metrics.py
multilabel_metrics ¶
Subset accuracy, Hamming accuracy and micro-F1 for Boolean (B, K) predictions.
Source code in src/torchtsetlin/metrics.py
vote_margin ¶
Difference between the best and second best vote sum per example ((B,)).
expected_calibration_error ¶
ECE of (B, K) probabilities.
Source code in src/torchtsetlin/metrics.py
trustworthiness_curve ¶
Accuracy of the predictions whose confidence is at least each level (Chapter 7).
Confidence is the winning vote sum. Returns {"confidence": (L,), "accuracy": (L,),
"coverage": (L,)}; a trustworthy model has monotonically increasing accuracy.
Source code in src/torchtsetlin/metrics.py
segmentation_confusion_matrix ¶
segmentation_confusion_matrix(votes_or_pred: Tensor, target: Tensor, n_classes: Optional[int] = None, ignore_index: Optional[int] = None) -> Tensor
(K, K) pixel confusion matrix for dense predictions (see :func:confusion_matrix).
Source code in src/torchtsetlin/metrics.py
pixel_accuracy ¶
Fraction of pixels predicted correctly.
Source code in src/torchtsetlin/metrics.py
iou_from_confusion ¶
Per-class intersection-over-union from a confusion matrix; NaN for absent classes.
A class is absent when it appears neither in the ground truth nor in the prediction, in which case its IoU is undefined rather than 0 — averaging a 0 there would punish a model for a class the image does not contain.
Source code in src/torchtsetlin/metrics.py
iou ¶
iou(votes_or_pred: Tensor, target: Tensor, n_classes: Optional[int] = None, ignore_index: Optional[int] = None) -> Tensor
Per-class IoU (K,), NaN where the class is absent from both sides.
Source code in src/torchtsetlin/metrics.py
mean_iou ¶
mean_iou(votes_or_pred: Tensor, target: Tensor, n_classes: Optional[int] = None, ignore_index: Optional[int] = None) -> float
Mean IoU over the classes that are present (the usual mIoU).
Source code in src/torchtsetlin/metrics.py
dice_from_confusion ¶
Per-class Dice / F1 coefficient from a confusion matrix (2|A∩B| / (|A|+|B|)).
Source code in src/torchtsetlin/metrics.py
dice ¶
dice(votes_or_pred: Tensor, target: Tensor, n_classes: Optional[int] = None, ignore_index: Optional[int] = None) -> Tensor
Per-class Dice coefficient (K,) — the medical-imaging convention for overlap.
Source code in src/torchtsetlin/metrics.py
segmentation_metrics ¶
segmentation_metrics(votes_or_pred: Tensor, target: Tensor, n_classes: Optional[int] = None, class_names: Optional[Sequence[str]] = None, ignore_index: Optional[int] = None, per_class: bool = True) -> Dict[str, float]
Pixel accuracy, mean IoU, mean Dice and (optionally) per-class IoU in one dict.
Source code in src/torchtsetlin/metrics.py
segmentation_report ¶
segmentation_report(votes_or_pred: Tensor, target: Tensor, class_names: Optional[Sequence[str]] = None, n_classes: Optional[int] = None, ignore_index: Optional[int] = None, digits: int = 3) -> str
Per-class IoU / Dice / precision / recall table, plus pixel accuracy and mIoU.
Source code in src/torchtsetlin/metrics.py
boundary_f1 ¶
boundary_f1(votes_or_pred: Tensor, target: Tensor, tolerance: int = 2, ignore_index: Optional[int] = None) -> float
Boundary F1 (BF) score: how well the predicted class outlines match the true ones.
A predicted boundary pixel counts as a hit when a true boundary pixel lies within
tolerance pixels (Chebyshev distance), and vice versa; the score is the harmonic mean
of the two rates. Unlike IoU this is sensitive to thin structures and to how crisp the
edges are, which is what a per-pixel model with no smoothing tends to get wrong.
Both arguments must be dense: vote maps (B, K, H, W) or label maps (B, H, W).