question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Constructing reference/baseline for LSTM - Layer Integrated Gradients

See original GitHub issue

`# compute attributions and approximation delta using layer integrated gradients

attributions_ig, delta = lig.attribute((input_indices,h), (reference_indices,h),
n_steps=500, return_convergence_delta=True) ` When I run the above code, I get the following error. AssertionError: baseline input argument must be either a torch.Tensor or a number however <class ‘tuple’> detected

But the documentation says that a tuple can be passed as baseline argument.

Am I doing this right?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
miguelmartin75commented, Dec 1, 2020

A tuple can be passed as a baseline (the tuple represents multiple inputs to the model), but this tuple is should effectively be a Tuple[Union[PythonScalar, Tensor], ...]. Based off your error, it seems that your h is actually a tuple (it’s not clear from your comments but perhaps you’re doing out, h = model.lstm(x) rather than out, (h, c) = model.lstm(x)).

Note that the inputs to LayerIG will be fed to the model directly. So if you want to pass in the hidden state you’d have to handle that in your model’s forward function.

However, with LayerIntegratedGradients, if given the LSTM layer, it should patch the last hidden state for you (i.e. the output of the LSTM Layer; it will also patch the other outputs as well - specifically the last cell state and output of the lstm itself).

I am assuming the last hidden state is what you want to patch, since this is the most common use-case. Unless you need to set the hidden state(s) to something specific, you should be able to just use LayerIntegratedGradients in the same way the tutorial shows.

If the case is you want to set the hidden state(s) to something else (i.e. a hidden state that does not correspond to the reference indices), e.g. a random vector, then I think you have a couple of options:

  1. Feed the hidden state(s) to a dummy module (e.g. the identity) and pass this dummy module to LayerIntegratedGradients as the layer. Additionally you’d have to have a flag in this dummy module to output your desired baseline for the hidden state(s).
  2. Do what LayerIntegratedGradients does manually

I think (1) is likely your best/easiest option in this case.

1reaction
devpramodcommented, Dec 1, 2020

Thank you very much for your reply. I’ll try these out as soon as possible and report back here to close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating LSTM attributions with integrated gradients
We first learned about integrated gradients (IG) in Chapter 8, Visualizing Convolutional Neural Networks. Unlike the other gradient-based attribution methods ...
Read more >
Understand What Drives Predictions in Deep NLP Models | by ...
We'll focus on a method called integrated gradients, and use a pre-trained review sentiment classifier coming from the well known fast.ai ...
Read more >
Integrated Gradients - Captum
This section of the documentation shows how to apply integrated gradients on models with different types of parameters and inputs using Captum.
Read more >
Characterizing the sequence and prior chromatin ... - bioRxiv
Specifically, our sequence-only network MS uses a convolutional layer followed by a long short-term memory (LSTM) layer and multiple dense layers (Fig. 1a)....
Read more >
Model interpretability with Integrated Gradients - Keras
Identify the input and the output. · Compute which features are important to a neural network when making a prediction on a particular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found