marcotcr / lime

Lime: Explaining the predictions of any machine learning classifier
BSD 2-Clause "Simplified" License
11.54k stars 1.8k forks source link

Value error for more then one word ? any work through ? #723

Open Achinth04 opened 1 year ago

Achinth04 commented 1 year ago
import torch.nn.functional as f
texts='i dont know'

def predictor(texts):
 token=tokenizer(texts, return_tensors="pt", padding=True).to(device)
 print(token)
 outputs = model(**token)
 print(type(outputs))
 probas = f.softmax(outputs.logits,dim=1).detach().cpu().numpy()
 return probas
print(predictor(texts))
stringpreds='Parth is'
exp=explainer.explain_instance(stringpreds,predictor,num_samples=10)

when ever i include more than a word in thestringpreds i get a value error, it works perfectly fine for a single word i do not know what happens when i increase the word am using a layoutLMfortokenclassification model

this the error i get:

[/usr/local/lib/python3.10/dist-packages/lime/lime_base.py](https://localhost:8080/#) in feature_selection(self, data, labels, weights, num_features, method)
    132             else:
    133                 n_method = 'highest_weights'
--> 134             return self.feature_selection(data, labels, weights,
    135                                           num_features, n_method)
    136 

[/usr/local/lib/python3.10/dist-packages/lime/lime_base.py](https://localhost:8080/#) in feature_selection(self, data, labels, weights, num_features, method)
    108             else:
    109                 weighted_data = coef * data[0]
--> 110                 feature_weights = sorted(
    111                     zip(range(data.shape[1]), weighted_data),
    112                     key=lambda x: np.abs(x[1]),

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()