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

Update analyzer init and serialization #266

Merged
merged 10 commits into from
Aug 3, 2021

Conversation

adrhill
Copy link
Collaborator

@adrhill adrhill commented Jul 22, 2021

Following up on the refactoring of the backend in #263, this PR makes the analyzers more transparent.
Tests covering the new serializations will be added in a separate PR.

Changes

  1. make all class attributes explicit in the class __init__ and remove dynamic tricks using getattr-defaults
  2. make class initialization more transparent by always calling super().__init__() first
  3. move AnalyzerNetworkBase and ReverseAnalyzerBase into separate files
  4. fix / add missing serialization for WrapperBase, LRPSequentialPreset*Flat and BoundedDeepTaylor analyzers

also adds more type annotations, comments, and linting changes.

update imports accordingly and remove "comment walls".
This commit makes all class attributes explicit in the class __init__.

This means that some dynamic tricks using `getattr` defaults had to be refactored, the most notable being the function `_do_model_checks()` in base.py. As a consequence, `super().__init__()` can now always be called first on initialization, which makes it much more transparent. As a consequence, each analyzer has to manually call `_do_model_checks()` at the end of initialization.

Other improvements:
* switched to Python 3 style super without arguments
* add type annotations, e.g. using TypedDicts like `ModelCheckDict` and `CondReverseMapping`
* broke out `_add_lambda_layer_check` in network_base.py
* add warning on change of `neuron_selection_mode` in AugmentReduceBase
* remove boolean attribute `_keras_based_augment_reduce` from AugmentReduceBase and adapt `analyze()`
* removed some duplicate softmax checks
* removed lists from kwarg defaults
* made `BASELINE_LRPZ_LAYERS` global
previously, calling `analyzer.load()` would result in duplicate `input_layer_rule` kwargs. Popping the kwarg when it exists seems like a reasonable workaround. Rules differing from "Flat" now result in an error, which I think is preferable to having them silently overwritten.
add separate function `_get_neuron_selection_array` for neuron_selection_mode "index"
@adrhill adrhill requested a review from albermax July 22, 2021 13:43
@codecov-commenter
Copy link

codecov-commenter commented Jul 22, 2021

Codecov Report

Merging #266 (7419aa4) into master (32ead9d) will decrease coverage by 1.23%.
The diff coverage is 82.07%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #266      +/-   ##
==========================================
- Coverage   72.65%   71.41%   -1.24%     
==========================================
  Files          43       45       +2     
  Lines        4136     4216      +80     
  Branches      614      619       +5     
==========================================
+ Hits         3005     3011       +6     
- Misses        947     1017      +70     
- Partials      184      188       +4     
Impacted Files Coverage Δ
src/innvestigate/utils/keras/graph.py 49.28% <ø> (-7.16%) ⬇️
src/innvestigate/analyzer/deeptaylor.py 79.31% <71.42%> (-10.06%) ⬇️
src/innvestigate/analyzer/misc.py 87.50% <73.33%> (-12.50%) ⬇️
...stigate/analyzer/relevance_based/relevance_rule.py 71.42% <73.80%> (+0.76%) ⬆️
src/innvestigate/analyzer/base.py 72.63% <77.55%> (-7.50%) ⬇️
src/innvestigate/analyzer/network_base.py 80.80% <80.80%> (ø)
src/innvestigate/analyzer/wrapper.py 87.97% <83.09%> (+1.63%) ⬆️
...ate/analyzer/relevance_based/relevance_analyzer.py 79.03% <83.65%> (-2.29%) ⬇️
src/innvestigate/analyzer/gradient_based.py 81.19% <84.31%> (-2.88%) ⬇️
src/innvestigate/analyzer/reverse_base.py 86.61% <86.61%> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 32ead9d...7419aa4. Read the comment docs.

Move class attribute `neuron_selection_mode` to parent class. This is useful as it is also used in `WrapperBase`, which just inherits from `AnalyzerBase`.
use previous mechanism of overwriting the subanalyzer's `neuron_selection_mode`. Add comments and assert subanalyzer neuron selection mode.
@albermax albermax merged commit db8a58e into albermax:master Aug 3, 2021
@adrhill adrhill deleted the adrhill/update-analyzers branch August 3, 2021 20:39
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.

None yet

3 participants