zennit.rules
Rules based on Hooks
Functions
Add 'bias' to zero_params, where zero_params is a string or a list of strings. |
Classes
LRP AlphaBeta rule [Bach et al., 2015]. |
|
ParamMod to clamp module parameters. |
|
LRP Epsilon rule [Bach et al., 2015]. |
|
LRP Flat rule [Lapuschkin et al., 2019]. |
|
Generalized LRP Gamma rule [Andéol et al., 2021, Montavon et al., 2019]. |
|
ParamMod to modify module parameters as in the Gamma rule. |
|
ParamMod that does not modify the parameters. |
|
Normalize and weight by input contribution. |
|
Unmodified pass-through rule. |
|
Modify ReLU gradient to smooth softplus gradient [Dombrowski et al., 2019]. |
|
DeconvNet ReLU rule [Zeiler and Fergus, 2014]. |
|
GuidedBackprop ReLU rule [Springenberg et al., 2015]. |
|
LRP WSquare rule [Montavon et al., 2017]. |
|
LRP ZBox rule [Montavon et al., 2017]. |
|
LRP ZPlus rule [Bach et al., 2015, Montavon et al., 2017]. |
- zennit.rules.zero_bias(zero_params=None)[source]
Add ‘bias’ to zero_params, where zero_params is a string or a list of strings.
- Parameters:
zero_params (str or list of str, optional) – Name or names, to which
'bias'should be added.- Returns:
Supplied
zero_params, with the string'bias'appended.- Return type:
list of str
- class zennit.rules.ClampMod[source]
Bases:
ParamModParamMod to clamp module parameters.
- Parameters:
min (float or None, optional) – Minimum float value for which the parameters should be clamped, or None if no clamping should be done.
max (float or None, optional) – Maximum float value for which the parameters should be clamped, or None if no clamping should be done.
**kwargs (dict[str, object]) – Additional keyword arguments used for
ParamMod.
- class zennit.rules.GammaMod[source]
Bases:
ParamModParamMod to modify module parameters as in the Gamma rule. Adds the scaled, clamped parameters to the parameter itself.
- Parameters:
gamma (float, optional) – Gamma scaling parameter, by which the clamped parameters are multiplied.
min (float or None, optional) – Minimum float value for which the parameters should be clamped, or None if no clamping should be done.
max (float or None, optional) – Maximum float value for which the parameters should be clamped, or None if no clamping should be done.
**kwargs (dict[str, object]) – Additional keyword arguments used for
ParamMod.
- class zennit.rules.NoMod[source]
Bases:
ParamModParamMod that does not modify the parameters. Allows other modification flags.
- Parameters:
**kwargs (dict[str, object]) – Additional keyword arguments used for
ParamMod.
- class zennit.rules.Epsilon[source]
Bases:
BasicHookLRP Epsilon rule [Bach et al., 2015]. Setting
(epsilon=0)produces the LRP-0 rule [Bach et al., 2015]. LRP Epsilon is most commonly used in middle layers, LRP-0 is most commonly used in upper layers [Montavon et al., 2019]. Sometimes higher values ofepsilonare used, therefore it is not always only a stabilizer value.- Parameters:
epsilon (callable or float, optional) – Stabilization parameter. If
epsilonis a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator. Note that this is calledstabilizerfor all other rules.zero_params (list[str], optional) – A list of parameter names that shall set to zero. If None (default), no parameters are set to zero.
- class zennit.rules.Gamma[source]
Bases:
BasicHookGeneralized LRP Gamma rule [Andéol et al., 2021, Montavon et al., 2019]. The gamma parameter scales the added positive/negative parts of the weights. The original Gamma rule [Montavon et al., 2019] may only be used with positive inputs. The generalized version is equivalent to the original Gamma when there are only positive inputs, but may also be used for negative inputs.
- Parameters:
gamma (float, optional) – Multiplier for added positive weights.
stabilizer (callable or float, optional) – Stabilization parameter. If
stabilizeris a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator.zero_params (list[str], optional) – A list of parameter names that shall set to zero. If None (default), no parameters are set to zero.
- class zennit.rules.ZPlus[source]
Bases:
BasicHookLRP ZPlus rule [Bach et al., 2015, Montavon et al., 2017]. It is the same as using
AlphaBetawith(alpha=1, beta=0)- Parameters:
stabilizer (callable or float, optional) – Stabilization parameter. If
stabilizeris a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator.zero_params (list[str], optional) – A list of parameter names that shall set to zero. If None (default), no parameters are set to zero.
Notes
Note that the original deep Taylor Decomposition (DTD) specification of the ZPlus Rule [Montavon et al., 2017] only considers positive inputs, as they are used in ReLU Networks. This implementation is effectively alpha=1, beta=0, where negative inputs are allowed.
- class zennit.rules.AlphaBeta[source]
Bases:
BasicHookLRP AlphaBeta rule [Bach et al., 2015]. The AlphaBeta rule weights positive (alpha) and negative (beta) contributions. Most common parameters are
(alpha=1, beta=0)and(alpha=2, beta=1). It is most commonly used for lower layers [Montavon et al., 2019].- Parameters:
alpha (float, optional) – Multiplier for the positive output term.
beta (float, optional) – Multiplier for the negative output term.
stabilizer (callable or float, optional) – Stabilization parameter. If
stabilizeris a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator.zero_params (list[str], optional) – A list of parameter names that shall set to zero. If None (default), no parameters are set to zero.
- Raises:
ValueError – If alpha and beta do not sum to one, or are negative.
- class zennit.rules.ZBox[source]
Bases:
BasicHookLRP ZBox rule [Montavon et al., 2017]. The ZBox rule is intended for “boxed” input pixel space. Generally, the lowest and highest possible values are used, i.e.
(low=0., high=1.)for raw image data in the float data type. Neural network inputs are often normalized to match an isotropic gaussian distribution with mean 0 and variance 1, which means that the lowest and highest values also need to be adapted. For image data, this generally happens per channel, for which caselowandhighcan be passed as tensors with shape(1, 3, 1, 1), which will be broadcasted as expected.- Parameters:
low (
torch.Tensoror float) – Lowest pixel values of input. Subject to broadcasting.high (
torch.Tensoror float) – Highest pixel values of input. Subject to broadcasting.stabilizer (callable or float, optional) – Stabilization parameter. If
stabilizeris a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator.zero_params (list[str], optional) – A list of parameter names that shall set to zero. If None (default), no parameters are set to zero.
- class zennit.rules.Pass[source]
Bases:
HookUnmodified pass-through rule. If the rule of a layer shall not be any other, is elementwise and shall not be the gradient, the Pass rule simply passes upper layer relevance through to the lower layer.
- backward(module, grad_input, grad_output)[source]
Pass through the upper gradient, skipping the one for this layer.
- Parameters:
module (
torch.nn.Module) – The module to which this hook is attached.grad_input (
torch.Tensor) – The input gradient tensor.grad_output (
torch.Tensor) – The output gradient tensor.
- Returns:
The unmodified grad_output.
- Return type:
torch.Tensor
- class zennit.rules.Norm[source]
Bases:
BasicHookNormalize and weight by input contribution. This is essentially the same as the LRP
Epsilonrule [Bach et al., 2015] with a fixed epsilon only used as a stabilizer, and without the need of the attached layer to have parametersweightandbias.- Parameters:
stabilizer (callable or float, optional) – Stabilization parameter. If
stabilizeris a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator.
- class zennit.rules.WSquare[source]
Bases:
BasicHookLRP WSquare rule [Montavon et al., 2017]. It is most commonly used in the first layer when the values are not bounded [Montavon et al., 2019].
- Parameters:
stabilizer (callable or float, optional) – Stabilization parameter. If
stabilizeris a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator.zero_params (list[str], optional) – A list of parameter names that shall set to zero. If None (default), no parameters are set to zero.
- class zennit.rules.Flat[source]
Bases:
BasicHookLRP Flat rule [Lapuschkin et al., 2019]. It is essentially the same as the LRP
WSquarerule, but with all parameters set to ones.- Parameters:
stabilizer (callable or float, optional) – Stabilization parameter. If
stabilizeris a float, it will be added to the denominator with the same sign as each respective entry. If it is callable, a function(input: torch.Tensor) -> torch.Tensoris expected, of which the output corresponds to the stabilized denominator.zero_params (list[str], optional) – A list of parameter names that shall set to zero. If None (default), no parameters are set to zero.
- class zennit.rules.ReLUDeconvNet[source]
Bases:
HookDeconvNet ReLU rule [Zeiler and Fergus, 2014].
- backward(module, grad_input, grad_output)[source]
Modify ReLU gradient according to DeconvNet [Zeiler and Fergus, 2014].
- Parameters:
module (
torch.nn.Module) – The module to which this hook is attached.grad_input (
torch.Tensor) – The input gradient tensor.grad_output (
torch.Tensor) – The output gradient tensor.
- Returns:
The modified grad_output.
- Return type:
torch.Tensor
- class zennit.rules.ReLUGuidedBackprop[source]
Bases:
HookGuidedBackprop ReLU rule [Springenberg et al., 2015].
- backward(module, grad_input, grad_output)[source]
Modify ReLU gradient according to GuidedBackprop [Springenberg et al., 2015].
- Parameters:
module (
torch.nn.Module) – The module to which this hook is attached.grad_input (
torch.Tensor) – The input gradient tensor.grad_output (
torch.Tensor) – The output gradient tensor.
- Returns:
The modified grad_output.
- Return type:
torch.Tensor
- class zennit.rules.ReLUBetaSmooth[source]
Bases:
HookModify ReLU gradient to smooth softplus gradient [Dombrowski et al., 2019]. Used to obtain meaningful surrogate gradients to compute higher order gradients with ReLUs. Equivalent to changing the gradient to be the (scaled) logistic function (sigmoid).
- Parameters:
beta_smooth (float, optional) – The beta parameter for the softplus gradient (i.e.
sigmoid(beta * input)). Defaults to10.
- copy()[source]
Return a copy of this hook with the same beta parameter.
- Returns:
A copy of this hook.
- Return type:
- forward(module, input, output)[source]
Remember the input for the backward pass.
- Parameters:
module (
torch.nn.Module) – The module to which this hook is attached.input (
torch.Tensor) – The input tensor.output (
torch.Tensor) – The output tensor.
- backward(module, grad_input, grad_output)[source]
Modify ReLU gradient to the smooth softplus gradient [Dombrowski et al., 2019].
- Parameters:
module (
torch.nn.Module) – The module to which this hook is attached.grad_input (
torch.Tensor) – The input gradient tensor.grad_output (
torch.Tensor) – The output gradient tensor.
- Returns:
The modified grad_output.
- Return type:
torch.Tensor