-
Notifications
You must be signed in to change notification settings - Fork 36
Feature: Implement KMeans attribution support and TakesMost LRP rule #214
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
base: main
Are you sure you want to change the base?
Conversation
This commit introduces several new components to enhance k-means based
model attribution and provide a new LRP rule for pooling layers:
1. New Layers (`src/zennit/layer.py`):
- `PairwiseCentroidDistance`: Computes pairwise distances between
inputs and a set of centroids.
- `NeuralizedKMeans`: A layer representing k-means discriminants
as a linear transformation (tensor-matrix product with bias).
- `MinPool1d` and `MinPool2d`: Min-pooling layers for 1D and 2D
inputs, implemented by negating inputs/outputs of MaxPool.
2. New Canonizer (`src/zennit/canonizers.py`):
- `KMeansCanonizer`: Replaces `PairwiseCentroidDistance` layers
(with `power=2`) with a sequence of `NeuralizedKMeans`,
`MinPool1d`, and `torch.nn.Flatten`. This neuralization
facilitates the application of LRP rules to k-means like
clustering outputs.
3. New LRP Rule (`src/zennit/rules.py`):
- `TakesMost`: An LRP rule designed for max-pooling layers (and
by extension, min-pooling layers after neuralization).
- Distributes relevance based on a softmax of input contributions
within each pooling window, providing a "soft" alternative to
winner-takes-all.
- Implements window-wise maximum subtraction for robust numerical
stability during the softmax calculation.
- Dynamically adapts internal convolution, pooling, and transposed
convolution operations to match the input dimensionality (1D/2D).
- Utilizes common, refactored parameters for unpooling operations.
- Respects the original module's hyperparameters.
4. New Tests (`tests/`):
- `test_kmeans_canonizer` in `test_canonizers.py`: Verifies the
correctness of the `KMeansCanonizer`, ensuring module replacement,
functional equivalence of cluster assignments, and proper restoration.
|
Some tests fail, but I think these are mostly lint errors coming from the main branch. |
|
Hey Jacob, Thanks a lot for finalizing the PR! I will get rid of the tests on Python 3.7, so that's fine. Could you go through the places that are missing in the coverage report? Some might be unrelated to KMeans and TakesMost, so you can ignore those. It would be good to have those places at least called in some test. |
|
Just a heads-up @jackmcrider : I am planning to go over this again, fix potential issues, and merge once #215 is merged. Ideally, I will be done by today. |
|
I did not see your reply until now, sorry. You mean more tests, right? I will provide more, but not today. Thank you for considering this PR! |
|
Thanks! I might also look into it, as I am pushing Zennit this week for a new release. I have some other things still on the list, but would be happy to get this PR into the release. Some building infrastructure changed, but the building and testing should still work with the same commands. Be sure to rebase. |
3fbdb43 to
6204e31
Compare
This commit introduces several new components to enhance k-means based model attribution and provide a new LRP rule for pooling layers:
New Layers (
src/zennit/layer.py):PairwiseCentroidDistance: Computes pairwise distances between inputs and a set of centroids.NeuralizedKMeans: A layer representing k-means discriminants as a linear transformation (tensor-matrix product with bias).MinPool1dandMinPool2d: Min-pooling layers for 1D and 2D inputs, implemented by negating inputs/outputs of MaxPool.New Canonizer (
src/zennit/canonizers.py):KMeansCanonizer: ReplacesPairwiseCentroidDistancelayers (withpower=2) with a sequence ofNeuralizedKMeans,MinPool1d, andtorch.nn.Flatten. This neuralization enables the application of LRP rules to k-means like clustering outputs.New LRP Rule (
src/zennit/rules.py):TakesMost: An LRP rule designed for max-pooling layers (and by extension, min-pooling layers after neuralization).New Tests (
tests/):test_kmeans_canonizerintest_canonizers.py: Verifies the correctness of theKMeansCanonizer, ensuring module replacement, functional equivalence of cluster assignments, and proper restoration.Replaces #197
Closes #198