Skip to content

Gradient

pnpxai.explainers.gradient

Gradient

Bases: ZennitExplainer

Gradient explainer.

Supported Modules: Linear, Convolution, LSTM, RNN, Attention

Parameters:

Name Type Description Default
model Module

The PyTorch model for which attribution is to be computed. layer (Optional[Union[Union[str, Module], Sequence[Union[str, Module]]]]): The target module to be explained. n_classes (int): The number of classes.

required
forward_arg_extractor Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]

A function that extracts forward arguments from the input batch(s) where the attribution scores are assigned.

None
additional_forward_arg_extractor Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]

A secondary function that extract additional forward arguments from the input batch(s).

None
**kwargs

Keyword arguments that are forwarded to the base implementation of the Explainer

required
Reference

Gabriel Erion, Joseph D. Janizek, Pascal Sturmfels, Scott Lundberg, Su-In Lee. Improving performance of deep learning models with axiomatic attribution priors and expected gradients.

SUPPORTED_MODULES = [Linear, Convolution, LSTM, RNN, Attention] class-attribute instance-attribute
layer = layer instance-attribute
attributor: Union[GradientAttributor, LayerGradientAttributor] property
EXPLANATION_TYPE: ExplanationType = 'attribution' class-attribute instance-attribute
TUNABLES = {} class-attribute instance-attribute
model = model.eval() instance-attribute
forward_arg_extractor = forward_arg_extractor instance-attribute
additional_forward_arg_extractor = additional_forward_arg_extractor instance-attribute
device property
n_classes = n_classes instance-attribute
__init__(model: Module, forward_arg_extractor: Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]] = None, additional_forward_arg_extractor: Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]] = None, layer: Optional[Union[Union[str, Module], Sequence[Union[str, Module]]]] = None, n_classes: Optional[int] = None) -> None
attribute(inputs: Union[Tensor, Tuple[Tensor]], targets: Tensor) -> Union[Tensor, Tuple[Tensor]]

Computes attributions for the given inputs and targets.

Parameters:

Name Type Description Default
inputs Tensor

The input data.

required
targets Tensor

The target labels for the inputs.

required

Returns:

Type Description
Union[Tensor, Tuple[Tensor]]

Union[torch.Tensor, Tuple[torch.Tensor]]: The result of the explanation.

__repr__()
copy()
set_kwargs(**kwargs)
get_tunables() -> Dict[str, Tuple[type, dict]]

Returns a dictionary of tunable parameters for the explainer.

Returns:

Type Description
Dict[str, Tuple[type, dict]]

Dict[str, Tuple[type, dict]]: Dictionary of tunable parameters.

__init_subclass__() -> None