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

Example-based #165

Closed
wants to merge 146 commits into from
Closed
Show file tree
Hide file tree
Changes from 136 commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
c8df34e
example-based: add cole method
AdelinCO May 29, 2023
c331801
tests: unit testing cole
AdelinCO May 29, 2023
e1e4d82
example based: introduce projections
May 29, 2023
eda3317
example based: introduce search methods
May 29, 2023
4b0a340
example based: add base class
May 29, 2023
0daf281
cole: update and improve
Feb 14, 2024
62a621d
example based tests: update and complete
May 29, 2023
2aa133d
plots: add image visualization for example based
Feb 14, 2024
bed4747
commons: add operations for tf dataset
Feb 14, 2024
9f9acfb
pylint: disable similarities for signatures
Feb 14, 2024
d29f92f
example based: introduce base example method abstraction
Feb 12, 2024
69fef12
example based: adapt similar examples
Feb 12, 2024
539c387
example based: adapt cole
Feb 12, 2024
7a58c9f
example based: adapt tests
Feb 12, 2024
2255896
base example method: dataset projections in projections
Feb 14, 2024
ead19ea
base projection: dataset projections in projections
Feb 14, 2024
48bc853
latent space projection: dataset projections in projections
Feb 14, 2024
fcacfd1
attribution projection: dataset projections in projections
Feb 14, 2024
4dbb6b6
example based: introduce hadamard projection
Feb 14, 2024
6e8ddb1
base search method: remove projection from search
Feb 14, 2024
5abb298
knn search method: remove projection from search
Feb 14, 2024
92056f1
cole: add hadamard product possibility
Feb 14, 2024
3f69e7b
projections: factorize model splitting
Feb 14, 2024
5cdf1c1
example based projections: fuse custom projection with base class
Feb 28, 2024
0c141bb
projections tests: adapt to changes and complete
Feb 14, 2024
56ddd00
similar examples tests: adapt to changes
Feb 14, 2024
c44d256
cole tests: adapt to changes and add hadamard
Feb 14, 2024
445fce1
tf operations: add get device for dataset mapping
Feb 15, 2024
7cfa2f3
feat: add a new KNN object and improve distance computation efficiency
lucashervier Mar 6, 2024
52d1c6a
tests: add tests for KNNs
lucashervier Mar 6, 2024
939bbc8
feat: develop the naive semi factual, update search methods for integ…
lucashervier Mar 7, 2024
8d9f234
feat: add a semi factuals method that is dedicated to one specific label
lucashervier Mar 7, 2024
f5f05a3
feat: add a naive counter factuals methods and its test
lucashervier Mar 13, 2024
7b32df9
add Prototypes
ChafikBak Mar 21, 2024
a86fd55
add Prototypes fix up
ChafikBak Mar 21, 2024
d705573
add Prototypes fix up
ChafikBak Apr 4, 2024
4649436
add Prototypes fix up
ChafikBak Apr 30, 2024
a62fd8e
add Prototypes fix up
ChafikBak Apr 30, 2024
e839e24
feat: change the private initialize_cases_dataset method to a protec…
lucashervier May 21, 2024
b3ebe4a
feat: change the fill value to np.inf when gathering elements of a da…
lucashervier May 21, 2024
335dcb9
feat: add the kleor search methods and their tests
lucashervier May 21, 2024
fe5cc44
feat: add the possibilities as an initialisation args
lucashervier May 21, 2024
67b7804
feat: add the KLEOR example based method and its tests
lucashervier May 21, 2024
c6be204
feat: add the kleor method in the package init
lucashervier May 21, 2024
aa9a500
fix: change the set_returns and set_k methods to properties with a se…
lucashervier May 21, 2024
7d49cfd
example based: merge contrastive
May 22, 2024
9d5062e
example based: merge and solve part of problems and refacto
May 22, 2024
026c29e
fix: change the fill value depending on the dataset type
lucashervier May 22, 2024
31bb022
fix: update kleor search and example base methods to fit the new inte…
lucashervier May 22, 2024
19d7cb3
example based: cole tests pass
May 23, 2024
122f8d5
docs: refactoring of the documentation for the new interfaces
lucashervier May 29, 2024
7a66525
add the documentation for the prototypes search methods
ChafikBak Jul 3, 2024
c8047b0
prototypes: hotfix
Jul 8, 2024
693fa42
prototypes: hotfix
Jul 8, 2024
d1e8031
example-based: make tests pass
Jul 8, 2024
84fdb68
prototypes: support non-identity projections
Jul 8, 2024
a7a9cc4
example-based: hotfix
Jul 9, 2024
30e6a56
prototypes: enhance tests and adapt code
Jul 9, 2024
e073178
tf dataset operations: make them on cpu
Jul 10, 2024
1e1b0c7
test prototypes: remove absurd tests
Jul 10, 2024
edabcda
example based: linting
Jul 10, 2024
5904a77
prototypes: change constant for memory
Jul 10, 2024
dee4793
test contrastive: add projection test
Jul 11, 2024
2284905
contrastive: solve projection problems
Jul 11, 2024
0356279
semi-factual: allow to return nuns labels
Jul 11, 2024
ac812ae
prototypes: linting
Jul 11, 2024
f8a377d
example based: clarify distances
Jul 11, 2024
be1ce6d
example based: clarify distances
Jul 11, 2024
ae69b78
example based: clarify distances
Jul 11, 2024
90cb2cf
example base projection: support pytorch
Jul 16, 2024
5f88f74
projections: add initialization from splitted model and target free o…
Jul 17, 2024
25d5331
add the documentation for the prototypes search methods fix up
ChafikBak Jul 18, 2024
c889d48
example-based search methods: add infitity norm distance
Jul 19, 2024
b6c3ffb
example-based projections: debug and remove get inputs weights
Jul 19, 2024
10438b0
example-based: remove weights from possible returns
Jul 19, 2024
2607b56
Merge branch 'antonin/example-based-merge' of https://github.com/deel…
Jul 19, 2024
797f76e
example-based projections: put target free operator as common
Jul 22, 2024
d265330
example-based: split counterfactuals and semifactuals
Jul 22, 2024
56baf6b
add the documentation for the prototypes search methods fix up
ChafikBak Jul 24, 2024
fdee972
add the documentation for the prototypes search methods fix up
ChafikBak Jul 25, 2024
96fbf96
docs: create the api page for example based methods, search methods a…
lucashervier Jul 26, 2024
6e99393
docs: create the KLEOR page
lucashervier Jul 26, 2024
026634e
docs: add pages for naive and label aware cf, for kleor search method…
lucashervier Jul 26, 2024
393914e
docs: add the page for similar examples
lucashervier Jul 30, 2024
37312da
docs: create the documentation page for Cole
lucashervier Jul 30, 2024
6d1247c
fixup: wrong github link
lucashervier Jul 30, 2024
27af808
fixup: in cole documentation, change misleading information
lucashervier Jul 30, 2024
6d15e53
docs: add a documentation page for projections
lucashervier Aug 1, 2024
64903ab
docs: add some details in the api documentation
lucashervier Aug 1, 2024
5b6f026
docs: update the mkdocs.yml
lucashervier Aug 2, 2024
70106fe
example based docs: small modifications
Aug 2, 2024
467c79b
example based: small fixes
Aug 2, 2024
3e638a8
docs: improve documentation with antonin's feedbacks, modify a parame…
lucashervier Aug 5, 2024
410ed98
docs: change the wording of the prototypes such that the search metho…
lucashervier Aug 12, 2024
fbef589
docs: update the main example-based doc page considering antonin's co…
lucashervier Aug 12, 2024
4c2dbc0
projections tests: adapt to commons evolution
Aug 13, 2024
e7f9d4a
pojections tests: add simple splitteing test
Aug 13, 2024
85fc97d
fix: correction on the labelawarecf method for computation of the mas…
lucashervier Aug 14, 2024
1941eea
Merge branch 'antonin/example-based-merge' of github.com:deel-ai/xpli…
lucashervier Aug 14, 2024
1bb8a16
projections: add warning to tensorflow splitting
Aug 14, 2024
ac74a9d
commons: linting
Aug 14, 2024
84a3e65
Merge branch 'antonin/example-based-merge' of https://github.com/deel…
Aug 14, 2024
d709f06
requirements: limit to tensorflow < 2.16
Aug 14, 2024
cedf744
linting
Aug 14, 2024
0316d55
docs: update the README, add warning concerning the tensorflow versio…
lucashervier Aug 14, 2024
02b248a
linting
Aug 19, 2024
41b7476
Merge branch 'antonin/example-based-merge' of https://github.com/deel…
Aug 19, 2024
53b4ba4
projections: add missing targets warning
AntoninPoche Aug 23, 2024
3de2150
docs: update prototypes
ChafikBak Sep 9, 2024
19d8340
docs: update prototypes
ChafikBak Sep 12, 2024
67890c0
pylint: remove E1101 no-member warning
AntoninPoche Sep 18, 2024
5922f0b
counterfactuals: solve label aware explain signature
AntoninPoche Sep 18, 2024
1edde24
datasets operations: move them to example based
AntoninPoche Sep 18, 2024
38fcd81
example based: factorize inputs harmonization
AntoninPoche Sep 18, 2024
0274191
example based: add support for torch dataloader
AntoninPoche Sep 18, 2024
468cbfd
example based: add support for torch dataloader fixup
AntoninPoche Sep 18, 2024
be5bb74
projections: add warning
AntoninPoche Sep 18, 2024
587647c
datasets operations: move them to example based fixup
AntoninPoche Sep 18, 2024
51d040d
Merge branch 'antonin/example-based-merge' of https://github.com/deel…
AntoninPoche Sep 18, 2024
0f92127
docs: update prototypes
ChafikBak Sep 19, 2024
35e1962
Merge branch 'antonin/example-based-merge' of github.com:deel-ai/xpli…
ChafikBak Sep 19, 2024
0e0ab2e
projections: correct from splitted model
AntoninPoche Sep 24, 2024
9662bca
projections: correct target free operator
AntoninPoche Sep 24, 2024
4ddb859
projections: small fix to base
AntoninPoche Sep 24, 2024
8d9975d
commons: sanitize inputs targets disable pylint warning
AntoninPoche Sep 24, 2024
4f94e6b
tests projections: add tests for from splitted model and target free …
AntoninPoche Sep 24, 2024
f515f14
plots: small fix
AntoninPoche Sep 24, 2024
f8bc136
base example methods: projection can now be none
AntoninPoche Sep 24, 2024
f8bb9f8
example based: small pylint fixes
AntoninPoche Sep 24, 2024
cb8eee9
search methods: small pylint fixes
AntoninPoche Sep 24, 2024
f436713
prototypes: optimize and harmonize
AntoninPoche Sep 24, 2024
145f0d5
tests prototypes: adapt to prototypes changes and complete
AntoninPoche Sep 24, 2024
222527a
Merge branch 'antonin/example-based-merge' of https://github.com/deel…
AntoninPoche Sep 24, 2024
c8a0abe
prototypes: small fix
AntoninPoche Sep 25, 2024
97fd2cf
ci: restrict numpy version and specify torch test
AntoninPoche Sep 25, 2024
a4414dd
test example based: small fix
AntoninPoche Sep 25, 2024
4debfd6
rise: fix pylint
AntoninPoche Sep 30, 2024
ef7db46
all: resolve part of example based pr comments
AntoninPoche Oct 3, 2024
b684ad3
example based: small fix
AntoninPoche Oct 3, 2024
a876d7e
example based: small fix
AntoninPoche Oct 3, 2024
3bbf406
example based: small fix
AntoninPoche Oct 4, 2024
f316e9f
example based docs: add prototypes tuto link
AntoninPoche Oct 7, 2024
9c7e3ee
example based: fix pylint issues
AntoninPoche Oct 7, 2024
eaa9d49
setup: ignore pylint too-many-positionnal-arguments because of retroc…
AntoninPoche Oct 7, 2024
aac96c2
example based: small fixes
AntoninPoche Oct 7, 2024
3f4b406
example based tests: small fix
AntoninPoche Oct 7, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ coverage.xml
.pytest_cache/
cover/
*test*.sh
tests/concepts/checkpoints/

