omnixai.explanations.text package

word_importance

Word/token importance explanations for NLP tasks.

omnixai.explanations.text.word_importance module

Word/token importance explanations for NLP tasks.

class omnixai.explanations.text.word_importance.WordImportance(mode, explanations=None)

Bases: ExplanationBase

The class for word/token importance explanations. It uses a list to store the word/token importance scores of the input instances. Each item in the list is a dict with the following format {“instance”: the input instance, “tokens”: a list of tokens, “scores”: a list of feature importance scores}. If the task is classification, the dict has an additional entry {“target_label”: the predicted label of the input instance}.

Parameters
  • mode – The task type, e.g., classification or regression.

  • explanations – The explanation results for initializing WordImportance, which is optional.

add(instance, target_label, tokens, importance_scores, sort=False, **kwargs)

Adds the generated explanation of one instance.

Parameters
  • instance – The instance to be explained.

  • target_label – The label to be explained, which is ignored for regression.

  • tokens – The list of the words/tokens in the explanation.

  • importance_scores – The list of the corresponding word/token importance scores.

  • sort – Sort the features based on the importance scores if it is True.

get_explanations(index=None)

Gets the generated explanations.

Parameters

index – The index of an explanation result stored in WordImportance. When index is None, the function returns a list of all the explanations.

Returns

The explanation for one specific instance (a dict) or the explanations for all the instances (a list of dicts). Each dict has the following format: {“instance”: the input instance, “tokens”: a list of tokens, “scores”: a list of feature importance scores}. If the task is classification, the dict has an additional entry {“target_label”: the predicted label of the input instance}.

Return type

Union[Dict, List]

plot(index=None, class_names=None, num_tokens_per_class=5, max_num_subplots=4, **kwargs)

Returns a matplotlib figure showing the explanations.

Parameters
  • index – The index of an explanation result stored in WordImportance, e.g., it will plot the first explanation result when index = 0. When index is None, it shows a figure with max_num_subplots subplots where each subplot plots the word/token importance scores for one instance.

  • class_names – A list of the class names indexed by the labels, e.g., class_name = ['dog', 'cat'] means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.

  • num_tokens_per_class – The maximum number of tokens shown in the figure for each class.

  • max_num_subplots – The maximum number of subplots in the figure.

Returns

A matplotlib figure plotting word/token importance scores.

plotly_plot(index=None, class_names=None, num_tokens_per_class=5, max_length=512, **kwargs)

Returns a plotly dash figure plotting the explanations.

Parameters
  • index – The index of the instance, e.g., it will plot the first explanation result when index = 0.. For plotting all the results, set index to None.

  • class_names – A list of the class names indexed by the labels, e.g., class_name = ['dog', 'cat'] means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.

  • num_tokens_per_class – The maximum number of tokens shown in the figure for each class.

  • max_length – The maximum number of tokens to show.

Returns

A plotly dash figure plotting word/token importance scores.

ipython_plot(index=None, class_names=None, max_length=512, **kwargs)

Plots word/token importance scores in IPython.

Parameters
  • index – The index of the instance, e.g., it will plot the first explanation result when index = 0.. For plotting all the results, set index to None.

  • class_names – A list of the class names indexed by the labels, e.g., class_name = ['dog', 'cat'] means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.

  • max_length – The maximum number of tokens to show.

classmethod from_dict(d)