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

Fixes for Boolean Input Tensors #666

Closed
wants to merge 1 commit into from
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: 1 addition & 1 deletion captum/_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _zeros(inputs: Tuple[Tensor, ...]) -> Tuple[int, ...]:
Takes a tuple of tensors as input and returns a tuple that has the same
length as `inputs` with each element as the integer 0.
"""
return tuple(0 for input in inputs)
return tuple(0 if input.dtype is not torch.bool else False for input in inputs)


def _format_baseline(
Expand Down
2 changes: 1 addition & 1 deletion captum/attr/_core/feature_ablation.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def _get_feature_range_and_mask(self, input, input_mask, **kwargs):
)

def _get_feature_counts(self, inputs, feature_mask, **kwargs):
""" return the numbers of input features """
"""return the numbers of input features"""
if not feature_mask:
return tuple(inp[0].numel() if inp.numel() else 0 for inp in inputs)

Expand Down
11 changes: 7 additions & 4 deletions captum/attr/_core/lime.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,18 @@ def default_from_interp_rep_transform(curr_sample, original_inputs, **kwargs):
), "Must provide baselines to use default interpretable representation transfrom"
feature_mask = kwargs["feature_mask"]
if isinstance(feature_mask, Tensor):
binary_mask = curr_sample[0][feature_mask].to(original_inputs.dtype)
return binary_mask * original_inputs + (1 - binary_mask) * kwargs["baselines"]
binary_mask = curr_sample[0][feature_mask].bool()
return (
binary_mask.to(original_inputs.dtype) * original_inputs
+ (~binary_mask).to(original_inputs.dtype) * kwargs["baselines"]
)
else:
binary_mask = tuple(
curr_sample[0][feature_mask[j]] for j in range(len(feature_mask))
curr_sample[0][feature_mask[j]].bool() for j in range(len(feature_mask))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change related to inputs being boolean or is it so that we can use ~ operator ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is necessary since if this mask is an int / float, multiplying by a boolean still casts product to int / float causing the masked input to not be a boolean.

)
return tuple(
binary_mask[j].to(original_inputs[j].dtype) * original_inputs[j]
+ (1 - binary_mask[j].to(original_inputs[j].dtype)) * kwargs["baselines"][j]
+ (~binary_mask[j]).to(original_inputs[j].dtype) * kwargs["baselines"][j]
for j in range(len(feature_mask))
)

Expand Down
2 changes: 1 addition & 1 deletion captum/attr/_core/occlusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,5 @@ def _get_feature_range_and_mask(
return 0, feature_max, None

def _get_feature_counts(self, inputs, feature_mask, **kwargs):
""" return the numbers of possible input features """
"""return the numbers of possible input features"""
return tuple(np.prod(counts).astype(int) for counts in kwargs["shift_counts"])
7 changes: 3 additions & 4 deletions captum/attr/_core/shapley_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,7 @@ def _perturbation_generator(
target_repeated = _expand_target(target, perturbations_per_eval)
for i in range(len(feature_permutation)):
current_tensors = tuple(
current
* (torch.tensor(1) - (mask == feature_permutation[i]).to(current.dtype))
current * (~(mask == feature_permutation[i])).to(current.dtype)
+ input * (mask == feature_permutation[i]).to(input.dtype)
for input, current, mask in zip(inputs, current_tensors, input_masks)
)
Expand Down Expand Up @@ -478,7 +477,7 @@ def _perturbation_generator(
)

def _get_n_evaluations(self, total_features, n_samples, perturbations_per_eval):
""" return the total number of forward evaluations needed """
"""return the total number of forward evaluations needed"""
return math.ceil(total_features / perturbations_per_eval) * n_samples


Expand Down Expand Up @@ -740,7 +739,7 @@ def attribute(
)

def _get_n_evaluations(self, total_features, n_samples, perturbations_per_eval):
""" return the total number of forward evaluations needed """
"""return the total number of forward evaluations needed"""
return math.ceil(total_features / perturbations_per_eval) * math.factorial(
total_features
)
4 changes: 2 additions & 2 deletions captum/metrics/_core/infidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def sub_infidelity_perturb_func_decorator(pertub_func: Callable) -> Callable:
def default_perturb_func(
inputs: TensorOrTupleOfTensorsGeneric, baselines: BaselineType = None
):
r""""""
r""" """
inputs_perturbed = (
pertub_func(inputs, baselines)
if baselines is not None
Expand Down Expand Up @@ -398,7 +398,7 @@ def _generate_perturbations(
"""

def call_perturb_func():
r""""""
r""" """
baselines_pert = None
inputs_pert: Union[Tensor, Tuple[Tensor, ...]]
if len(inputs_expanded) == 1:
Expand Down
24 changes: 24 additions & 0 deletions tests/attr/test_feature_ablation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
BasicModel_ConvNet_One_Conv,
BasicModel_MultiLayer,
BasicModel_MultiLayer_MultiInput,
BasicModelBoolInput,
BasicModelWithSparseInputs,
)

