Skip to content

Commit dad55b0

Browse files
bilalsalfacebook-github-bot
authored andcommitted
Make the 'eps' parameter user-adjustable in the .attribute() method. (#854)
Summary: Important to fix numerical stability issues as in #835 Pull Request resolved: #854 Reviewed By: vivekmig Differential Revision: D33988823 Pulled By: bilalsal fbshipit-source-id: 181a509a6ae60373ea340d7b15992f8c27f38e14
1 parent 580b1cf commit dad55b0

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

captum/attr/_core/deep_lift.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ class DeepLift(GradientAttribution):
103103
https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/
104104
"""
105105

106-
def __init__(self, model: Module, multiply_by_inputs: bool = True) -> None:
106+
def __init__(
107+
self,
108+
model: Module,
109+
multiply_by_inputs: bool = True,
110+
eps: float = 1e-10,
111+
) -> None:
107112
r"""
108113
Args:
109114
@@ -123,9 +128,16 @@ def __init__(self, model: Module, multiply_by_inputs: bool = True) -> None:
123128
are being multiplied by (inputs - baselines).
124129
This flag applies only if `custom_attribution_func` is
125130
set to None.
131+
132+
eps (float, optional): A value at which to consider output/input change
133+
significant when computing the gradients for non-linear layers.
134+
This is useful to adjust, depending on your model's bit depth,
135+
to avoid numerical issues during the gradient computation.
136+
Default: 1e-10
126137
"""
127138
GradientAttribution.__init__(self, model)
128139
self.model = model
140+
self.eps = eps
129141
self.forward_handles: List[RemovableHandle] = []
130142
self.backward_handles: List[RemovableHandle] = []
131143
self._multiply_by_inputs = multiply_by_inputs
@@ -322,7 +334,6 @@ def attribute( # type: ignore
322334
activations. The hooks and attributes will be removed
323335
after the attribution is finished"""
324336
)
325-
326337
baselines = _tensorize_baseline(inputs, baselines)
327338
main_model_hooks = []
328339
try:
@@ -471,7 +482,6 @@ def _backward_hook(
471482
module: Module,
472483
grad_input: Union[Tensor, Tuple[Tensor, ...]],
473484
grad_output: Union[Tensor, Tuple[Tensor, ...]],
474-
eps: float = 1e-10,
475485
):
476486
r"""
477487
`grad_input` is the gradient of the neuron with respect to its input
@@ -495,7 +505,12 @@ def _backward_hook(
495505
)
496506
multipliers = tuple(
497507
SUPPORTED_NON_LINEAR[type(module)](
498-
module, module.input, module.output, grad_input, grad_output, eps=eps
508+
module,
509+
module.input,
510+
module.output,
511+
grad_input,
512+
grad_output,
513+
eps=self.eps,
499514
)
500515
)
501516
# remove all the properies that we set for the inputs and output

0 commit comments

Comments
 (0)