Domain modules¶
One entry point per kind of data, each a subclass of Interpreter that only
sets defaults and handles the inputs. The modules load on first access (fa.tabular, fa.image,
fa.text, fa.tree), so import fannar stays light.
fannar.tabular¶
TabularInterpreter
¶
TabularInterpreter(
model,
X,
y=None,
*,
layers: Sequence[str] | None = None,
feature_names: Sequence[str] | None = None,
class_names: Sequence[str] | None = None,
**kwargs,
)
Bases: Interpreter
Interpreter for a PyTorch model over tabular rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
A trained PyTorch classifier returning logits |
required | |
X
|
Rows to interpret: a |
required | |
y
|
True labels: integers |
None
|
|
layers
|
Sequence[str] | None
|
Module names to probe, as in |
None
|
**kwargs
|
Everything else is passed to |
{}
|
Source code in src\fannar\tabular.py
as_table
¶
as_table(
X,
y=None,
feature_names: Sequence[str] | None = None,
class_names: Sequence[str] | None = None,
classes: Sequence | None = None,
dtype=float32,
)
(X, y, feature_names, class_names) from arrays or pandas objects.
Explicit feature_names/class_names take precedence over what the DataFrame and the labels
provide. X becomes a dtype array (float32 by default, the usual dtype of a model).
Source code in src\fannar\tabular.py
encode_labels
¶
y as class indices 0 .. K-1 plus the class names, when y is not already numeric.
A pandas categorical keeps its category order. Strings, objects and booleans are matched against
classes when given (e.g. tree.classes_), otherwise against their sorted unique values, the
same order as scikit-learn's LabelEncoder. Numeric labels are returned unchanged with no names.
Source code in src\fannar\tabular.py
fannar.image¶
ImageInterpreter
¶
ImageInterpreter(
model,
X,
y=None,
*,
layers: Sequence[str] | None = None,
grid: int | None = 4,
class_names: Sequence[str] | None = None,
**kwargs,
)
Bases: Interpreter
Interpreter for PyTorch image classifiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
A trained PyTorch classifier returning logits |
required | |
X
|
Images |
required | |
y
|
True labels (integers |
None
|
|
layers
|
Sequence[str] | None
|
Module names to probe, as in |
None
|
grid
|
int | None
|
Positions kept per spatial axis when pooling each probed output ( |
4
|
class_names
|
Sequence[str] | None
|
Names of the classes, used by the plots and the error explanations. |
None
|
**kwargs
|
Passed to |
{}
|
Source code in src\fannar\image.py
fannar.text¶
TextInterpreter
¶
TextInterpreter(
model,
X,
y=None,
*,
layers: Sequence[str] | None = None,
grid: int | None = 8,
**kwargs,
)
Bases: Interpreter
Interpreter for PyTorch models over token sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
A trained PyTorch classifier taking one tensor (e.g. token ids |
required | |
X
|
The sequences: an array or tensor of token ids (or embeddings), or a |
required | |
y
|
True labels (integers |
None
|
|
layers
|
Sequence[str] | None
|
Module names to probe; |
None
|
grid
|
int | None
|
Positions kept along the sequence when pooling each hidden state ( |
8
|
**kwargs
|
Passed to |
{}
|
Source code in src\fannar\text.py
wrap_hf
¶
Wraps a Hugging Face sequence classifier (or anything called as model(input_ids=...))
into an HFClassifier whose forward(input_ids) returns the logits.
Extra keyword arguments are passed to every call of the wrapped model.
Source code in src\fannar\text.py
HFClassifier
¶
Bases: Module
A classifier taking one tensor of token ids and returning its logits (N, K).
The wrapped model is stored as self.model, so the probed layer names gain the prefix
model. (model.bert.encoder.layer.0...). With pad_token_id the attention mask is
input_ids != pad_token_id; otherwise every token is attended to.
Source code in src\fannar\text.py
block_names
¶
Names of the children of the longest nn.ModuleList in model: the transformer blocks.
Raises ValueError when the model has no nn.ModuleList; pass layers= explicitly then.
Source code in src\fannar\text.py
fannar.tree¶
TreeInterpreter
¶
TreeInterpreter(
tree,
X,
y=None,
*,
X_train=None,
scale=None,
depths: Sequence[int] | None = None,
feature_names: Sequence[str] | None = None,
class_names: Sequence[str] | None = None,
**kwargs,
)
Bases: Interpreter
Interpreter for a fitted scikit-learn decision tree classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
A fitted |
required | |
X
|
Rows to interpret: a |
required | |
y
|
True labels, with the same values the tree was trained on ( |
None
|
|
X_train
|
Optional training rows. Their per-feature standard deviation becomes the scale of the
|
None
|
|
scale
|
An explicit per-feature scale for the slack (overrides |
None
|
|
depths
|
Sequence[int] | None
|
Depths used as layers (default |
None
|
**kwargs
|
Passed to |
{}
|