Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix layer_gradient_x_activation and add logging for metrics #643

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions captum/metrics/_core/infidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
safe_div,
)
from captum._utils.typing import BaselineType, TargetType, TensorOrTupleOfTensorsGeneric
from captum.log import log_usage
from captum.metrics._utils.batching import _divide_and_aggregate_metrics


Expand Down Expand Up @@ -108,6 +109,7 @@ def default_perturb_func(
return sub_infidelity_perturb_func_decorator


@log_usage()
def infidelity(
forward_func: Callable,
perturb_func: Callable,
Expand Down
2 changes: 2 additions & 0 deletions captum/metrics/_core/sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
_format_tensor_into_tuples,
)
from captum._utils.typing import TensorOrTupleOfTensorsGeneric
from captum.log import log_usage
from captum.metrics._utils.batching import _divide_and_aggregate_metrics


Expand Down Expand Up @@ -57,6 +58,7 @@ def default_perturb_func(
return perturbed_input


@log_usage()
def sensitivity_max(
explanation_func: Callable,
inputs: TensorOrTupleOfTensorsGeneric,
Expand Down
6 changes: 6 additions & 0 deletions tests/attr/layer/test_layer_gradient_x_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def test_simple_input_gradient_activation(self) -> None:
inp = torch.tensor([[0.0, 100.0, 0.0]], requires_grad=True)
self._layer_activation_test_assert(net, net.linear0, inp, [0.0, 400.0, 0.0])

def test_simple_input_gradient_activation_no_grad(self) -> None:
net = BasicModel_MultiLayer()
inp = torch.tensor([[0.0, 100.0, 0.0]], requires_grad=True)
with torch.no_grad():
self._layer_activation_test_assert(net, net.linear0, inp, [0.0, 400.0, 0.0])

def test_simple_linear_gradient_activation(self) -> None:
net = BasicModel_MultiLayer()
inp = torch.tensor([[0.0, 100.0, 0.0]])
Expand Down