LIME for explaining Sentiment Predictions by XLNet

33 views Asked by At

I have trained the XLNet model for Sentiment Analysis on the IMDB Dataset and the code for making predictions looks like

def predict_sentiment(text):
    review_text = text

    encoded_review = tokenizer.encode_plus(
    review_text,
    max_length=MAX_LEN,
    add_special_tokens=True,
    return_token_type_ids=False,
    pad_to_max_length=False,
    return_attention_mask=True,
    return_tensors='pt',
    )

    input_ids = pad_sequences(encoded_review['input_ids'], maxlen=MAX_LEN, dtype=torch.Tensor ,truncating="post",padding="post")
    input_ids = input_ids.astype(dtype = 'int64')
    input_ids = torch.tensor(input_ids)

    attention_mask = pad_sequences(encoded_review['attention_mask'], maxlen=MAX_LEN, dtype=torch.Tensor ,truncating="post",padding="post")
    attention_mask = attention_mask.astype(dtype = 'int64')
    attention_mask = torch.tensor(attention_mask)

    input_ids = input_ids.reshape(1,512).to(device)
    attention_mask = attention_mask.to(device)

    outputs = model(input_ids=input_ids, attention_mask=attention_mask)

    outputs = outputs[0][0].cpu().detach()

    probs = F.softmax(outputs, dim=0).cpu().detach().numpy().tolist()
    _, prediction = torch.max(outputs, dim =0)
    return [probs]

Now when I try applying LIME for explaining the predictions like this

from lime.lime_text import LimeTextExplainer
explainer = LimeTextExplainer(class_names=class_names)

sample = "Movie is the worst one I have ever seen!! The story has no meaning at all"
exp = explainer.explain_instance(sample, predict_sentiment(sample), num_features = 100,top_labels=2)

I get the following error Any idea how do I explain the predictions using LIME ? enter image description here

0

There are 0 answers