captum icon indicating copy to clipboard operation
captum copied to clipboard

Support for Identity layer in LRP

Open soupault opened this issue 4 years ago • 2 comments

🐛 Bug

Hi! Thank you for the great tool!

It appears that LRP fails if the model includes Identity layer (https://pytorch.org/docs/stable/generated/torch.nn.Identity.html):

  File "???.py", line 523, in my_fn
    attrs = lrp.attribute(inputs=xs,
  File "???/python3.8/site-packages/captum/log/__init__.py", line 35, in wrapper
    return func(*args, **kwargs)
  File "???/python3.8/site-packages/captum/attr/_core/lrp.py", line 183, in attribute
    self._check_and_attach_rules()
  File "???/python3.8/site-packages/captum/attr/_core/lrp.py", line 283, in _check_and_attach_rules
    raise TypeError(
TypeError: Module type <class 'torch.nn.modules.linear.Identity'> is not supported.No default rule defined.

Expected behavior

To my understanding, Identity should act as no-op for LRP. It seems currently it is just missing from the sanitizing function (https://github.com/pytorch/captum/blob/9b33f13aabdb0efe2d5404dfe6166d2c8c5d2a7c/captum/attr/_core/lrp.py#L267-L289). To solve the issue, I believe the layer should be handled within this branch - https://github.com/pytorch/captum/blob/9b33f13aabdb0efe2d5404dfe6166d2c8c5d2a7c/captum/attr/_core/lrp.py#L279-L280 .

Environment

Describe the environment used for Captum

 - Captum / PyTorch Version (e.g., 1.0 / 0.4.0): 1.8.2 / 0.4.0
 - How you installed Captum / PyTorch (`conda`, `pip`, source): conda

soupault avatar Oct 19 '21 20:10 soupault

Thank you for the good proposal, @soupault!

Would you like to create a PR with your solution (along with a test case)? We can then review it and make sure all other tests pass.

Thanks!

cc @nanohanno @NarineK

bilalsal avatar Oct 19 '21 21:10 bilalsal

@bilalsal absolutely! I will work on it in the coming days.

soupault avatar Oct 19 '21 22:10 soupault