# Environments
.env
Expand Down
20 changes: 20 additions & 0 deletions .pylintrc
Copy link
Collaborator

Choose a reason for hiding this comment

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

We removed it to have all configurations in the setup.cfg file instead. Why do we add it back?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will try to remove it and see it works again.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[MASTER]
disable=
R0903, # allows to expose only one public method
R0914, # allow multiples local variables
E0401, # pending issue with pylint see pylint#2603
E1123, # issues between pylint and tensorflow since 2.2.0
E1120, # see pylint#3613
C3001, # lambda function as variable
E1101, # (no-member), flag for every tf.keras

[FORMAT]
max-line-length=100
max-args=12

[SIMILARITIES]
min-similarity-lines=6
ignore-comments=yes
ignore-docstrings=yes
ignore-imports=no
ignore-signatures=yes
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<a href="#">
<img src="https://img.shields.io/badge/Python-3.7, 3.8, 3.9, 3.10-efefef">
</a>
<a href="#">
<img src="https://img.shields.io/badge/Tensorflow-2.5, ..., 2.15-00008b">
</a>
<a href="https://github.com/deel-ai/xplique/actions/workflows/python-lints.yml">
<img alt="PyLint" src="https://github.com/deel-ai/xplique/actions/workflows/python-lints.yml/badge.svg">
</a>
Expand Down Expand Up @@ -41,8 +44,13 @@
<a href="https://deel-ai.github.io/xplique/latest/api/feature_viz/feature_viz/">Feature Visualization</a>
·
<a href="https://deel-ai.github.io/xplique/latest/api/attributions/metrics/api_metrics/">Metrics</a>
.
<a href="api/example_based/api_example_based/">Example-based</a>
</p>

