omnixai.explainers.nlp.specific package

ig

The integrated-gradient explainer for NLP tasks.

omnixai.explainers.nlp.specific.ig module

The integrated-gradient explainer for NLP tasks.

class omnixai.explainers.nlp.specific.ig.IntegratedGradientText(model, embedding_layer, preprocess_function, mode='classification', id2token=None, tokenizer=None, **kwargs)

Bases: ExplainerBase

The integrated-gradient explainer for NLP tasks. If using this explainer, please cite the original work: https://github.com/ankurtaly/Integrated-Gradients.

Parameters
  • model – The model to explain, whose type can be tf.keras.Model or torch.nn.Module.

  • embedding_layer – The embedding layer in the model, which can be tf.keras.layers.Layer or torch.nn.Module.

  • preprocess_function (Callable) – The pre-processing function that converts the raw inputs into the inputs of model. The first output of preprocess_function must be the token ids.

  • mode (str) – The task type, e.g., classification or regression.

  • id2token (Optional[Dict]) – The mapping from token ids to tokens. If tokenizer is set, id2token will be ignored.

  • tokenizer (Optional[Callable]) – The tokenizer for processing text inputs, i.e., tokenizers in HuggingFace.

explanation_type = 'local'
alias = ['ig', 'integrated_gradient']
explain(X, y=None, **kwargs)

Generates the word/token-importance explanations for the input instances.

Parameters
  • X (Text) – A batch of input instances.

  • y – A batch of labels to explain. For regression, y is ignored. For classification, the top predicted label of each input instance will be explained when y = None.

  • kwargs – Additional parameters, e.g., steps for IntegratedGradient.compute_integrated_gradients.

Return type

WordImportance

Returns

The explanations for all the instances, e.g., word/token importance scores.