albermax / innvestigate

A toolbox to iNNvestigate neural networks' predictions!
Other
1.24k stars 235 forks source link

Fix `is_layer_at_idx` for LRP #308

Closed Rubinjo closed 1 year ago

Rubinjo commented 1 year ago

Fixes the is_layer_at_idx function by binding the loop variable i to the lambda function and looping through the model's layers to find the corresponding index.

Closes #264

Example of fix

I have used a simple CNN network to test the implementation, see below the model summary.

model_summary

I used the following LRP settings.

analyzer = LRP(
    model,
    rule="Z",
    input_layer_rule="Flat",
    until_layer_idx=3,
    until_layer_rule="Epsilon",
)

For verification, I printed the rule object and the layer it will be applied to.

layer+rule

As you can see all three rules are being applied.

Discussion

The until_layer_idx argument will now count every layer in your model, also Input, Reshape, Pooling, etc.

adrhill commented 1 year ago

Thanks for the contribution, this looks good!

Could you add your example to the package tests, e.g. by adding a new file tests/backend/test_layer_idx.py?

Rubinjo commented 1 year ago

I have added a basic test similar to the example I discussed in my previous comment.

adrhill commented 1 year ago

Looks good to me and tests passed locally. Thanks! :)