ThilinaRajapakse / simpletransformers

Transformers for Information Retrieval, Text Classification, NER, QA, Language Modelling, Language Generation, T5, Multi-Modal, and Conversational AI
https://simpletransformers.ai/
Apache License 2.0
4.09k stars 728 forks source link

[Help Lime] Explain classification results with LIME #375

Closed databill86 closed 3 years ago

databill86 commented 4 years ago

Describe the bug Actually this is not a bug. I created a ClassificationModel with 4 classes, and tried to explain the results with LIME. I'm having weird results, so I just want to check if my method was correct.

Here is the code :


class Prediction:

    def __init__(self):

        self.model = ClassificationModel('bert', 'outputs/', use_cuda=False, args={}) 
        self.tokenizer = self.model.tokenizer
        self.max_seq_length = 128
        self.device = "cpu"

    def predictor(self, texts):
        for text in texts: 
            preds, raw_outputs = self.model.predict([text])
            results.append(raw_outputs[0])

        ress = [res for res in results]
        results_array = np.array(ress)
        return results_array 

Then with Lime:

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

prediction = Prediction()
text= "Bueno, me gustaría saber qué talla debo coger"
exp = explainer.explain_instance(text, prediction.predictor, labels=(0,1,2,3), num_features=10, num_samples=len(text.split()))
exp.show_in_notebook(text=True)

I did find some solutions on how to use Lime with BERT, but I guess it is not the same thing with simpletransformers. There is an example here : https://github.com/marcotcr/lime/issues/409

Thank you !

ThilinaRajapakse commented 4 years ago

I haven't tried Lime so I'm afraid I can't give you any concrete examples. What do you mean by weird results though?

databill86 commented 4 years ago

The results seem to be random, they change every time I rerun the explainer.

ThilinaRajapakse commented 4 years ago

I can't see anything wrong with the code itself. Do the predictions change every time you run it as well?

iknoorjobs commented 4 years ago

Hi @databill86

Did you figure out the problem? Were you able to run LIME with simple transformers? If yes, please let us know.

Thanks

databill86 commented 4 years ago

Hi @iknoorjobs,

No still the same issue, the results are still not stable. I'm not sure I'm doing it right : In the call of explainer.explain_instance, i'm passing num_samples=len(text.split()), I'm not sure if this is right. I did because if was the only way to make it work.

goru001 commented 4 years ago

@databill86 @iknoorjobs I think you should increase num_samples to something around 100. The way LIME works is it tries to build interpretable models to explain predictions and those interpretable models are built using num_samples samples. If the num_samples is too low, variance is expected in those interpretable models. It should work out fine after this change.

iknoorjobs commented 4 years ago

@databill86 @iknoorjobs I think you should increase num_samples to something around 100. The way LIME works is it tries to build interpretable models to explain predictions and those interpretable models are built using num_samples samples. If the num_samples is too low, variance is expected in those interpretable models. It should work out fine after this change.

@goru001 Thanks for the update.

ThilinaRajapakse commented 4 years ago

Is this solved now?

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.