methods

Submodules

Attributes

model

Functions

_validate_input_gen_arguments(→ None)

Validates the input arguments for the input generator function.

default_attribute_input_generator(→ Dict[str, Any])

Returns the default input generator.

_validate_wrapper_inputs(→ None)

Validates the input wrappers.

wrap_method(method[, input_generator_fns, ...])

Creates Dynamic Subclass to standardize class methods across difference method classes.

wrap_method_llm(method[, input_generator_fns, ...])

Creates Dynamic Subclass to standardize class methods across difference method classes.

Package Contents

methods._validate_input_gen_arguments(feature_inputs: torch.Tensor, y_labels: torch.Tensor | None, target: torch.Tensor | None, context: Dict | None, model: Any) None

Validates the input arguments for the input generator function.

Parameters:
  • feature_inputs (Any) – Input features.

  • y_labels (Any) – Ground truth labels.

  • target (Any) – Target labels.

  • context (Any) – Context.

  • model (Any) – Model instance.

Raises:

AssertionError – If input arguments are not of the expected types.

methods.default_attribute_input_generator(feature_inputs: torch.Tensor, y_labels: torch.Tensor | None, target: torch.Tensor | None, context: Dict | None, model: Any) Dict[str, Any]

Returns the default input generator.

Input generator collates information from model, model output, dataset and others into single dictionary that will be unpack and used as arguments for the attribute class method.

The default keys naming schema is that of captum’s attribute; create a custom input generator if you are using functions from other libraries.

Some arguments passed in (see pipeline classes) are not used by the default function (e.g. y_labels). These arguments are there as we anticipate that users who create and pass in their own input generator function may require these arguments.

Parameters:
  • feature_inputs (torch.Tensor) – Input tensor.

  • y_labels (torch.Tensor) – True y label tensor.

  • target (torch.Tensor) – Target arguments pass on to Captum attribute function.

  • context (dict) – Dictionary containing other relevant data (e.g. ground truth attribution).

  • model (torch.nn.Module) – The model that FA method is evaluated on.

Returns:

Dictionary with all argument required for captum attribute function.

Return type:

dict

methods._validate_wrapper_inputs(method: Any, input_generator_fns: Callable, out_processing: Callable) None

Validates the input wrappers.

Parameters:
  • method (type) – Explanation Method to be extended.

  • input_generator_fns (Callable) – Callable to generate input arguments for method.attribute.

  • out_processing (Callable) – Callable to post-process method.attribute output.

methods.wrap_method(method: Any, input_generator_fns: Callable = default_attribute_input_generator, out_processing: Callable | None = None, other_inputs: Dict = {}, class_params: Dict = {}, pre_fix: str = 'wrapper_', name: str | None = None)

Creates Dynamic Subclass to standardize class methods across difference method classes.

Parameters:
  • method (class) – Explanation Method to be extended.

  • input_generator_fns (Callable) – Callable to generate input arguments for method.attribute. Defaults to default_attribute_input_generator.

  • out_processing (Callable, optional) – Callable to post-process method.attribute output. Defaults to None.

  • other_inputs (dict) – Other args to be pass on to method.attribute. Defaults to {}.

  • class_params (dict) – Other args to be used to initialize method class. Defaults to {}.

  • pre_fix (str) – prefix to be added to subclass. Defaults to ‘wrapper_’

  • name (str) – The name of the method. If None uses the name of the method class. Defaults to None.

Returns:

A subclass of method with standardised interface.

Return type:

type

methods.wrap_method_llm(method: Any, input_generator_fns: Callable = default_attribute_input_generator, out_processing: Callable | None = None, other_inputs: Dict = {}, class_params: Dict = {}, gen_args: Dict = {'max_new_tokens': 1}, tokenizer: Any | None = None, pre_fix: str = 'wrapper_')

Creates Dynamic Subclass to standardize class methods across difference method classes.

Parameters:
  • method (class) – Explanation Method to be extended.

  • input_generator_fns (Callable) – Callable to generate input arguments for method.attribute. Defaults to default_attribute_input_generator.

  • out_processing (Callable, optional) – Callable to post-process method.attribute output. Defaults to None.

  • other_inputs (dict) – Other args to be pass on to method.attribute. Defaults to {}.

  • class_params (dict) – Other args to be used to initialize method class. Defaults to {}.

  • pre_fix (str) – prefix to be added to subclass. Defaults to ‘wrapper_’.

Returns:

A subclass of method with standardized interface.

Return type:

type

methods.model