Integrated Gradients with Captum in a sequential model

Hi, I am using Integrated Gradients with a NN model that has both sequential (LSTM) and non sequential layers (MLP). I was following the basic steps as given in the examples and ran into a weird error.
Code snippet:
test_tens.requires_grad_()
attr = ig.attribute(test_tens, target=1, return_convergence_delta=False)
attr = attr.detach().numpy()

error:
in line 2 of the above snippet:
" index 1 is out of bounds for dimension 1 with size 1 "

Probably some problem around the target thing, but not sure what to do

Thank you for posting the issue, @key_crusher! What are the shapes of the output of your model and test_tens?

Thank you for the reply @Narine_Kokhlikyan. The output dimensions = [batch_size, 1]. The shape of the test_tens = [batch_size, 103], here 103 is the feature count.

I see! If the output dim is: output dimensions = [batch_size, 1]. then you might want to use target=0. Try that and let me know if you see the same error.

Thank you @Narine_Kokhlikyan. This worked but I still don’t understand this. Doesn’t the target value mean the value to which we want to check the contribution of the corresponding features ?

Target value is basically the index to your output. In a specific case it could be predicted class index.
It looks like you have only one dimensional output per example that’s why it can only be 0 or you don’t even need to specify it and leave None

Here you can find a definition of it: https://christophm.github.io/interpretable-ml-book/terminology.html

So, if I understand this correctly, had I used SoftMax instead of Sigmoid, then my output dimensions would have been [ batch_size, num_classes ] and then the target value would have been used to determine the contribution to the specific class and it could take values from 0 to num_classes - 1

Is this correct ? @Narine_Kokhlikyan

1 Like

Yes, that sounds right!

@key_crushe Hello , I have question for you related to this if it is possible.
Why you did not specify baselines.
I have some code same, takes sequence of 341 and give one classes from {1,2,3} the model take batch of data tensor of shape [16,341] and return vector of length 16. I am new o capium but the following code did not work

ig = IntegratedGradients(model)
test_input_tensor, targets = next(iter(testloader))
test_input_tensor =test_input_tensor.requires_grad_()
attr = ig.attribute(test_input_tensor, target = 0 , return_convergence_delta=False)
attr = attr.detach().numpy()

the error is related to baseline i tried to specify basline of the same shape of input but it doest work
did i wrote somethong wrong here ?
any suggestion?
I