> [!IMPORTANT]
> With the release of Keras 3.X since TensorFlow 2.16, some methods may not function as expected. We are actively working on a fix. In the meantime, we recommend using TensorFlow 2.15 or earlier versions for optimal compatibility.

The library is composed of several modules, the _Attributions Methods_ module implements various methods (e.g Saliency, Grad-CAM, Integrated-Gradients...), with explanations, examples and links to official papers.
The _Feature Visualization_ module allows to see how neural networks build their understanding of images by finding inputs that maximize neurons, channels, layers or compositions of these elements.
The _Concepts_ module allows you to extract human concepts from a model and to test their usefulness with respect to a class.
Expand All @@ -54,6 +62,9 @@ Finally, the _Metrics_ module covers the current metrics used in explainability.

<br>

> [!NOTE]
> We are proud to announce the release of the _Example-based_ module! This module is dedicated to methods that explain a model by retrieving relevant examples from a dataset. It includes methods that belong to different families: similar examples, contrastive (counter-factuals and semi-factuals) examples, and prototypes (as concepts based methods have a dedicated sections).

## 🔥 Tutorials

<details>
Expand Down Expand Up @@ -110,6 +121,8 @@ Finally, the _Metrics_ module covers the current metrics used in explainability.
</a>
</p>

- [**Example-based Methods**: Getting started](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) <sub> [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) </sub>

