Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add batch_size to noise tunnel #555

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
26 changes: 26 additions & 0 deletions captum/_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ def _expand_target(
return target


def _expand_feature_mask(
feature_mask: Union[Tensor, Tuple[Tensor, ...]], n_samples: int
):
is_feature_mask_tuple = _is_tuple(feature_mask)
feature_mask = _format_tensor_into_tuples(feature_mask)
feature_mask_new = tuple(
feature_mask_elem.repeat_interleave(n_samples, dim=0)
if feature_mask_elem.size(0) > 1
else feature_mask_elem
for feature_mask_elem in feature_mask
)
return _format_output(is_feature_mask_tuple, feature_mask_new)


def _expand_and_update_baselines(
inputs: Tuple[Tensor, ...],
n_samples: int,
Expand Down Expand Up @@ -317,6 +331,18 @@ def _expand_and_update_target(n_samples: int, kwargs: dict):
kwargs["target"] = target


def _expand_and_update_feature_mask(n_samples: int, kwargs: dict):
if "feature_mask" not in kwargs:
return

feature_mask = kwargs["feature_mask"]
if feature_mask is None:
return

feature_mask = _expand_feature_mask(feature_mask, n_samples)
kwargs["feature_mask"] = feature_mask


@typing.overload
def _format_output(
is_inputs_tuple: Literal[True], output: Tuple[Tensor, ...]
Expand Down
Loading