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#
Supported Riemann integration variants for gradient approximation. |
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.EnumSupported Riemann integration variants for gradient approximation.
- 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