omnixai.explainers.nlp.agnostic package

lime

The LIME explainer for text classification.

shap

The SHAP explainer for text classification.

l2x

The L2X explainer for NLP tasks.

omnixai.explainers.nlp.agnostic.lime module

The LIME explainer for text classification.

class omnixai.explainers.nlp.agnostic.lime.LimeText(predict_function, mode='classification', **kwargs)

Bases: ExplainerBase

The LIME explainer for text classification. If using this explainer, please cite the original work: https://github.com/marcotcr/lime. This explainer only supports text classification tasks.

Parameters
  • predict_function (Callable) – The prediction function corresponding to the machine learning model to explain. When the task is classification, the outputs of the predict_function are the class probabilities.

  • mode (str) – The task type can be classification only.

  • kwargs – Additional parameters for lime_text.LimeTextExplainer. Please refer to the doc of lime_text.LimeTextExplainer.

explanation_type = 'local'
alias = ['lime']
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 classification, the top predicted label of each input instance will be explained when y = None.

  • kwargs – Additional parameters for LimeTextExplainer.explain_instance.

Return type

WordImportance

Returns

The explanations for all the input instances.

omnixai.explainers.nlp.agnostic.shap module

The SHAP explainer for text classification.

class omnixai.explainers.nlp.agnostic.shap.ShapText(model, mode='classification', **kwargs)

Bases: ExplainerBase

The SHAP explainer for text classification. If using this explainer, please cite the original work: https://github.com/slundberg/shap. This explainer only supports TextClassificationPipeline in the transformer library.

Parameters
  • model (Callable) – The model with type transformers.pipelines.text_classification.TextClassificationPipeline.

  • mode (str) – The task type can be classification only.

  • kwargs – Additional parameters for shap.Explainer. Please refer to the doc of shap.Explainer.

explanation_type = 'local'
alias = ['shap']
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 classification, the top predicted label of each input instance will be explained when y = None.

  • kwargs – Additional parameters for shap.Explainer.

Return type

WordImportance

Returns

The explanations for all the instances.

omnixai.explainers.nlp.agnostic.l2x module

The L2X explainer for NLP tasks.

class omnixai.explainers.nlp.agnostic.l2x.DefaultSelectionModel(explainer, **kwargs)

Bases: _DefaultModelBase

The default selection model in L2X for text data. It consists of five 1D convolution layers and one linear output layer.

Parameters
  • explainer – A L2XText explainer.

  • kwargs – Additional parameters, e.g., hidden_size – the hidden layer size, and kernel_size – the kernel size in 1D convolution.

forward(inputs, masks)
Parameters
  • inputs – The input IDs.

  • masks – The input masks.

training: bool
class omnixai.explainers.nlp.agnostic.l2x.DefaultPredictionModel(explainer, **kwargs)

Bases: _DefaultModelBase

The default prediction model in L2X for text data. It consists of three 1D convolution layers and one linear output layer by default.

Parameters
  • explainer – A L2XText explainer.

  • kwargs – Additional parameters, e.g., hidden_size – the hidden layer size, and kernel_sizes – the kernel sizes in the 1D convolution layers.

forward(inputs, masks, weights)
Parameters
  • inputs – The input IDs.

  • masks – The input masks.

  • weights – The weights generated via Gumbel-Softmax sampling.

training: bool
class omnixai.explainers.nlp.agnostic.l2x.L2XText(training_data, predict_function, mode='classification', tau=0.5, k=10, selection_model=None, prediction_model=None, loss_function=None, optimizer=None, learning_rate=0.001, batch_size=None, num_epochs=20, **kwargs)

Bases: ExplainerBase

The LIME explainer for text data. If using this explainer, please cite the original work: Learning to Explain: An Information-Theoretic Perspective on Model Interpretation, Jianbo Chen, Le Song, Martin J. Wainwright, Michael I. Jordan, https://arxiv.org/abs/1802.07814.

Parameters
  • training_data (Text) – The data used to train the explainer. training_data should be the training dataset for training the machine learning model.

  • predict_function (Callable) – The prediction function corresponding to the model to explain. When the model is for classification, the outputs of the predict_function are the class probabilities. When the model is for regression, the outputs of the predict_function are the estimated values.

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

  • tau (float) – Parameter tau in Gumbel-Softmax.

  • k (int) – The maximum number of the selected features in L2X.

  • selection_model – A pytorch model class for estimating P(S|X) in L2X. If selection_model = None, a default model DefaultSelectionModel will be used.

  • prediction_model – A pytorch model class for estimating Q(X_S) in L2X. If selection_model = None, a default model DefaultPredictionModel will be used.

  • loss_function (Optional[Callable]) – The loss function for the task, e.g., nn.CrossEntropyLoss() for classification.

  • optimizer – The optimizer class for training the explainer, e.g., torch.optim.AdamW.

  • learning_rate (float) – The learning rate for training the explainer.

  • batch_size (Optional[int]) – The batch size for training the explainer. If batch_size is None, batch_size will be picked from [32, 64, 128, 256] based on the sample size.

  • num_epochs (int) – The number of epochs for training the explainer.

  • kwargs – Additional parameters, e.g., parameters for selection_model and prediction_model.

explanation_type = 'local'
alias = ['l2x', 'L2X']
explain(X, **kwargs)

Generates the explanations for the input instances. For classification, it explains the top predicted label for each input instance.

Parameters

X (Text) – A batch of input instances.

Return type

WordImportance

Returns

The explanations for all the input instances.

save(directory, filename=None, **kwargs)

Saves the initialized explainer.

Parameters
  • directory (str) – The folder for the dumped explainer.

  • filename (Optional[str]) – The filename (the explainer class name if it is None).