Captum: Integrated gradients using with pack_padded_sequence returns error

Created on 4 Feb 2020  路  4Comments  路  Source: pytorch/captum

Hi all,

I am using integrated gradient (IG) package from Captum package, which I apply one LSTM on varying length sequences and then I try to get IG from the trained model using the following line of code:

attr, delta = ig.attribute((data, seq_lengths), target=1, return_convergence_delta=True)

but I am getting the following error:

RuntimeError: lengths array must be sorted in decreasing order when enforce_sorted is True. You can pass enforce_sorted=False to pack_padded_sequence and/or pack_sequence to sidestep this requirement if you do not need ONNX exportability.

however, I have sorted the lengths of the array in each batch in decreasing order.
please note that If I use this IG without using pack_padded_sequence it works perfectly.

regarding the previous error, I set enforce_sorted=False in pack_padded_sequence but I am getting another error:

RuntimeError: Length of all samples has to be greater than 0, but found an element in 'lengths' that is <= 0

Here is the length of all the samples which none of them are less than zero:

tensor([23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 21, 21, 21, 20,
14, 10])

any help would be much appreciated.

attribution question

Most helpful comment

@NarineK Thanks for your constructive answer. Solved the issue by passing seq_lengths as an additiona_forward_arg

All 4 comments

@mostafaalishahi, if you pass (data, seq_lengths) then it will try to attribute both to input tensor and sequence length which wouldn't make much sense. You can pass seq_lengths as an additional_forward_args .

I need to know more about the inputs and model in order to understand how the problem emerges.
Does data contain token indices or is that the embedding tensor ?
Are you using a JIT model ?

We have couple tutorials for text:
https://captum.ai/tutorials/IMDB_TorchText_Interpret
https://captum.ai/tutorials/Multimodal_VQA_Captum_Insights

@NarineK Thanks for your reply, here is the model

class BiRNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_classes):
        super(BiRNN, self).__init__()
        self.num_layers  = num_layers
        self.num_classes = num_classes
        self.drop = nn.Dropout(p=0.2)
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first = True, bidirectional = True)
        self.fc = nn.Linear(hidden_size*2, num_classes) # 2 for bidirection
        self.sigmoid = torch.nn.Sigmoid()
    def forward(self,x, seq_lengths):

        packed_input = pack_padded_sequence(x, seq_lengths, batch_first=True,enforce_sorted=False)

        out, _ =  self.lstm(packed_input) # out: tensor of shape (batch_size, seq_length, hidden_size*2)

        output, input_sizes = pad_packed_sequence(out, batch_first=True)        
        output = self.drop(output)
        output = self.fc(output[:,-1,:])

        return output

Actually the data is not text and is in structured form without any embeddings. data shape is (20000,24,36).
Please note that I ran the model without using Integrated Gradients and it worked, and Integrated Gradients works fine without pack_padded_sequence.

Thank you @mostafaalishahi ! IG internally expands the input to approximate the integral.
You can try to put printouts right before calling IG and before pack_padded_sequence and look into the input shapes. I'd pass seq_lengths as an additional_forward_arg.

@NarineK Thanks for your constructive answer. Solved the issue by passing seq_lengths as an additiona_forward_arg

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xiaoda99 picture xiaoda99  路  3Comments

hal-314 picture hal-314  路  5Comments

AvantiShri picture AvantiShri  路  5Comments

kmario23 picture kmario23  路  4Comments

gorogoroyasu picture gorogoroyasu  路  5Comments