Sources¶
Sources of information indexed by object -- the instantiation of the method.
The method needs, for each object and each layer, one array per source. Any extractor that
returns a Sources works with Interpreter:
TorchSources-- activations, contrastive gradient and activated parameters of any PyTorch module (MLP, CNN, transformer);TreeSources-- path, slack and class distribution per depth of a scikit-learn tree;FunctionSources-- wrap your ownfn(X) -> {layer: {source: array}}.
Sources
dataclass
¶
Sources(
layers: dict[str, dict[str, ndarray]],
predictions: ndarray,
probabilities: ndarray | None = None,
logits: ndarray | None = None,
meta: dict = dict(),
)
Per-layer sources for n objects and the model's behaviour on them.
layers[layer][source] is an array whose first axis indexes the objects.
TorchSources
¶
TorchSources(
model: Module,
layers: Sequence[str | Module] | None = None,
*,
target: str | int = "fixed",
fixed_class: int = 0,
weight_modules: Mapping[str, str | Module] | None = None,
sources: Sequence[str] = SOURCE_NAMES,
grid: int | None = 4,
layout: str = "channels",
margin: str | Callable = "contrastive",
device=None,
batch_size: int = 256,
dtype=float32,
)
Extracts the three sources of the paper from any torch.nn.Module classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
A trained classifier returning logits |
required |
layers
|
Sequence[str | Module] | None
|
Module names (as in |
None
|
target
|
str | int
|
|
'fixed'
|
weight_modules
|
Mapping[str, str | Module] | None
|
Optional |
None
|
sources
|
Sequence[str]
|
Which sources to extract (subset of |
SOURCE_NAMES
|
grid
|
int | None
|
Positions kept per spatial/token axis ( |
4
|
margin
|
str | Callable
|
|
'contrastive'
|
Source code in src\fannar\sources\torch.py
device
instance-attribute
¶
weight_modules
instance-attribute
¶
weight_modules = {
k: named[v] if isinstance(v, str) else v for k, v in (weight_modules or {}).items()
}
positions
¶
Sources with the positions of one input as objects: {source: (P, C or F)}.
Used for energy maps over an image; max_side caps the grid (the Gram is P x P).
Source code in src\fannar\sources\torch.py
position_shape
¶
Spatial shape of the position grid returned by positions for layer.
Source code in src\fannar\sources\torch.py
TreeSources
¶
Per-depth sources of a fitted DecisionTreeClassifier (or any sklearn tree with tree_).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Fitted scikit-learn decision tree classifier. |
required | |
scale
|
Per-feature scale for the slack (e.g. the training standard deviation); defaults to 1. |
None
|
|
depths
|
Depths to use as layers; defaults to |
None
|