def test_sequence_classification_viz(): explainer_string = "I love you , I like you" seq_explainer = SequenceClassificationExplainer(explainer_string, DISTILBERT_MODEL, DISTILBERT_TOKENIZER) seq_explainer() seq_explainer.visualize()
import pandas as pd import numpy as np import transformers #We can't use our initial dataset (3GB), too heavy, so instead... : file_name = "clothes.csv" ds = pd.read_csv(file_name) example = ds.iloc[ 15, 4] #Getting the right column (4), i.e. the one with the review classifier = transformers.pipeline('sentiment-analysis', return_all_scores=True) tokenizer = classifier.tokenizer model = classifier.model #Explaining using Sequence Classification Explainer from the transformers explainer from transformers_interpret import SequenceClassificationExplainer cls_explainer = SequenceClassificationExplainer(model, tokenizer) word_attributions = cls_explainer(example) cls_explainer.visualize("visualize.html")
# "0": "natural", # "1": "positive", # "2": "negative" from transformers import AutoModelForSequenceClassification, AutoTokenizer from transformers_interpret import SequenceClassificationExplainer if __name__ == "__main__": model_name = "avichr/heBERT" model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) # With both the model and tokenizer initialized we are now able to get explanations on an example text. cls_explainer = SequenceClassificationExplainer( "I love you, I like you", model, tokenizer) attributions = cls_explainer() # print(attributions.predicted_class_name) cls_explainer.visualize("outputs/explainer-viz.html")