Expand Down Expand Up @@ -113,6 +114,29 @@ def test_simple_ablation_with_baselines(self) -> None:
perturbations_per_eval=(1, 2, 3),
)

def test_simple_ablation_boolean(self) -> None:
ablation_algo = FeatureAblation(BasicModelBoolInput())
inp = torch.tensor([[True, False, True]])
self._ablation_test_assert(
ablation_algo,
inp,
[[40.0, 40.0, 40.0]],
feature_mask=torch.tensor([[0, 0, 1]]),
perturbations_per_eval=(1, 2, 3),
)

def test_simple_ablation_boolean_with_baselines(self) -> None:
ablation_algo = FeatureAblation(BasicModelBoolInput())
inp = torch.tensor([[True, False, True]])
self._ablation_test_assert(
ablation_algo,
inp,
[[-40.0, -40.0, 0.0]],
feature_mask=torch.tensor([[0, 0, 1]]),
baselines=True,
perturbations_per_eval=(1, 2, 3),
)

def test_multi_sample_ablation(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[2.0, 10.0, 3.0], [20.0, 50.0, 30.0]], requires_grad=True)
Expand Down
26 changes: 26 additions & 0 deletions tests/attr/test_lime.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from tests.helpers.basic_models import (
BasicModel_MultiLayer,
BasicModel_MultiLayer_MultiInput,
BasicModelBoolInput,
)


Expand Down Expand Up @@ -146,6 +147,31 @@ def test_simple_lime_with_baselines(self) -> None:
test_generator=True,
)

def test_simple_lime_boolean(self) -> None:
net = BasicModelBoolInput()
inp = torch.tensor([[True, False, True]])
self._lime_test_assert(
net,
inp,
[31.42, 31.42, 30.90],
feature_mask=torch.tensor([[0, 0, 1]]),
perturbations_per_eval=(1, 2, 3),
test_generator=True,
)

def test_simple_lime_boolean_with_baselines(self) -> None:
net = BasicModelBoolInput()
inp = torch.tensor([[True, False, True]])
self._lime_test_assert(
net,
inp,
[-36.0, -36.0, 0.0],
feature_mask=torch.tensor([[0, 0, 1]]),
baselines=True,
perturbations_per_eval=(1, 2, 3),
test_generator=True,
)

@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_simple_lime_with_show_progress(self, mock_stderr) -> None:
net = BasicModel_MultiLayer()
Expand Down
24 changes: 24 additions & 0 deletions tests/attr/test_shapley.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from tests.helpers.basic_models import (
BasicModel_MultiLayer,
BasicModel_MultiLayer_MultiInput,
BasicModelBoolInput,
)


Expand All @@ -39,6 +40,29 @@ def test_simple_shapley_sampling_with_mask(self) -> None:
perturbations_per_eval=(1, 2, 3),
)

def test_simple_shapley_sampling_boolean(self) -> None:
net = BasicModelBoolInput()
inp = torch.tensor([[True, False, True]])
self._shapley_test_assert(
net,
inp,
[35.0, 35.0, 35.0],
feature_mask=torch.tensor([[0, 0, 1]]),
perturbations_per_eval=(1, 2, 3),
)

def test_simple_shapley_sampling_boolean_with_baseline(self) -> None:
net = BasicModelBoolInput()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way of testing would be using BasicModel_MultiLayer() with the transformed input and seeing if we get the same result but if we manually verified expected value, then it looks good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I double checked the values manually, they seem correct.

inp = torch.tensor([[True, False, True]])
self._shapley_test_assert(
net,
inp,
[-40.0, -40.0, 0.0],
feature_mask=torch.tensor([[0, 0, 1]]),
baselines=True,
perturbations_per_eval=(1, 2, 3),
)

def test_simple_shapley_sampling_with_baselines(self) -> None:
net = BasicModel_MultiLayer()
inp = torch.tensor([[20.0, 50.0, 30.0]])
Expand Down
15 changes: 15 additions & 0 deletions tests/helpers/basic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,21 @@ def forward(
return lin2_out


class BasicModelBoolInput(nn.Module):
def __init__(self) -> None:
super().__init__()
self.mod = BasicModel_MultiLayer()

def forward(
self,
x: Tensor,
add_input: Optional[Tensor] = None,
mult: float = 10.0,
):
assert x.dtype is torch.bool, "Input must be boolean"
return self.mod(x.float() * mult, add_input)


class BasicModel_MultiLayer_MultiInput(nn.Module):
def __init__(self) -> None:
super().__init__()
Expand Down