Open pranshu1202 opened 3 years ago
import lime
import torch
import torch.nn.functional as F
from lime.lime_text import LimeTextExplainer
from transformers import AutoTokenizer, AutoModelForSequenceClassification
filename_model = 'ProsusAI/finbert'
tokenizer = AutoTokenizer.from_pretrained(filename_model)
model = AutoModelForSequenceClassification.from_pretrained(filename_model)
class_names = ['positive','negative', 'neutral']
def predictor(texts):
device = torch.device("cuda")
model.to(device)
input = tokenizer(texts, return_tensors="pt", padding=True)
input.to(device)
outputs = model(**input)
tensor_logits = outputs[0]
probas = F.softmax(tensor_logits.cpu()).detach().numpy()
return probas
text = '''void sort(int a[],int s)'''
print(tokenizer(text, return_tensors='pt', padding=True))
explainer = LimeTextExplainer(class_names=class_names)
exp = explainer.explain_instance(text, predictor, num_features=20, num_samples=200)
exp.show_in_notebook(text=text)
LIME : https://explainml-tutorial.github.io/assets/files/explainml-tutorial-neurips20.pdf https://www.slideshare.net/KrishnaramKenthapadi/explainable-ai-in-industry-kdd-2019-tutorial Video Playlist : https://www.youtube.com/playlist?list=PLewjn-vrZ7d3x0M4Uu_57oaJPRXkiS221
Influence Function : https://arxiv.org/pdf/2005.06676.pdf