Skip to content

Commit

Permalink
Fixes TCAV concept ordering bug reported in issue 909 on GH
Browse files Browse the repository at this point in the history
Summary:
Fixes TCAV concept ordering bug reported in this issue: pytorch#909

The original implementation wasn't sorting the concepts correctly in the output. I changed so that the ordering is based on the input concept ordering in the experimental set.

Differential Revision: D35302278

fbshipit-source-id: 61fab8e24db586af514356d83843ab1bd4d026e3
  • Loading branch information
NarineK authored and facebook-github-bot committed Apr 1, 2022
1 parent d17e850 commit d7c82fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions captum/concept/_core/tcav.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,11 @@ def _tcav_sub_computation(
concepts_key = concepts_to_str(concepts)

# sort classes / concepts in the order specified in concept_keys
concept_ord = {concept.id: ci for ci, concept in enumerate(concepts)}
concept_ord = [concept.id for concept in concepts]
class_ord = {cls_: idx for idx, cls_ in enumerate(cls_set)}

new_ord = torch.tensor(
[concept_ord[cls] for cls in cls_set], device=tcav_score.device
[class_ord[cncpt] for cncpt in concept_ord], device=tcav_score.device
)

# sort based on classes
Expand Down
10 changes: 10 additions & 0 deletions tests/concept/test_tcav.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,16 @@ def test_TCAV_x_1_1_c(self) -> None:
processes=1,
)

def test_TCAV_x_1_1_c_concept_order_changed(self) -> None:
self.compute_cavs_interpret(
[["random", "striped"], ["random", "ceo"], ["ceo", "striped"]],
True,
0.4848,
0.5000,
8.185208066890937e-09,
processes=1,
)

# Non-existing concept in the experimental set ("dotted")
def test_TCAV_x_1_1_d(self) -> None:
self.compute_cavs_interpret(
Expand Down

0 comments on commit d7c82fe

Please sign in to comment.