Captum: LayerDeepLift for BERT

Created on 12 Apr 2020  路  7Comments  路  Source: pytorch/captum

Hi,

I want you use LayerDeepLift instead of LayerIntegratedGradients in your BERT tutorial. If I understand correctly, when it computes the attributions, I should pass the same parameters, but I am getting the following error:
RuntimeError: only Tensors of floating point dtype can require gradients

Any guidance?

question

All 7 comments

@ngopee, would you please post the code snippet here ? Are you passing token indices or the embeddings as inputs ?

In your BERT tutorial, I just switch:

lig = LayerIntegratedGradients(squad_pos_forward_func, model.bert.embeddings)

to

lig = LayerDeepLift(squad_pos_forward_func, model.bert.embeddings)

Thanks you very much!

LayerDeepLift and LayerIG are a bit different. LayerIG internally pre-computes and replaces the output of the hooked embedding layer. Is the error that you're seeing at the following line in LayerDeepLift ?

       gradient_mask = apply_gradient_requirements(inputs)

I see. Yes, This is exactly where the error is happening.

More detailed error message:

     18                                     baselines=ref_input_ids,
     19                                    additional_forward_args=(token_type_ids, position_ids, attention_mask, classx),
---> 20                                    return_convergence_delta=True)
     21 
     22 

.../captumENV/scripts/captum/captum/attr/_core/layer/layer_deep_lift.py in attribute(self, inputs, baselines, target, additional_forward_args, return_convergence_delta, attribute_to_layer_input, custom_attribution_func)
    223         inputs = _format_input(inputs)
    224         baselines = _format_baseline(baselines, inputs)
--> 225         gradient_mask = apply_gradient_requirements(inputs)
    226 
    227         _validate_input(inputs, baselines)

.../captumENV/scripts/captum/captum/attr/_utils/gradient.py in apply_gradient_requirements(inputs)
     27                 "required_grads has been set automatically." % index
     28             )
---> 29             input.requires_grad_()
     30         if input.grad is not None:
     31             if torch.sum(torch.abs(input.grad)).item() > 1e-7:

RuntimeError: only Tensors of floating point dtype can require gradients

Hi @ngopee, because the inputs are token indices of type int we cannot set gradients to those tensors. We need to set the gradients to the embedding vectors. To do that you can try to use InterpretableEmbeddingBase and try to compute the gradients and Deeplift scores w.r.t. those embedding vectors. Here is an example how to use InterpretableEmbeddingBase. Follow the example of how to configure interpretable embedding layer for the word embeddings.
https://captum.ai/tutorials/Multimodal_VQA_Interpret

DeepLift supports overrides for a limited number of non-linearities. In case of Bert it will have overrides for softmax but since softmax isn't defined in the model structure (F.softmax is used instead) the overrides won't happen. You'd need to redefine the architecture by explicitly initializing nn.Softmax in the init of your model. If you use DeepLift as is, the importance scores may be something close to:
(input - baseline) * gradients

@ngopee , do you have more questions ? Can we close this issue ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yangfantrinity picture yangfantrinity  路  5Comments

yanbek picture yanbek  路  5Comments

abhigoku10 picture abhigoku10  路  5Comments

hchong-ml picture hchong-ml  路  3Comments

gkakogeorgiou picture gkakogeorgiou  路  3Comments