You can find a certain number of [**other practical tutorials just here**](https://github.com/deel-ai/xplique/blob/master/TUTORIALS.md). This section is actively developed and more contents will be
included. We will try to cover all the possible usage of the library, feel free to contact us if you have any suggestions or recommendations towards tutorials you would like to see.

Expand Down Expand Up @@ -361,6 +374,28 @@ TF : Tensorflow compatible

</details>

Even though we are only at the early stages, we have also recently added an [Example-based methods](api/example_based/api_example_based/) module. Do not hesitate to give us feedback! Currently, the methods available are summarized in the following table:

<details>
<summary><b>Table of example-based methods available</b></summary>

| Method | Family | Documentation | Tutorial |
| --- | --- | --- | --- |
| `SimilarExamples` | Similar Examples | [SimilarExamples](../similar_examples/similar_examples/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| `Cole` | Similar Examples | [Cole](../similar_examples/cole/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| | | |
| `NaiveCounterFactuals` | Counter Factuals | [NaiveCounterFactuals](../counterfactuals/naive_counter_factuals/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| `LabelAwareCounterFactuals` | Counter Factuals | [LabelAwareCounterFactuals](../counterfactuals/label_aware_counter_factuals/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
||||
| `KLEORSimMiss` | Semi Factuals | [KLEOR](../semifactuals/kleor/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| `KLEORGlobalSim` | Semi Factuals | [KLEOR](../semifactuals/kleor/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
||||
| `ProtoGreedy` | Prototypes | [ProtoGreedy](../prototypes/proto_greedy/) | **TODO** |
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Add link when tutorial is ready.

| `ProtoDash` | Prototypes | [ProtoDash](../prototypes/proto_dash/) | **TODO** |
| `MMDCritic` | Prototypes | [MMDCritic](../prototypes/mmd_critic/) | **TODO** |

</details>

## 👍 Contributing

Feel free to propose your ideas or come and contribute with us on the Xplique toolbox! We have a specific document where we describe in a simple way how to make your first pull request: [just here](https://github.com/deel-ai/xplique/blob/master/CONTRIBUTING.md).
Expand Down
9 changes: 9 additions & 0 deletions TUTORIALS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Here is the lists of the available tutorial for now:
| Metrics | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1WEpVpFSq-oL1Ejugr8Ojb3tcbqXIOPBg) |
| Concept Activation Vectors | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1iuEz46ZjgG97vTBH8p-vod3y14UETvVE) |
| Feature Visualization | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1st43K9AH-UL4eZM1S4QdyrOi7Epa5K8v) |
| Example-Based Methods | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| Prototypes | **TODO** |
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Add a link when the tutorial is ready.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to actually add the link of the notebook here



## Attributions
Expand Down Expand Up @@ -74,3 +76,10 @@ Here is the lists of the available tutorial for now:
| :------------------------------------- | :-----------------------------------------------------------------------------------------------------------------------------------------------------: |
| Feature Visualization: Getting started | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1st43K9AH-UL4eZM1S4QdyrOi7Epa5K8v) |
| Modern Feature Visualization: MaCo | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1l0kag1o-qMY4NCbWuAwnuzkzd9sf92ic) |

## Example-Based Methods

| **Tutorial Name** | Notebook |
| :------------------------------------- | :-----------------------------------------------------------------------------------------------------------------------------------------------------: |
| Example-Based Methods: Getting started | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| Prototypes: Getting started | **TODO** |
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Add a link when the tutorial is ready.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

127 changes: 127 additions & 0 deletions docs/api/example_based/api_example_based.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# API: Example-based

- [**Example-based Methods**: Getting started](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) <sub> [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) </sub>
- [**TODO: Add the Getting Started on Prototypes**]()
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved

## Context ##

!!! quote
While saliency maps have stolen the show for the last few years in the XAI field, their ability to reflect models' internal processes has been questioned. Although less in the spotlight, example-based XAI methods have continued to improve. It encompasses methods that use samples as explanations for a machine learning model's predictions. This aligns with the psychological mechanisms of human reasoning and makes example-based explanations natural and intuitive for users to understand. Indeed, humans learn and reason by forming mental representations of concepts based on examples.

-- <cite>[Natural Example-Based Explainability: a Survey (2023)](https://arxiv.org/abs/2309.03234)</cite>[^1]

As mentioned by our team members in the quote above, example-based methods are an alternative to saliency maps and can be more aligned with some users' expectations. Thus, we have been working on implementing some of those methods in Xplique that have been put aside in the previous developments.

While not being exhaustive we tried to cover a range of methods that are representative of the field and that belong to different families: similar examples, contrastive (counter-factuals and semi-factuals) examples, and prototypes (as concepts based methods have a dedicated sections).

At present, we made the following choices:
- Focus on methods that are natural example methods (post-hoc and non-generative, see the paper above for more details).
- Try to unify the four families of approaches with a common API.

!!! info
We are in the early stages of development and are looking for feedback on the API design and the methods we have chosen to implement. Also, we are counting on the community to furnish the collection of methods available. If you are willing to contribute reach us on the [GitHub](https://github.com/deel-ai/xplique) repository (with an issue, pull request, ...).

## Common API ##

```python
projection = ProjectionMethod(model)

explainer = ExampleMethod(
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved
cases_dataset,
labels_dataset,
targets_dataset,
k,
projection,
case_returns,
batch_size,
**kwargs
)

explanations = explainer.explain(inputs, targets)
```

We tried to keep the API as close as possible to the one of the attribution methods to keep a consistent experience for the users.

The `BaseExampleMethod` is an abstract base class designed for example-based methods used to explain classification models. It provides examples from a dataset (usually the training dataset) to help understand a model's predictions. Examples are projected from the input space to a search space using a [projection function](#projections). The projection function defines the search space. Then, examples are selected using a [search method](#search-methods) within the search space. For all example-based methods, one can define the `distance` that will be used by the search method.

We can broadly categorize example-based methods into four families: similar examples, counter-factuals, semi-factuals, and prototypes.

- **Similar Examples**: This method involves finding instances in the dataset that are similar to a given instance. The similarity is often determined based on the feature space, and these examples can help in understanding the model's decision by showing what other data points resemble the instance in question.
- **Counter Factuals**: Counterfactual explanations identify the minimal changes needed to an instance's features to change the model's prediction to a different, specified outcome. They help answer "what-if" scenarios by showing how altering certain aspects of the input would lead to a different decision.
- **Semi Factuals**: Semifactual explanations describe hypothetical situations where most features of an instance remain the same except for one or a few features, without changing the overall outcome. They highlight which features could vary without altering the prediction.
- **Prototypes**: Prototypes are representative examples from the dataset that summarize typical cases within a certain category or cluster. They act as archetypal instances that the model uses to make predictions, providing a reference point for understanding model behavior. Additional documentation can be found in the [Prototypes API documentation](../prototypes/api_prototypes/).

??? abstract "Table of example-based methods available"

| Method | Family | Documentation | Tutorial |
| --- | --- | --- | --- |
| `SimilarExamples` | Similar Examples | [SimilarExamples](../similar_examples/similar_examples/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| `Cole` | Similar Examples | [Cole](../similar_examples/cole/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| | | |
| `NaiveCounterFactuals` | Counter Factuals | [NaiveCounterFactuals](../counterfactuals/naive_counter_factuals/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| `LabelAwareCounterFactuals` | Counter Factuals | [LabelAwareCounterFactuals](../counterfactuals/label_aware_counter_factuals/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
||||
| `KLEORSimMiss` | Semi Factuals | [KLEOR](../semifactuals/kleor/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
| `KLEORGlobalSim` | Semi Factuals | [KLEOR](../semifactuals/kleor/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1gA7mhWhWzdKholZWkTvAg4FzFnzS8NHF) |
||||
| `ProtoGreedy` | Prototypes | [ProtoGreedy](../prototypes/proto_greedy/) | **TODO** |
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved
| `ProtoDash` | Prototypes | [ProtoDash](../prototypes/proto_dash/) | **TODO** |
| `MMDCritic` | Prototypes | [MMDCritic](../prototypes/mmd_critic/) | **TODO** |

### Parameters ###

- **cases_dataset** (`Union[tf.data.Dataset, tf.Tensor, np.ndarray]`): The dataset used to train the model, from which examples are extracted. It should be batched as TensorFlow provides no method to verify this. Ensure the dataset is not reshuffled at each iteration.
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved
- **labels_dataset** (`Optional[Union[tf.data.Dataset, tf.Tensor, np.ndarray]]`): Labels associated with the examples in the cases dataset. Indices should match the `cases_dataset`.
- **targets_dataset** (`Optional[Union[tf.data.Dataset, tf.Tensor, np.ndarray]]`): Targets associated with the `cases_dataset` for dataset projection, often the one-hot encoding of a model's predictions.
- **k** (`int`): The number of examples to retrieve per input.
- **projection** (`Union[Projection, Callable]`): A projection or callable function that projects samples from the input space to the search space. The search space should be relevant for the model. (see [Projections](#projections))
- **case_returns** (`Union[List[str], str]`): Elements to return in `self.explain()`. Default is "examples".
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved
- **batch_size** (`Optional[int]`): Number of samples processed simultaneously for projection and search. Ignored if `cases_dataset` is a `tf.data.Dataset`.

!!!tips
If the elements of your dataset are tuples (cases, labels), you can pass this dataset directly to the `cases_dataset`.

!!!tips
Apart from contrastive explanations, in the case of classification, the built-in [Projections](#projections) compute `targets` online and the `targets_dataset` is not necessary.

### Properties ###

- **search_method_class** (`Type[BaseSearchMethod]`): Abstract property to define the search method class to use. Must be implemented in subclasses. (see [Search Methods](#search-methods))
- **k** (`int`): Getter and setter for the `k` parameter.
- **returns** (`Union[List[str], str]`): Getter and setter for the `returns` parameter. Defines the elements to return in `self.explain()`.

### `explain(self, inputs, targets)` ###

Returns the relevant examples to explain the (inputs, targets). Projects inputs using `self.projection` and finds examples using the `self.search_method`.

- **inputs** (`Union[tf.Tensor, np.ndarray]`): Input samples to be explained. Shape: (n, ...) where n is the number of samples.
- **targets** (`Optional[Union[tf.Tensor, np.ndarray]]`): Targets associated with the `cases_dataset` for dataset projection. Shape: (n, nb_classes) where n is the number of samples and nb_classes is the number of classes.
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved

**Returns:** Dictionary with elements listed in `self.returns`.

!!!info
The `__call__` method is an alias for the `explain` method.

## Projections ##
Projections are functions that map input samples to a search space where examples are retrieved with a `search_method`. The search space should be relevant for the model (e.g. projecting the inputs into the latent space of the model).

!!!info
If one decides to use the identity function as a projection, the search space will be the input space, thus rather explaining the dataset than the model.

The `Projection` class is a base class for projections. It involves two parts: `space_projection` and `weights`. The samples are first projected to a new space and then weighted.

!!!warning
If both parts are `None`, the projection acts as an identity function. In general, we advise that one part should involve the model to ensure meaningful distance calculations with respect to the model.

To know more about projections and their importance, you can refer to the [Projections](../../projections/) section.

## Search Methods ##
AntoninPoche marked this conversation as resolved.
Show resolved Hide resolved

Search methods are used to retrieve examples from the `cases_dataset` that are relevant to the input samples.

!!!warning
In an search method, the `cases_dataset` is the dataset that has been projected with a `Projection` object (see the previous section). The search methods are used to find examples in this projected space.

Each example-based method has its own search method. The search method is defined in the `search_method_class` property of the `ExampleMethod` class.

[^1]: [Natural Example-Based Explainability: a Survey (2023)](https://arxiv.org/abs/2309.03234)
Loading
Loading