omnixai.explainers.nlp.counterfactual package

polyjuice

The NLP counterfactual explainer based on the Polyjuice model.

omnixai.explainers.nlp.counterfactual.polyjuice module

The NLP counterfactual explainer based on the Polyjuice model.

class omnixai.explainers.nlp.counterfactual.polyjuice.Polyjuice(predict_function, mode='classification', **kwargs)

Bases: ExplainerBase

The counterfactual explainer for NLP tasks. The method implemented here is based on the model developed by Wu et al., please cite the work: https://github.com/tongshuangwu/polyjuice if using this explainer.

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. When the task is qa (question-answering), the outputs are the generated answers.

  • mode (str) – The task type, e.g., classification pr qa (question-answering).

  • kwargs – Additional parameters, e.g., model_path and cuda.

explanation_type = 'local'
alias = ['polyjuice']
explain(X, max_number_examples=5, **kwargs)

Generates the counterfactual explanations for the input instances.

Parameters
  • X (Text) – A batch of input instances. For question-answering tasks, each instance in X has format [context] [SEP] [question], i.e., concatenating the context and question with seperator [SEP].

  • max_number_examples (int) – The maximum number of the generated counterfactual examples for each input instance.

  • kwargs – Additional parameters for polyjuice.Polyjuice, e.g., “ce_type” - the perturb type (“perturb” or “blank”).

Return type

CFExplanation

Returns

The explanations for all the input instances.