-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix `is_layer_at_idx` for LRP * add composite lrp test
- Loading branch information
Showing
3 changed files
with
49 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
import tensorflow.keras.layers as klayers | ||
import tensorflow.keras.models as kmodels | ||
|
||
from innvestigate.analyzer.relevance_based.relevance_analyzer import LRP | ||
|
||
|
||
@pytest.mark.graph | ||
@pytest.mark.fast | ||
@pytest.mark.lrp | ||
def test_composite_lrp(): | ||
model = kmodels.Sequential( | ||
[ | ||
klayers.Input(shape=(28, 28, 3)), | ||
klayers.Conv2D(8, 3, activation="relu"), | ||
klayers.Conv2D(4, 5, activation="relu"), | ||
klayers.Flatten(), | ||
klayers.Dense(16, activation="relu"), | ||
klayers.Dense(2, activation="softmax"), | ||
] | ||
) | ||
analyzer = LRP( | ||
model, | ||
rule="Z", | ||
input_layer_rule="Flat", | ||
until_layer_idx=2, | ||
until_layer_rule="Epsilon", | ||
) | ||
correct_rules = [ | ||
"Flat", | ||
"Epsilon", | ||
"Epsilon", | ||
"Z", | ||
"Z", | ||
] # Correct rules corresponding to analyzer input args | ||
|
||
for i, layer in enumerate(model.layers): | ||
for condition, rule in analyzer._rules: | ||
if condition(layer): | ||
rule_class = rule | ||
break | ||
assert rule_class == correct_rules[i] |