tdhook.attribution.gradient_helpers.helpers#
Helpers for gradient attribution.
This code is adapted from the Captum library (BSD 3-Clause) Original source: pytorch/captum
Attributes#
Classes#
Create a collection of name/value pairs. |
Functions#
|
Retrieves parameters for the input approximation method |
|
Step sizes are identical and alphas are scaled in [0, 1] |
Numpy's np.polynomial.legendre function helps to compute step sizes |
Module Contents#
- class tdhook.attribution.gradient_helpers.helpers.Riemann(*args, **kwds)[source]#
Bases:
enum.EnumCreate a collection of name/value pairs.
Example enumeration:
>>> class Color(Enum): ... RED = 1 ... BLUE = 2 ... GREEN = 3
Access them by:
attribute access:
>>> Color.RED <Color.RED: 1>
value lookup:
>>> Color(1) <Color.RED: 1>
name lookup:
>>> Color['RED'] <Color.RED: 1>
Enumerations can be iterated over, and know how many members they have:
>>> len(Color) 3
>>> list(Color) [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]
Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.
- tdhook.attribution.gradient_helpers.helpers.SUPPORTED_RIEMANN_METHODS = ['riemann_left', 'riemann_right', 'riemann_middle', 'riemann_trapezoid'][source]#
- tdhook.attribution.gradient_helpers.helpers.SUPPORTED_METHODS: List[str] = ['riemann_left', 'riemann_right', 'riemann_middle', 'riemann_trapezoid', 'gausslegendre'][source]#
- tdhook.attribution.gradient_helpers.helpers.approximation_parameters(method)[source]#
Retrieves parameters for the input approximation method
- Parameters:
method (str) – The name of the approximation method. Currently only riemann and gauss legendre are
- Return type:
Tuple[Callable[[int], List[float]], Callable[[int], List[float]]]
- tdhook.attribution.gradient_helpers.helpers.riemann_builders(method=Riemann.trapezoid)[source]#
Step sizes are identical and alphas are scaled in [0, 1]
- Parameters:
method (Riemann) – left, right, middle and trapezoid riemann
- Returns:
- step_sizes (Callable):
step_sizes takes the number of steps as an input argument and returns an array of steps sizes which sum is smaller than or equal to one.
- alphas (Callable):
alphas takes the number of steps as an input argument and returns the multipliers/coefficients for the inputs of integrand in the range of [0, 1]
- Return type:
2-element tuple of step_sizes, alphas
- tdhook.attribution.gradient_helpers.helpers.gauss_legendre_builders()[source]#
Numpy’s np.polynomial.legendre function helps to compute step sizes and alpha coefficients using gauss-legendre quadrature rule. Since numpy returns the integration parameters in different scales we need to rescale them to adjust to the desired scale.
Gauss Legendre quadrature rule for approximating the integrals was originally proposed by [Xue Feng and her intern Hauroun Habeeb] (https://research.fb.com/people/feng-xue/).
- Returns:
- step_sizes (Callable):
step_sizes takes the number of steps as an input argument and returns an array of steps sizes which sum is smaller than or equal to one.
- alphas (Callable):
alphas takes the number of steps as an input argument and returns the multipliers/coefficients for the inputs of integrand in the range of [0, 1]
- Return type:
2-element tuple of step_sizes, alphas