torchtsetlin.interpret¶
torchtsetlin.interpret ¶
Interpretability tools: rules, clause activity, explanations and closed-form feature importance (Blakely & Granmo, 2020).
Explanation
dataclass
¶
Why the model predicted what it predicted for one example.
PixelExplanation
dataclass
¶
rules ¶
Human readable rules of any model (delegates to model.rules).
clause_activity ¶
clause_activity(model: TsetlinMachineBase, x, y: Optional[Tensor] = None, n_classes: Optional[int] = None) -> Tensor
How often each clause matches.
Returns (C,) firing frequencies, or (K, C) per-class frequencies when labels
y are given (row k = fraction of class-k examples matched by each clause).
Source code in src/torchtsetlin/interpret.py
clause_precision ¶
For each clause of a multi-class model: how often it fires (support) and how often
the example belongs to the clause's own class when it fires (precision). Negative
polarity clauses are scored against not their class.
Source code in src/torchtsetlin/interpret.py
global_feature_importance ¶
global_feature_importance(model: TsetlinMachineBase, class_index: Optional[int] = None, normalize: bool = True) -> Tensor
Closed-form global literal importance.
For output k, the strength of literal l is the (weighted) fraction of positive
clauses voting for k that include l:
g_k[l] = (1/m) * sum_{j: W[j,k] > 0} W[j,k] * include[j, l].
Returns (2F,) for one class or (K, 2F) for all classes. The first F entries
correspond to the positive literals x_l and the last F to NOT x_l.
Source code in src/torchtsetlin/interpret.py
local_feature_importance ¶
local_feature_importance(model: TsetlinMachineBase, x, class_index: Optional[Tensor] = None, normalize: bool = True) -> Tensor
Closed-form local literal importance for each example.
l_k[l, X] = sum_{j: W[j,k] > 0, C_j(X) = 1} W[j,k] * include[j, l] * literal_l(X) —
the weight of the matching positive clauses of class k that rely on literal l.
By default k is the predicted class. Returns (B, 2F).
Source code in src/torchtsetlin/interpret.py
aggregate_literal_importance ¶
aggregate_literal_importance(importance: Tensor, groups: Sequence[int], n_groups: Optional[int] = None, combine_negated: bool = True) -> Tensor
Sum literal importances over groups of Boolean features (e.g. the thermometer bits of
one original feature). groups[i] is the group id of Boolean feature i.
importance has 2F entries (or (..., 2F)); with combine_negated the
NOT literals are added to the same group, otherwise 2 * n_groups values are
returned (positive groups first).
Source code in src/torchtsetlin/interpret.py
explain ¶
explain(model: TsetlinMachineBase, x, feature_names: Optional[Sequence[str]] = None, output: Optional[int] = None, max_clauses: int = 20) -> Explanation
List the clauses that matched a single example and how they voted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
TsetlinMachineBase
|
a classifier (vanilla or coalesced). |
required |
x
|
Tensor | ndarray
|
one example ( |
required |
feature_names
|
Optional[Sequence[str]]
|
names used to render clause expressions. |
None
|
output
|
Optional[int]
|
restrict to clauses voting on this output (default: the predicted class). |
None
|
max_clauses
|
int
|
keep the |
20
|
Source code in src/torchtsetlin/interpret.py
explain_pixel ¶
explain_pixel(model: TsetlinMachineBase, x: Tensor, row: int, col: int, target: Optional[int] = None, output: Optional[int] = None, max_clauses: int = 20, absolute_names: bool = True) -> PixelExplanation
List the clauses that decided one pixel's label, as statements about named pixels.
This is the thing a convolutional network cannot do. Each matching clause decodes into a
conjunction over absolute image coordinates — p[1,14,7] AND NOT p[0,13,8] reads
"plane 1 of pixel (14, 7) is set and plane 0 of pixel (13, 8) is not" — so a prediction
can be audited pixel by pixel rather than attributed by a saliency heuristic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
TsetlinMachineBase
|
a :class: |
required |
x
|
Tensor
|
one image |
required |
row
|
int
|
output-grid row of the pixel to explain. |
required |
col
|
int
|
output-grid column. |
required |
target
|
Optional[int]
|
optional true label, shown in the summary when it differs from the prediction. |
None
|
output
|
Optional[int]
|
restrict to clauses voting on this class (default: the predicted one). |
None
|
max_clauses
|
int
|
keep the strongest |
20
|
absolute_names
|
bool
|
name literals by absolute pixel coordinates (via
|
True
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
PixelExplanation
|
class: |
Example
print(tt.interpret.explain_pixel(model, x[0], row=14, col=7))