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

when there are many input features ,how to use the IntegratedGradients? #1312

Open
Sarah-air opened this issue Jul 18, 2024 · 5 comments
Open

Comments

@Sarah-air
Copy link

❓ Questions and Help

the classification model is classification_pred = model([input_x[0], input_x[1],input_x[2], input_x[3]], os_event.size(0),
 {'gex': 0, 'methy': 1, 'mut':2, 'cna': 3}),the input_x of one sample is as the figure showed,how to use the IntegratedGradients
to get the four features' attribution?image

@khairulislam
Copy link

Hi @Sarah-air , if your model uses all 4 inputs, then normal passing is fine. but if it doesn't, you'll have to pass only the inputs that your model uses to the attribute method as input and the rest as additional forward args. The Gradient method throws an error if any input is unused. Check our example here.

@phet1999
Copy link

Hi @khairulislam, I have the same problem with this bro and I don't know how to utilize this code for multi-inputs. For example, if I have four different inputs for the model. Is it the code should be like this?

'''ig = IntegratedGradients(model)
input1 = torch.Tensor(train_X_hist[:20])
input2 = torch.Tensor(train_X_ex_hist[:20])
input3 = torch.Tensor(train_X_ex_fu[:20])
input4 = torch.Tensor(train_X_static[:20])
input = (input1,input2,input3,input4)
baseline1 = torch.zeros_like(input1)
baseline2 = torch.zeros_like(input2)
baseline3 = torch.zeros_like(input3)
baseline4 = torch.zeros_like(input4)
baseline = (baseline1,baseline2,baseline3,baseline4)
IG, delta = ig.attribute(input,baseline, target=1, return_convergence_delta=True)'''

But it reports that

"AttributeError: 'tuple' object has no attribute 'shape'"

@khairulislam
Copy link

Hi @phet1999 , try interpreting one input at a time. Then add the returned interpretations in a tuple. You could have a variable inside the model class to set which input no is being passed. Then the default call method will arrange them 1-4 and call the original call method. the gradient calculation is w.r.t one input only, hence it won't directly support multiple inputs.

additional forward args = (input2,input3,input4)
baseline = (baseline2,baseline3,baseline4)
IG, delta = ig.attribute(
  input1,baseline, target=1, return_convergence_delta=True, 
  additional_forward_args=additional_forward_args
)

@phet1999
Copy link

Hi @khairulislam, thank you for your replying, but problems still exist.
Here is the code, I first define a new forward function.
'''
def forward(self,
x_extra_future: torch.Tensor,
*args
) -> torch.Tensor:
'''

Then, I want to get the attribution of the first input feature
'''
additional_forward_args = (input2,input3,input4)
baseline = baseline1
IG, delta = ig.attribute(
input1,baseline1, target=1, return_convergence_delta=True,
additional_forward_args=additional_forward_args
)
'''

but it still comes with a problem that
'''
AttributeError: 'tuple' object has no attribute 'shape'
'''

@phet1999
Copy link

Hi, @khairulislam. I am sorry for bothering and I tried another method that sets

model.input1 = torch.Tensor(train_X_hist[:20]).to(device)
model.input2= torch.Tensor(train_X_ex_hist[:20]).to(device)
model.input3 = torch.Tensor(train_X_static[:20]).to(device)

and inside forward function, it acts like this

x_hist,x_extra_hist,x_static = self.input1,self.input2,self.input3

In this situation, there is just one input in the forward function. But the problem is still not solved for this code

ig = IntegratedGradients(model)
    IG, delta = ig.attribute(
    input, baseline, target=1, return_convergence_delta=True
    )

It said that

RuntimeError: Sizes of tensors must match except in dimension 2. Expected size 1000 but got size 20 for tensor number 1 in the list.

It seems that the code repeats my input 50 times at the batch channel(the same as the number of parameters n_steps, and when I set it to 1, the problem is 'AttributeError: 'tuple' object has no attribute 'shape'')

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

No branches or pull requests

3 participants