Skip to content

Slices

trulens.nn.slices

The slice, or layer, of the network provides flexibility over the level of abstraction for the explanation. In a low layer, an explanation may highlight the edges that were most important in identifying an object like a face, while in a higher layer, the explanation might highlight high-level features such as a nose or mouth. By raising the level of abstraction, explanations that generalize over larger sets of samples are possible.

Formally, A network, $f$, can be broken into a slice, $f = g \circ h$, where $h$ can be thought of as a pre-processor that computes features, and $g$ can be thought of as a sub-model that uses the features computed by $h$.

Classes

Cut

Bases: object

A cut is the primary building block for a slice. It determines an internal component of a network to expose. A slice if formed by two cuts.

Functions
__init__
__init__(name: LayerIdentifier, anchor: str = 'out', accessor: Optional[Callable] = None)
PARAMETER DESCRIPTION
name

The name or index of a layer in the model, or a list containing the names/indices of mutliple layers.

TYPE: LayerIdentifier

anchor

Determines whether input ('in') or the output ('out') tensor of the spcified layer should be used.

TYPE: str DEFAULT: 'out'

accessor

An accessor function that operates on the layer, mapping the tensor (or list thereof) corresponding to the layer's input/output to another tensor (or list thereof). This can be used to, e.g., extract a particular output from a layer that produces a sequence of outputs. If accessor is None, the following accessor function will be used:

lambda t: t[-1] if isinstance(t, list) else t

TYPE: Optional[Callable] DEFAULT: None

access_layer
access_layer(layer: TensorLike) -> TensorLike

Applies self.accessor to the result of collecting the relevant tensor(s) associated with a layer's output.

PARAMETER DESCRIPTION
layer

The tensor output (or input, if so specified by the anchor) of the layer(s) specified by this cut.

TYPE: TensorLike

RETURNS DESCRIPTION
TensorLike

The result of applying self.accessor to the given layer.

InputCut

Bases: Cut

Special cut that selects the input(s) of a model.

Functions
__init__
__init__(anchor: str = 'in', accessor: Optional[Callable] = None)
PARAMETER DESCRIPTION
anchor

Determines whether input ('in') or the output ('out') tensor of the spcified layer should be used.

TYPE: str DEFAULT: 'in'

accessor

An accessor function that operates on the layer, mapping the tensor (or list thereof) corresponding to the layer's input/output to another tensor (or list thereof). This can be used to, e.g., extract a particular output from a layer that produces a sequence of outputs. If accessor is None, the following accessor function will be used:

lambda t: t[-1] if isinstance(t, list) else t

TYPE: Optional[Callable] DEFAULT: None

OutputCut

Bases: Cut

Special cut that selects the output(s) of a model.

Functions
__init__
__init__(anchor: str = 'out', accessor: Optional[Callable] = None)
PARAMETER DESCRIPTION
anchor

Determines whether input ('in') or the output ('out') tensor of the spcified layer should be used.

TYPE: str DEFAULT: 'out'

accessor

An accessor function that operates on the layer, mapping the tensor (or list thereof) corresponding to the layer's input/output to another tensor (or list thereof). This can be used to, e.g., extract a particular output from a layer that produces a sequence of outputs. If accessor is None, the following accessor function will be used:

lambda t: t[-1] if isinstance(t, list) else t

TYPE: Optional[Callable] DEFAULT: None

LogitCut

Bases: Cut

Special cut that selects the logit layer of a model. The logit layer must be named 'logits' or otherwise specified by the user to the model wrapper.

Functions
__init__
__init__(anchor: str = 'out', accessor: Optional[Callable] = None)
PARAMETER DESCRIPTION
anchor

Determines whether input ('in') or the output ('out') tensor of the spcified layer should be used.

TYPE: str DEFAULT: 'out'

accessor

An accessor function that operates on the layer, mapping the tensor (or list thereof) corresponding to the layer's input/output to another tensor (or list thereof). This can be used to, e.g., extract a particular output from a layer that produces a sequence of outputs. If accessor is None, the following accessor function will be used:

lambda t: t[-1] if isinstance(t, list) else t

TYPE: Optional[Callable] DEFAULT: None

Slice

Bases: object

Class representing a slice of a network. A network, $f$, can be broken into a slice, $f = g \circ h$, where $h$ can be thought of as a pre-processor that computes features, and $g$ can be thought of as a sub-model that uses the features computed by $h$.

A Slice object represents a slice as two Cuts, from_cut and to_cut, which are the layers corresponding to the output of $h$ and $g$, respectively.

Attributes
from_cut property
from_cut: Cut

Cut representing the output of the preprocessing function, $h$, in slice, $f = g \circ h$.

to_cut property
to_cut: Cut

Cut representing the output of the sub-model, $g$, in slice, $f = g \circ h$.

Functions
__init__
__init__(from_cut: Cut, to_cut: Cut)
PARAMETER DESCRIPTION
from_cut

Cut representing the output of the preprocessing function, $h$, in slice, $f = g \circ h$.

TYPE: Cut

to_cut

Cut representing the output of the sub-model, $g$, in slice, $f = g \circ h$.

TYPE: Cut

full_network staticmethod
full_network()
Returns

Slice A slice representing the entire model, i.e., :math:f = g \circ h, where :math:h is the identity function and :math:g = f.

Functions