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

Open
wants to merge 141 commits into
base: master
Choose a base branch
from
Open

Example-based #165

wants to merge 141 commits into from

Conversation

AntoninPoche
Copy link
Collaborator

@AntoninPoche AntoninPoche commented Sep 25, 2024

Introduce the Example-based module

This pull request introduces example-based methods from four families of methods. Namely:

  • Similar Examples
  • Semi-factuals
  • Counterfactuals
  • Prototypes

The API

The API is common for the four families, and it has three steps:

  • Construct a projection function with the model.
  • Initialize the method with a dataset, projection, and other parameters.
  • Explain given samples with a local explanation.
projection = ProjectionMethod(model)

explainer = ExampleMethod(
    cases_dataset=cases_dataset, 
    k=k,
    projection=projection,
    case_returns=case_returns,
    distance=distance,
)

examples = explainer.explain(inputs, targets)

AdelinCO and others added 30 commits February 14, 2024 16:26
@AntoninPoche AntoninPoche self-assigned this Sep 26, 2024
Copy link
Collaborator Author

@AntoninPoche AntoninPoche left a comment

Choose a reason for hiding this comment

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

First part of my review. I will wait to review all to make a final comment.

| `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.

@@ -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.

| **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.

# 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**]()
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.

docs/api/example_based/api_example_based.md Show resolved Hide resolved
xplique/example_based/base_example_method.py Show resolved Hide resolved
xplique/example_based/base_example_method.py Show resolved Hide resolved
xplique/example_based/base_example_method.py Outdated Show resolved Hide resolved
xplique/example_based/base_example_method.py Outdated Show resolved Hide resolved
xplique/example_based/base_example_method.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@lucashervier lucashervier left a comment

Choose a reason for hiding this comment

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

I think we have done a nice first draft. I have made some comments on the points I do not want to forget to discuss with you. I have made very few comments on prototypes as I am not familiar with the methods.

.pylintrc Outdated
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.

@@ -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

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

| **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

Choose a reason for hiding this comment

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

Same

# 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**]()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

| `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

Choose a reason for hiding this comment

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

Same



def tensor_sanitize(inputs: Union[tf.data.Dataset, tf.Tensor, np.ndarray],
targets: Optional[Union[tf.Tensor, np.ndarray]]) -> Tuple[tf.Tensor, tf.Tensor]:
targets: Union[tf.Tensor, np.ndarray]) -> Tuple[tf.Tensor, tf.Tensor]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can't use None anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was never the case for classic attribution methods. The typing did not correspond to what the method supported.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If I am correct, there are no tests for this script.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All functions in this file are tested through the tests/example_based/test_torch.py file.

return False


def is_not_shuffled(dataset: Optional[tf.data.Dataset]) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The name is not really well suited. In addition, I would rather use an iterator to check that is two instantiations of the same dataset..

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 agree on the name, but what do you mean on the iterator?

xplique/example_based/projections/base.py Outdated Show resolved Hide resolved
xplique/example_based/projections/commons.py Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@AntoninPoche AntoninPoche left a comment

Choose a reason for hiding this comment

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

Second part of my review.

xplique/example_based/counterfactuals.py Show resolved Hide resolved
xplique/example_based/counterfactuals.py Outdated Show resolved Hide resolved
xplique/example_based/counterfactuals.py Show resolved Hide resolved
xplique/example_based/counterfactuals.py Outdated Show resolved Hide resolved
xplique/example_based/counterfactuals.py Outdated Show resolved Hide resolved
xplique/example_based/projections/commons.py Show resolved Hide resolved
xplique/example_based/projections/commons.py Outdated Show resolved Hide resolved
xplique/example_based/projections/hadamard.py Outdated Show resolved Hide resolved
xplique/example_based/projections/hadamard.py Outdated Show resolved Hide resolved
xplique/example_based/projections/latent_space.py Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@AntoninPoche AntoninPoche left a comment

Choose a reason for hiding this comment

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

Third part of the review, there are many small modifications in docstrings.

xplique/example_based/prototypes.py Show resolved Hide resolved
xplique/example_based/prototypes.py Outdated Show resolved Hide resolved
xplique/example_based/prototypes.py Outdated Show resolved Hide resolved
xplique/example_based/prototypes.py Outdated Show resolved Hide resolved
xplique/example_based/prototypes.py Outdated Show resolved Hide resolved
xplique/example_based/similar_examples.py Outdated Show resolved Hide resolved
xplique/example_based/similar_examples.py Show resolved Hide resolved
xplique/example_based/similar_examples.py Outdated Show resolved Hide resolved
xplique/example_based/similar_examples.py Show resolved Hide resolved
xplique/plots/image.py Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@AntoninPoche AntoninPoche left a comment

Choose a reason for hiding this comment

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

To summarize remaining comments:

  • No link or wrong link for prototypes tutorial.
  • Some questions on example based test, for pytorch ones and the ones regarding datasets operations.
  • Questions on the FilterKNN on how the mask is applied.
  • A pylint problem too-many-positional-arguments. If resolved, this could lead to loose retro-compatibility. See pylint doc on this warning for more informations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants